Update UITextView content dynamically in a specific range

I need to dynamically update the words in the content of the UiTextView and the TTS without refreshing the page.

Problem: During the call to tts, for each word, the delegate (willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance)

is called. so at this time I am updating the text color with NSMutableAttributedString

.

In this process, the content in text mode is unstable because the word is updated for each page.

so I need to update the text in text mode without refreshing the page.

I wrote some code like this.

NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString: _theContent];

[attrStr addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: self.fontSize] range: [self wholeArticleRange]];

[attrStr addAttribute: NSForegroundColorAttributeName value: [UIColor redColor]: theRange];

textview.attributedText = attrStr;

- In android we used a spannable class for this. Is there any class in IOS?

Please give us a solution as soon as possible

Thanks in advance.

+3


source to share


3 answers


You can access the text view type textStorage

, of the type NSTextStorage

that is subclassed NSMutableString

. From there, you can use any operation NSMutableString

, including setting attributes in different ranges, without sending a completely newline to the text view (which will make it flicker).



Remember to call beginEditing

and endEditing

to group changes together if you need to make multiple calls to improve performance.

+1


source


I wrote code like this. I am new to IOS, so if I am wrong please kindly correct me.

  • (NSMutableDictionary *) attDict {

    if (! _attDict) {_attDict = [NSMutableDictionary];

    UIFont *font = [UIFont systemFontOfSize:self.fontSize];
    // UIFont *font = [UIFont fontWithName:@"TimesNewRomanPSMT" size:self.fontSize];
     [_attDict setObject:font forKey:NSFontAttributeName];
    [_attDict setObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName];
    
          

    } return _attDict; }

    [[displayArticleViewViewView] .textStorage setAttributes: self.attDict range: theRange];



thank

+2


source


If you just want to replace any text in your UITextField with something else on the fly, you can use the string replacement method, Like

[textField.text stringByReplacingOccurrencesOfString:@"your string" withString:@"string to be replaced"];

      

This will replace all occurrences of the string (the string being replaced) with the replacement.

0


source







All Articles