Setting NSLinkAttributeName font color

二次信任 提交于 2019-11-30 17:07:46
  1. Use a UITextView
  2. Set the UITextView's linkTextAttributes like so:

    textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    

I actually ended up using TTTAttributedLabel for my label and then was able to do the following which worked perfectly:

NSDictionary *linkAttributes = @{(id)kCTForegroundColorAttributeName: [UIColor whiteColor],
                                 (id)kCTUnderlineStyleAttributeName: [NSNumber numberWithInt:kCTUnderlineStyleSingle]
                                 };
self.lblDescription.linkAttributes = linkAttributes;    

txtLabel.linkAttributes = @{};

This is the right one, call this line before set any other attribute

For swift (for reference for others):

// Color the links
var linkAttributes: NSMutableDictionary = NSMutableDictionary()
linkAttributes.setValue(self.appDelegate.variables.color320, forKey: NSForegroundColorAttributeName)

myTextView.linkTextAttributes = linkAttributes as [NSObject : AnyObject]
gschool

This works for me:

txtLabel.linkAttributes = @{};

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:expression];

{
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:result.range];

[string addAttribute:NSLinkAttributeName value:@"link" range:result.range];
}

Explanation:

This is surprisingly easy, text components that supports link detection have a property called linkTextAttributes.

This property stores the style for the link as of the component can apply it when detects a new link.

Summing up, your style will be applied and then the style stored in this property (in this order), successfully overriding your desired style.

Solution:

Set this property to empty ( linkTextAttributes = [:] ) and take total control of you link Styles.


TIP: This can be used to create touchable elements on your UITextView that behaves like a button. Creating a Really nice effect 🙃

For Swift 3

var aURL = "Http:// Your URL"
var description = "Click Me:"
let attributedString = NSMutableAttributedString(string: description + aURL)

/// Deal with link color
let foundURLRange = attributedString.mutableString.range(of: aURL)
attributedString.addAttribute(NSLinkAttributeName, value: aURL, range: foundURLRange)
textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.yellow]

/// Deal with description color
let foundDescriptionRange = attributedString.mutableString.range(of: description)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: foundDescriptionRange)

textview.attributedText = attributedString

If you use TTTAttributedLabel, Oren's answer is worked.But you need to pay attention to the linkAttributes's warning in annotation.

/**
A dictionary containing the default NSAttributedString attributes to be applied to links detected or manually added to the label text. The default link style is blue and underlined.

@warning You must specify linkAttributes before setting autodecting or manually-adding links for these attributes to be applied.
*/

@property (nonatomic, strong) NSDictionary *linkAttributes;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!