Change color of UITextView links with a filter?

前端 未结 3 1409
你的背包
你的背包 2021-01-06 14:19

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?<

相关标签:
3条回答
  • 2021-01-06 15:05

    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.

    0 讨论(0)
  • 2021-01-06 15:09

    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 讨论(0)
  • 2021-01-06 15:10

    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.

    0 讨论(0)
提交回复
热议问题