2015-02-06 151 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()]