1

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 comprehended how to change colors of some texts inside UITextView if a text is initially set, but what I do not understand is that when my UITextView began entering editing, it does not seem to be working properly at all. In the following, it is my attempted code that closely full fills my desired behaviour.

 func textViewDidBeginEditing(_ textView: UITextView) {

    let main_string = ""
    let getData: [String] = userDefaults.object(forKey: "myData") as! [String]
    print("\(getData)")

    let searchWords = "world"

        let range = (main_string as NSString).range(of: searchWords)

        let attribute = NSMutableAttributedString.init(string: main_string)
        attribute.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: range)

        mytextView.attributedText = attribute


}

I saved input data by naming MyData. I tried to use a for-loop to take all elements out of getData and use it as individual values. However, in the console, there are so many lines of explanation. I think this is a proper way to write, but my mac says nooooooo. However, If I set a String inside main_string and searchWords, for example, "this is my question. I want people to help me from the world" in main_string and "world" in searchWords as written in the code. Then after the app is being loaded, on my text view the word, "world" is highlighted in red perfectly, but after the world word, all text is in red. I do not understand why I am being tortured by this unknown bug.

So What I want to accomplish is

  1. from the saved date in my myData, I only want the stored words to be highlighted timely when users type in. For example, if a user types "hello" and "world". Then they are saved by myData and they are going to be highlighted only when typed. And switch back to the normal color when words which are not stored are typed.

I assume I lack some explanation. If you need to know something, please point out for me. Thanks very much!!

1
  • Check my answer Commented Oct 20, 2016 at 10:33

1 Answer 1

5

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

13 Comments

This is very helpful. Thank you very much. But I have one more question. If there are several words in a String array, how am I able to implement such code?
@ryohei I will update my answer with your question but according to your very first question, My answer is perfect, So please approve for others
@ryohei I have updated my answer with your requirement Like I have Multiple strings and I have to change color of all occurences
Ok, Just wait for it. I will change it to swift but If its working in swift you have to approve my answer
Okay, I approved it but it is not working for me... So I appreciate your change into Swift thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.