cannyboy
cannyboy

Reputation: 24426

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 instance, red?

Upvotes: 3

Views: 2928

Answers (3)

Leander
Leander

Reputation: 752

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

Upvotes: 1

bhnascar
bhnascar

Reputation: 289

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.

Upvotes: 6

bmeulmeester
bmeulmeester

Reputation: 1117

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.

Upvotes: 3

Related Questions