Setting NSLinkAttributeName font color

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

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?

回答1:

  1. Use a UITextView
  2. Set the UITextView's linkTextAttributes like so:

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


回答2:

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;    


回答3:

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]


回答4:

txtLabel.linkAttributes = @{};

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



回答5:

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]; }


回答6:

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 ?



回答7:

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



回答8:

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;



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