UITextView富文本?

时间:2009-09-17 05:29:31

标签: iphone

我想知道UITextView是否可以显示富文本?我想在我的只读文本上进行简单的格式化(比如文本不同部分的不同对齐方式)。如果没有,我应该使用什么视图?

3 个答案:

答案 0 :(得分:2)

通常,最好的方法是使用UIWebView并加载本地内容,您可以在其中插入标记以及您需要的任何其他内容。本地加载应该很快,您可以将其样式化为任何uitextview

另一种选择是使用UILabel子类来获取Three20开源代码。你想要TTStyledTextLabel

答案 1 :(得分:2)

以下是尝试执行此操作的任何人的代码段 - 使用uiwebview处理样式文本 - 让本地js文件正常工作可能会很棘手。进入构建阶段 - >编译源 - >并在html内容中添加要在本地访问的js文件。您还可以在webview上将字符串评估为来自目标c的javascript。

NSString *someHtmlContent = [NSString stringWithFormat:@"<html><head><style type=\"text/css\">body {padding: 0px; margin: 0px; }</style><script src=\"yourLocalJsFile.js\"></script></head><body></body></html>", dynamicText]; 

NSLog(@"HTML with code: \n%@", someHtmlContent);

NSString *resourcePath = [[[[NSBundle mainBundle] resourcePath]
                               stringByReplacingOccurrencesOfString:@"/" withString:@"//"]
                              stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

[MyWebView loadHTMLString:someHtmlContent baseURL:[NSURL URLWithString:
                                            [NSString stringWithFormat:@"file:/%@//", resourcePath]]];

[self.MyWebView setDelegate:self];

设置代理让您决定如何处理链接 - 您希望它们在移动版Safari或Webview中打开吗

答案 2 :(得分:1)

您可以查看github上的EGOTextView项目。它将自己描述为

  

“完全取代UITextView,增加对支持   富文本编辑。“

相关问题