objective-c - 显示 NSMutableAttributedString

标签 objective-c ios ios4

我的应用程序中有以下代码

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/

相关文章:

ios - 如何确保我一次只有 1 个异步 NSURLConnection?

objective-c - iPad 上的 UIActionSheet 未显示取消按钮

ios4 - 在 EKEvent(iPhone 日历)中存储自定义值

objective-c - 获取例程中出现死锁

ios - 下标和上标字符串在行之间添加额外的空间

ios - Secure Transport TLS 应该如何与 Swift 中的 BSD 套接字一起使用?

iOS 应用内购买收据验证 iOS 7

cocoa-touch - QLPreviewController 与 UIWebView -- 优点/缺点

objective-c - 替换对象索引 :withObject does not work

ios - 滚动tableview计数标签值是否改变?