self.descriptionLbl.text = actionReqdText
let labelString = self.descriptionLbl.text as! NSString
let contactRange1 = labelString.range(of: actionString1)
let contactRange2 = labelString.range(of: actionString2)
self.descriptionLbl.addLink(to: URL(string: link1), with: contactRange1)
self.descriptionLbl.addLink(to: URL(string: link2), with: contactRange2)
let attributedText = NSMutableAttributedString(attributedString: descriptionLbl.attributedText!)
if UIScreen.main.bounds.size.height < 900 {
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 13)!], range: contactRange1)
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 13)!], range: contactRange2)
} else {
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 18)!], range: contactRange1)
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 18)!], range: contactRange2)
}
attributedText.addAttributes([NSAttributedString.Key(rawValue: kCTForegroundColorAttributeName as String as String): UIColor(red: 0.06, green: 0.49, blue: 0.25, alpha: 1.0)], range: contactRange1)
attributedText.addAttributes([NSAttributedString.Key(rawValue: kCTForegroundColorAttributeName as String as String): UIColor(red: 0.06, green: 0.49, blue: 0.25, alpha: 1.0)], range: contactRange2)
attributedText.addAttributes([NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleNone.rawValue], range: contactRange1)
attributedText.addAttributes([NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleNone.rawValue], range: contactRange2)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.1
attributedText.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedText.length))
descriptionLbl.attributedText = attributedText
I've been trying this code to get a text in green color and with no underline style. It works the first time I launch the view controller. The next time I get to this screen, it is in native blue and underlined. Any help will be appreciated.
self.descriptionLbl.text = actionReqdText
let labelString = self.descriptionLbl.text as! NSString
let contactRange1 = labelString.range(of: actionString1)
let contactRange2 = labelString.range(of: actionString2)
let attributedText = NSMutableAttributedString(attributedString: descriptionLbl.attributedText!)
var linkAttributes: [AnyHashable : Any] = [:]
linkAttributes[NSAttributedString.Key.underlineStyle] = NSUnderlineStyle.styleNone.rawValue
if UIScreen.main.bounds.size.height < 900 {
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 13)!], range: contactRange1)
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 13)!], range: contactRange2)
} else {
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 18)!], range: contactRange1)
attributedText.addAttributes([NSAttributedStringKey.font : UIFont(name: HELVETICA_LIGHT, size: 18)!], range: contactRange2)
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 1.1
linkAttributes[NSAttributedString.Key.foregroundColor] = UIColor(red: 0.06, green: 0.49, blue: 0.25, alpha: 1.0)
linkAttributes[NSAttributedString.Key.paragraphStyle] = paragraphStyle
self.descriptionLbl.attributedText = attributedText
self.descriptionLbl.linkAttributes = linkAttributes
self.descriptionLbl.addLink(to: URL(string: link1), with: contactRange1)
self.descriptionLbl.addLink(to: URL(string: link2), with: contactRange2)
Implemented the above code by replacing the addAttributes with link attributes and moving the addLinks after adding the attributes. This resolved the issue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.