【问题标题】:Update the UITextView content dynamically in a specific range在特定范围内动态更新 UITextView 内容
【发布时间】:2015-01-19 13:22:39
【问题描述】:

我需要在不刷新页面的情况下在播放 TTS 时动态更新 UiTextView 内容中的单词。

问题:当 tts 播放每个单词时,delegate(willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance) 正在调用。 所以当时我正在使用NSMutableAttributedString更新文本颜色。

在这个过程中,textview中的内容是不稳定的,因为每个word页面都在刷新。

所以我需要在不刷新页面的情况下更新 textview 中的文本。

我是这样写代码的。

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

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

[attrStr addAttribute:NSForegroundColorAttributeName 值:[UIColor redColor] range:theRange];

textview.attributedText = attrStr;

--在android中我们使用spannable class来做到这一点。 IOS有这样的类吗?

请尽快给我们解决方案

提前致谢。

【问题讨论】:

    标签: ios objective-c uitextview text-to-speech


    【解决方案1】:

    我是这样写代码的。我是IOS新手,如有错误请指正。

    • (NSMutableDictionary *)attDict {

      如果 (!_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];
      

      } 返回_attDict; }

      [[displayingArticleView contentView].textStorage setAttributes:self.attDict range:theRange] ;

    谢谢

    【讨论】:

      【解决方案2】:

      您可以访问文本视图的textStorage 属性,类型为NSTextStorage,它是NSMutableString 的子类。从那里您可以使用任何NSMutableString 操作,包括设置各种范围的属性,而无需向文本视图发送全新的字符串(这会使其闪烁)。

      如果您必须进行多次调用,请不要忘记调用beginEditingendEditing 将编辑组合在一起,以提高性能。

      【讨论】:

      • 感谢您的大力帮助。我想我的问题已经解决了。
      【解决方案3】:

      如果你只是想用其他文本替换 UITextField 中的任何文本,你可以使用字符串替换方法,比如

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

      这将用替换替换所有出现的目标(要替换的字符串)字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2012-08-27
        • 1970-01-01
        • 2016-08-14
        • 2012-02-11
        • 2016-05-20
        • 1970-01-01
        相关资源
        最近更新 更多