开发者

Change color of UITextView links with a filter?

The detected links on a UITextView are always blue. There's no way to directly change this. But can I overlay some sort of filter which changes blue to, for in开发者_Go百科stance, red?


There's actually a way to do this with private API.

A UITextView has a (single) subview of class UIWebDocumentView, with the selector setUserStyleSheet:.

The following code should change the color of the links to green. At least, it worked for me! :)

for (UIView *subview in textView.subviews) {
    [subview setUserStyleSheet:@"a { color: #00FF00; }"];
}

I know this is really late, but hours of Googling got me no where, so I thought I'd share this.


There's no practical way to do this with a UITextView, what you can try is using a UIWebView changing its 'auto detect links' property and then reformatting the 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];

Or something closely similar.

Hope this helps.

EDIT

Don't forget to implement the UIWebView delegate for this to work.


UIwebDocumentView has the missing selector. Import UIWebDocumentView.h to get it. Unfortunately this is a private API, and yourapp may get rejected by Apple :-(

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜