I feel like I'm missing something easy but I can't seem to find out how to do this:
I set the attribute to a link like so:
[myAttrString addAttribute:NSLinkAttributeName value:linkURL range:selectedRange];
That works but the link is blue and I can't seem to change the color. This sets everything except the link to white:
[myAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:selectedRange];
Is there another color attribute name that I can't seem to find that is specific to links?
- Use a
UITextView
Set the
UITextView
'slinkTextAttributes
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]
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 defaultNSAttributedString
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;
来源:https://stackoverflow.com/questions/25457131/setting-nslinkattributename-font-color