2015-02-06 399 views
32

我有以下代码,但我的链接始终为蓝色。我如何拼出它们的颜色?更改NSMutableAttributedString中链接的颜色

[_string addAttribute:NSLinkAttributeName value:tag range:NSMakeRange(position, length)]; 
[_string addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(12.0)] range:NSMakeRange(position, length)]; 
[_string addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(position, length)]; 

_string是一个NSMutableAttributedString,位置和长度正常工作。

+1

我用这个:http://stackoverflow.com/questions/25457131/setting-nslinkattributename-font-color – cdub 2015-02-06 08:27:16

+0

如果你觉得这个问题由用户进行充分的回答,请选择这是接受的答案。 – 2017-09-01 17:55:13

回答

0
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@"here" attributes:@{ @"myCustomTag" : @(YES), NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) }]; 

Objective-C的

这将使下划线白色点击文本。为您的代码选择必要的属性并使用它。

要让字符串可点击的链接在它下一步:

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Click " attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15]}]; 
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@"here" attributes:@{ @"myCustomTag" : @(YES), NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:@"SourceSansPro-Semibold" size:15], NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle) }]; 
[string appendAttributedString:attributedString]; 

因此,你会得到字符串“点击这里”和“这里”将是一个链接。您可以为每个字符串设置不同的样式。

68

斯威夫特

更新了斯威夫特3

使用linkTextAttributesUITextView

textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.green] 

而且在上下文中:

let attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_") 
let linkRange = (attributedString.string as NSString).range(of: "@marcelofabri_") 
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: linkRange) 
let linkAttributes: [String : Any] = [ 
    NSForegroundColorAttributeName: UIColor.green, 
    NSUnderlineColorAttributeName: UIColor.lightGray, 
    NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue] 

// textView is a UITextView 
textView.linkTextAttributes = linkAttributes 
textView.attributedText = attributedString 
textView.delegate = self 

斯威夫特4:

let linkAttributes: [String : Any] = [ 
    NSAttributedStringKey.foregroundColor.rawValue: UIColor.green, 
    NSAttributedStringKey.underlineColor.rawValue: UIColor.lightGray, 
    NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue] 

Objective-C的

使用linkTextAttributesUITextView

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

来源:this answer

而且从this post

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"]; 
[attributedString addAttribute:NSLinkAttributeName 
         value:@"username://marcelofabri_" 
         range:[[attributedString string] rangeOfString:@"@marcelofabri_"]]; 


NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor], 
           NSUnderlineColorAttributeName: [UIColor lightGrayColor], 
           NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}; 

// assume that textView is a UITextView previously created (either by code or Interface Builder) 
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links 
textView.attributedText = attributedString; 
textView.delegate = self; 
+0

我已经能够更改链接颜色,但是如何更改在按下链接时显示的颜色? – 2017-10-13 18:46:54

21

链接颜色是标签/ textView的色调颜色。所以,你可以通过改变视图的色彩来改变它。但是,如果您想在同一个视图中使用不同的链接颜色,这将不起作用。

+8

从iOS 9开始,这对于'UILabel'不起作用。 – Pol 2016-07-25 20:37:25

3

斯威夫特

let str = "By using this app you agree to our Terms and Conditions and Privacy Policy" 
let attributedString = NSMutableAttributedString(string: str) 
var foundRange = attributedString.mutableString.rangeOfString("Terms and Conditions") 

attributedString.addAttribute(NSLinkAttributeName, value: termsAndConditionsURL, range: foundRange) 
foundRange = attributedString.mutableString.rangeOfString("Privacy Policy") 
attributedString.addAttribute(NSLinkAttributeName, value: privacyURL, range: foundRange) 
policyAndTermsTextView.attributedText = attributedString 
policyAndTermsTextView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.blueColor()]