【发布时间】:2011-11-07 13:28:32
【问题描述】:
在 UITextView 上检测到的链接总是蓝色的。没有办法直接改变这一点。但是我可以覆盖某种将蓝色变为红色的过滤器吗?
【问题讨论】:
-
好主意 =) 其他文本呢?
标签: iphone objective-c ios filter uitextview
在 UITextView 上检测到的链接总是蓝色的。没有办法直接改变这一点。但是我可以覆盖某种将蓝色变为红色的过滤器吗?
【问题讨论】:
标签: iphone objective-c ios filter uitextview
没有实用的方法可以使用 UITextView 执行此操作,您可以尝试使用 UIWebView 更改其“自动检测链接”属性,然后重新格式化 HTML。
UIWebView *webView = [[UIWebView alloc] init];
webText.delegate = self;
[webView setDataDetectorType:UIDataDetectorTypeLink];
NSString * htmlString = [NSString stringWithFormat:@"<html><head><script> document.ontouchmove = function(event) { if (document.body.scrollHeight == document.body.clientHeight) event.preventDefault(); } </script><style type='text/css'>* { margin:0; padding:0; } p { color:black; font-family:Helvetica; font-size:14px; } a { color:#000000; text-decoration:underline; }</style></head><body><p>%@</p></body></html>", [update objectForKey:@"text"]];
[webText loadHTMLString:htmlString baseURL:nil];
或类似的东西。
希望这会有所帮助。
编辑
不要忘记实现 UIWebView 委托以使其正常工作。
【讨论】:
实际上有一种方法可以做到这一点使用私有 API。
UITextView 具有类 UIWebDocumentView 的(单个)子视图,选择器为 setUserStyleSheet:。
以下代码应将链接的颜色更改为绿色。至少,它对我有用! :)
for (UIView *subview in textView.subviews) {
[subview setUserStyleSheet:@"a { color: #00FF00; }"];
}
我知道这真的很晚了,但是几个小时的谷歌搜索让我无处可去,所以我想我会分享这个。
【讨论】:
@interface UIView (Stylesheet) - (void) setUserStyleSheet: (NSString *) str; @end
for (UIView *subview in textview.subviews) { if ([subview respondsToSelector:@selector(setUserStyleSheet:)]) { [subview performSelector:@selector(setUserStyleSheet:) withObject:@"a { color: #00FF00; }"]; } }
UIwebDocumentView 缺少选择器。 导入 UIWebDocumentView.h 得到它。 不幸的是,这是一个私有 API,您的应用可能会被 Apple 拒绝:-(
【讨论】: