Can I change the color of auto detected links on UITextView?
You can use the UIAppearance
protocol to apply changes for all text views:
Swift 4.x+:
UITextView.appearance().linkTextAttributes = [ .foregroundColor: UIColor.red ]
Swift 3.x:
UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.red ]
Swift 2.x:
UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.redColor() ]
Objective-C:
[UITextView appearance].linkTextAttributes = @{ NSForegroundColorAttributeName : UIColor.redColor };
Appearance for UITextView
is not documented, but works well.
Keep in mind UIAppearance
notes:
iOS applies appearance changes when a view enters a window, it doesn’t change the appearance of a view that’s already in a window. To change the appearance of a view that’s currently in a window, remove the view from the view hierarchy and then put it back.
In other words:
Calling this code in init()
, or init(coder:)
methods will change UI Object appearance, but calling in loadView()
, or viewDidLoad()
of viewController won't.
If you want to set appearance for whole application, application(_:didFinishLaunchingWithOptions:)
is good place for calling such code.
On iOS 7 you can set the tintColor
of the UITextView
. It affects the link color as well as the cursor line and the selected text color.
iOS 7 also added a new property to UITextView
called linkTextAttributes
which would appear to let you fully control the link style.