[英]TTTAttributedLabel, change of color works only the first time controller launches in Swift
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
我一直在嘗試使用此代碼來獲取綠色且沒有下划線樣式的文本。 它在我第一次啟動視圖控制器時工作。 下次我進入這個屏幕時,它是原生藍色並帶有下划線。 任何幫助將不勝感激。
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)
通過用鏈接屬性替換 addAttributes 並在添加屬性后移動 addLinks 來實現上述代碼。 這解決了問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.