更改UITextView中超链接的颜色

32

我正在使用 UITextView ,想要改变在此组件中使用的超链接颜色。例如,如果 UITextView 显示www.gmail.com,则以蓝色显示。我想改变这种颜色。

2个回答

137

哎呀!苹果在iOS7中发布了一个合适的解决方案!如这个答案所解释的那样,您可以使用UITextViewlinkTextAttributes属性。 白色下划线链接将看起来像这样:

yourTextView.linkTextAttributes = @{
    NSForegroundColorAttributeName: [UIColor whiteColor], 
    NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle]
};

或者您可以更改tintColor,因为UITextView继承自UIView并使用tintColor属性来着色链接 - 请参见:

或者您可以更改tintColor,因为UITextView继承自UIView并使用tintColor属性来着色链接 - 请参见:

yourTextView.tintColor = [UIColor whiteColor];

现在你的链接可以看起来很棒!


通过设置 yourTextView.linkTextAttributes 更改链接颜色会使光标看起来像悬停在文本上而不是链接上(手形光标)。有没有办法解决这个问题? - eli-bd
我找到了解决光标问题的方法:yourTextView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.redColor(), NSUnderlineStyleAttributeName : NSUnderlineStyle.StyleSingle.rawValue, NSCursorAttributeName: NSCursor.pointingHand()] - eli-bd
第二个对我有效。 - Abhishek Thapliyal

-8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接