I am using a TTTAttributedLabel in my project. I have managed to change the default color and underlining for any link that I create by modifying the link attributes.
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;
However, I have noticed that when I tap on the link, it turns red momentarily as any other link does when tapped. I need to change this color. Any clues to how that might be done?
Swift 2 Solution:
Specifically, need to set activeLinkAttributes
, see below example:
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With