2015-02-06 12 views
32

Mam następujący kod, ale moje łącza są zawsze niebieskie. Jak mogę zmienić ich kolor?Zmiana koloru łącza w pliku 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 jest NSMutableAttributedString oraz położenie i długość pracy w porządku.

+1

użyłem to: http://stackoverflow.com/questions/25457131/setting-nslinkattributename-font-color – cdub

+0

Jeśli czujesz, że pytanie zostało odpowiednio odbierane przez użytkownika, wybierz to jako zaakceptowana odpowiedź. –

Odpowiedz

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

Objective-C

To sprawi podkreślony białą klikalny tekst. Wybierz niezbędne atrybuty dla swojego kodu i użyj go.

Aby mieć ciąg z klikalny link to dalej:

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

W rezultacie otrzymasz ciąg „kliknij tutaj” i „tu” będzie link. Możesz ustawić różne style dla każdego ciągu.

68

Swift

Aktualizacja dla Swift 3

użytkowania linkTextAttributes z UITextView

textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.green] 

A w kontekście:

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 

Swift 4:

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

Objective-C

Zastosowanie linkTextAttributes z UITextView

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

Źródło: this answer

iz 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

Udało mi się zmienić kolor łącza, ale w jaki sposób mogę zmienić kolor, który pojawia się po naciśnięciu linku? –

21

Kolor łącza to kolor tinty etykiety/textView. Możesz go zmienić, zmieniając kolor tinty widoku. To jednak nie zadziała, jeśli chcesz mieć różne kolory linków w tym samym widoku.

+8

To nie działa dla 'UILabel' jak iOS 9. – Pol

3

Swift

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()]