2015-02-09 14 views
14

Używam TTTAttributedLabel w moim projekcie. Udało mi się zmienić domyślny kolor i podkreślenia dla każdego tworzonego przez siebie łącza, modyfikując atrybuty linków.Kolor stuknięcia łącza dla TTTAttributedLabel

NSArray *pKeys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName, 
         (id)kCTUnderlineStyleAttributeName 
        , nil]; 

NSArray *pObjects = [[NSArray alloc] initWithObjects:pAlertColor,[NSNumber numberWithInt: 
                      kCTUnderlineStyleNone], nil]; 

NSDictionary *pLinkAttributes = [[NSDictionary alloc] initWithObjects:pObjects 
                    forKeys:pKeys]; 

self.alertMessage.linkAttributes = pLinkAttributes; 
self.alertMessage.activeLinkAttributes = pLinkAttributes; 

Zauważyłem jednak, że po stuknięciu w link zmienia kolor na czerwony, tak jak robi to inne łącze po dotknięciu. Muszę zmienić ten kolor. Jakieś wskazówki, jak to zrobić?

Odpowiedz

14

Będziesz wyglądać w TTTAttributedLabel documentation, specjalnie na activeLinkAttributes

activeLinkAttributes

@property (nonatomic, silny) NSDictionary * activeLinkAttributes Dyskusja

Słownik zawierający atrybuty NSAttributedString, który ma być stosowany do linków, gdy są one w stanie aktywnym. Jeśli zero lub pusty NSDictionary, aktywne łącza nie będą stylizowane. Domyślny styl aktywnego linku jest czerwony i podkreślony.

zadeklarowane w

TTTAttributedLabel.h

+0

Dzięki chłopaki .. I ustaw te same atrybuty łącza dla activeLinkAttributes i zadziałało. (Zasadniczo nie chciałem, aby kolor łącza zmieniał się po dotknięciu). Widziałem ActiveLinkAttributes, ale nie wiedziałem, że to mi pomoże. – StudentX

1

Można użyć atrybutu "activeLinkAttributes"

NSMutableDictionary* attributes = [NSMutableDictionary dictionaryWithDictionary:self.attributedLabel.activeLinkAttributes]; 
[attributes setObject:(__bridge id)[UIColor blueColor].CGColor forKey:(NSString*)kCTForegroundColorAttributeName]; 
self.attributedLabel.activeLinkAttributes = attributes; 
5

Powinieneś zrobić coś takiego

NSMutableDictionary *mutableActiveLinkAttributes = [NSMutableDictionary dictionary]; 
    [mutableActiveLinkAttributes setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName]; 
    [mutableActiveLinkAttributes setObject:[UIColor greenColor] forKey:(NSString *)kCTForegroundColorAttributeName]; 
    label.activeLinkAttributes = [NSDictionary dictionaryWithDictionary:mutableActiveLinkAttributes]; 
18

Swift 2 Rozwiązanie:

enter image description here

W szczególności, należy ustawić activeLinkAttributes, patrz poniżej przykład:

private func subscriptionNoticeWithDelegate(delegate:TTTAttributedLabelDelegate) -> TTTAttributedLabel { 
    let subscriptionNotice:String = "To turn on all notifications, subscribe to our monthly " + 
    "service ($0.99/month). If you have already subscribed, please restore your purchase." 

    let paragraphStyle = NSMutableParagraphStyle() 
    paragraphStyle.lineHeightMultiple = 1.2 

    let subscriptionNoticeAttributedString = NSAttributedString(string:subscriptionNotice, attributes: [ 
    NSFontAttributeName: UIFont(name:"HelveticaNeue-Light", size:15)!, 
    NSParagraphStyleAttributeName: paragraphStyle, 
    NSForegroundColorAttributeName: UIColor.grayColor().CGColor, 
    ]) 
    let subscriptionNoticeLinkAttributes = [ 
    NSForegroundColorAttributeName: UIColor.grayColor(), 
    NSUnderlineStyleAttributeName: NSNumber(bool:true), 
    ] 
    let subscriptionNoticeActiveLinkAttributes = [ 
    NSForegroundColorAttributeName: UIColor.grayColor().colorWithAlphaComponent(0.80), 
    NSUnderlineStyleAttributeName: NSNumber(bool:true), 
    ] 

    let subscriptionNoticeLabel:TTTAttributedLabel = TTTAttributedLabel(frame:CGRectZero) 
    subscriptionNoticeLabel.delegate = delegate 
    subscriptionNoticeLabel.numberOfLines = 0 
    subscriptionNoticeLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping 
    subscriptionNoticeLabel.textInsets = UIEdgeInsets(top:10, left:15, bottom:0, right:15) 
    subscriptionNoticeLabel.setText(subscriptionNoticeAttributedString) 
    subscriptionNoticeLabel.linkAttributes = subscriptionNoticeLinkAttributes 
    subscriptionNoticeLabel.activeLinkAttributes = subscriptionNoticeActiveLinkAttributes 

    let subscribeLinkRange = (subscriptionNotice as NSString).rangeOfString("subscribe") 
    let subscribeURL = NSURL(string:kSubscriptionNoticeSubscribeURL)! 
    subscriptionNoticeLabel.addLinkToURL(subscribeURL, withRange:subscribeLinkRange) 

    let restoreLinkRange = (subscriptionNotice as NSString).rangeOfString("restore") 
    let restoreURL = NSURL(string:kSubscriptionNoticeRestoreURL)! 
    subscriptionNoticeLabel.addLinkToURL(restoreURL, withRange:restoreLinkRange) 

    return subscriptionNoticeLabel 
} 
+1

dzięki za rozwiązanie Swift! – StudentX

2

Dla Swift 4:

let activeLinkAttributes = NSMutableDictionary(dictionary: attributedLabel.activeLinkAttributes) 
activeLinkAttributes[NSAttributedStringKey.foregroundColor] = UIColor.blue 
attributedLabel.activeLinkAttributes = activeLinkAttributes as NSDictionary as! [AnyHashable: Any] 

Dla Swift 3:

let activeLinkAttributes = NSMutableDictionary(dictionary: attributedLabel.activeLinkAttributes) 
activeLinkAttributes[NSForegroundColorAttributeName] = UIColor.blue 
attributedLabel.activeLinkAttributes = activeLinkAttributes as NSDictionary as! [AnyHashable: Any] 
Powiązane problemy