我的应用程序中有以下代码
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 时,出现以下错误:
Incompatible objective c types struct NSMutableAttributedString expected struct nsstring
所以请告诉我,如何在 UITextView 中显示 NSMutableAttributedString。
最佳答案
您可以尝试使用一些库来做到这一点。正如 omz 所写,不幸的是 UITextView 不支持 NSAttributedString
。
也许这个可以帮助你:https://github.com/enormego/EGOTextView
他们对这个库的评价如下:
UITextView replacement with additional support for NSAttributedString.
更新:根据您在 omz 答案评论中的澄清,您可以查看此处: Underline text inside uitextview
更新2:在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)];
关于objective-c - 显示 NSMutableAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10313120/