ios - 如何在ios5中更改textview特定单词的文字颜色

标签 ios uitextview

如何在 ios5 中更改 textview 文本的特定单词的文本颜色。我已经实现了一个应用程序,我在 textview 中显示了 mangalAshtak shlok。它还有播放按钮,可以播放 mangal ashtak 的音频文件。现在我想将音频文件与 textview 同步。我已经在 ios 6 中使用 NSMutableAttributedString 实现了这个,如下所示

   if(seconds>=42 && seconds<43) 
   {
    NSMutableAttributedString * string = [[NSMutableAttributedString   alloc]initWithString:self.textView.text]; 
   NSString *word= [shlok2 objectAtIndex:0]; //array shlok 2 contains each word of shlok       
   NSLog(@"%@",word); 
   NSRange range=[self.textView.text rangeOfString:word]; 
  [string addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range]; 
   [self.textView setAttributedText:string]; 
   [textView setFont:[UIFont systemFontOfSize:16]]; 
}

使用上面的代码,它改变了音频文件文本的颜色。在播放按钮操作中,我检查了 AVAudioPlayer 对象的当前时间并更改了相应的文本。但是在 ios 5 中它不起作用?

最佳答案

仅在 iOS 6 中添加了对 UIKit 类(如 UITextView)中的属性字符串的支持。要在较早的系统上执行此操作,您必须使用 Core Text 或类似 DTCoreText 的框架自己绘制字符串。 .

关于ios - 如何在ios5中更改textview特定单词的文字颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17742728/

相关文章:

objective-c - 用opengl绘制少于每一帧

ios - 无法从 UNNotificationServiceExtension 中安排任务 - 错误 : BGTaskSchedulerErrorDomain error 1

android - 具有捕获和接受属性的 HTML 文件输入控件工作错误?

ios - 属性检查器中未显示已安装的选项

ios - 防止 UITextView.typingattributes 在 Swift 中的换行符 (\n) 上应用 backgroundColor

IOS Swift 处理全局事件

html - UITextView html 文本 - 引用项目中的字体

ios - UITextView/UILabel Swift 中每个链接的不同文本属性

ios - 从 UIButton 编辑 UItextview

ios - 在用 Swift 编写的 Xcode 中的 iOS 应用程序中的 UITextView 中,将 UIButton 放置在文本的末尾