TTTAttributedLabel,颜色的更改仅在控制器首次在Swift中启动时起作用

问题描述 投票:0回答:1
 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

我一直在尝试这段代码来获取绿色且没有下划线样式的文本。它在我第一次启动视图控制器时起作用。下次我进入该屏幕时,它是本机蓝色并带有下划线。任何帮助将不胜感激。

ios swift xcode nsmutableattributedstring nsattributedstringkey
1个回答
0
投票
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来实现上述代码。这解决了问题

© www.soinside.com 2019 - 2024. All rights reserved.