How to change color of text strings inside UITextView in Swift3

后端 未结 1 1415
清歌不尽
清歌不尽 2020-12-07 00:10

I have looked at some existing questions posts here and I tried to implement all code but ended up not being able to successfully implement any code. Actually, I think I com

相关标签:
1条回答
  • 2020-12-07 00:35

    In this I have taken string "world" as example. When you r typing in textview then This method is in working

    func textViewDidChange(_ textView: UITextView) {
            let attrStr = NSMutableAttributedString(string:txtview.text)
            let inputLength = attrStr.string.characters.count
            let searchString = "world"
            let searchLength = searchString.characters.count
            var range = NSRange(location: 0, length: attrStr.length)
    
            while (range.location != NSNotFound) {
                range = (attrStr.string as NSString).range(of: searchString, options: [], range: range)
                if (range.location != NSNotFound) {
                    attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location: range.location, length: searchLength))
                    range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
                    textView.attributedText = attrStr
                }
            }    
    }
    

    But when You have already set initial text of textview then use this method

    func textViewDidBeginEditing(_ textView: UITextView) {
            //Just set your text as you set in textview
            let attrStr = NSMutableAttributedString(string: "hello world I ma jeckjklwefljlwjflkjwfkljelwfjklfgjwklfjlkwgjwlgkjwklsgjklsjgklsdjgkljdslkgjsdlkgjlksdjgldjsgldjskl world nsfhjklshfklhsllsd fgiw world")
            let inputLength = attrStr.string.characters.count
            let searchString = "world"
            let searchLength = searchString.characters.count
            var range = NSRange(location: 0, length: attrStr.length)
    
            while (range.location != NSNotFound) {
                range = (attrStr.string as NSString).range(of: searchString, options: [], range: range)
                if (range.location != NSNotFound) {
                    attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location: range.location, length: searchLength))
                    range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
                    textView.attributedText = attrStr
                }
              }  
      }
    

    For Multiple Strings, You can do like this SWIFT 3

    func textViewDidChange(_ textView: UITextView) {
            let attrStr = NSMutableAttributedString(string: txtview.text)
            let inputLength = attrStr.string.characters.count
            let searchString : NSArray = NSArray.init(objects: "hello","world")
            for i in 0...searchString.count-1
            {
    
                 let string : String = searchString.object(at: i) as! String
                 let searchLength = string.characters.count
                 var range = NSRange(location: 0, length: attrStr.length)
    
                 while (range.location != NSNotFound) {
                     range = (attrStr.string as NSString).range(of: string, options: [], range: range)
                     if (range.location != NSNotFound) {
                     attrStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSRange(location: range.location, length: searchLength))
                     range = NSRange(location: range.location + range.length, length: inputLength - (range.location + range.length))
                     textView.attributedText = attrStr
            }
        }   
      }
    }
    

    OBJECTIVE C
    For Multiple Strings, You can do like this

    -(void)textViewDidChange:(UITextView *)textView
    {
        NSMutableAttributedString *attstr = [[NSMutableAttributedString alloc]initWithString:textView.text];
        NSUInteger characterCount = [attstr length];
        NSArray *arr = [[NSArray alloc]initWithObjects:@"football",@"player",nil];
    
        for (int i=0; i<arr.count; i++) {
    
        NSUInteger searchlength = [[NSString stringWithFormat:@"%@",[arr objectAtIndex:i]] length];
        NSRange range1 = NSMakeRange(0, attstr.length);
    
        while (range1.location != NSNotFound) {
            range1 =[attstr.string rangeOfString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:i]] options:0 range:range1];
            if (range1.location !=NSNotFound) {
                [attstr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(range1.location, searchlength)];
                [attstr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20.0]  range:range1];
                range1 = NSMakeRange(range1.location + range1.length, characterCount -(range1.location + range1.length));
                textView.attributedText = attstr;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题