我的应用程序中有下面的代码
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:self.myDisplayTxt];
[string addAttribute:(NSString*)kCTForegroundColorAttributeName
value:(id)[[UIColor redColor] CGColor]
range:NSMakeRange(0,5)];
self.myTextView.text = string;当将NSMutableAttributedString分配给UITextView时,我得到以下错误:
不兼容的目标c类型结构NSMutableAttributedString预期的结构nsstring
所以请告诉我,如何在NSMutableAttributedString中显示UITextView。
发布于 2012-04-25 10:21:22
你可以尝试使用一些库来做这件事。正如omz所写的,UITextView并不不幸地支持NSAttributedString。
也许这个可以帮你:https://github.com/enormego/EGOTextView
他们对这个图书馆的看法如下:
UITextView取代了对NSAttributedString的额外支持。
更新:基于您在评论中对omz的回答所作的澄清,您可以在这里查看:Underline text inside uitextview
更新2: In iOS 6您可以直接使用NSAttributedString。例如:
UIColor *_red=[UIColor redColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:72.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeColorAttributeName value:_red range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:-3.0] range:NSMakeRange(0, _stringLength)];发布于 2012-04-25 09:51:00
您不能,UITextView不支持属性字符串。如果您真的只想使用属性化字符串来设置前景颜色(如您的示例中所示),则可以通过设置文本视图的textColor属性来实现相同的目标。
发布于 2015-01-07 11:16:32
您应该通过UITextView.attributedText属性分配属性化字符串。UITextView.text只接受NSString或纯文本。
textView.attributedText = attributedString;见文件:
https://stackoverflow.com/questions/10313120
复制相似问题