Swift-如何在设置了UIButton的NSMutableAttributedString之后更改其颜色

问题描述 投票:0回答:1

我有2个UIButtons,并且它们的titlescolors最初是使用NSMutableAttributedString设置的。文本为2 lines of text

我有一个自定义的UISegmentedControl,当切换段时,我只需要更改按钮的颜色。现在我的方式颜色会发生变化,但是发生这种情况时会出现难看的眨眼,因为实际的文字又被设置了。我只需要平滑过渡就可以使用颜色

var selectedSegmentIndex = 0 {
    didSet {

        layoutIfNeeded()
        UIView.animate(withDuration: 0.1) {
            self.setTextForFollowersButton()
            self.setTextForFollowingButton()
            self.layoutIfNeeded()
        }
    }
}

如何仅更改按钮的NSMutableAttributedString的颜色?

按钮代码:

var numOfFollowers = 0 {
    didSet {
        setTextForFollowersButton()
    }
}
var numOfFollowing = 0 {
    didSet {
        setTextForFollowingButton()
    }
}

lazy var followersButton: UIButton = {
    let button = UIButton.init(type: .system)
    // ...
    return button
}()

lazy var followingButton: UIButton = {
    let button = UIButton.init(type: .system)
    //...
    return button
}()

func setTextForFollowersButton() {

    let button = self.followersButton
    let buttonText = "Following\n\(String(numOfFollowing))" as NSString
    // other code for 2 lines of text

    var color = UIColor.blue

    if selectedSegmentIndex == 0 {
        color = UIColor.blue
    } else {
        color = UIColor.gray
    }

    let attrString1 = NSMutableAttributedString(string: substring1,
                                                attributes: [NSMutableAttributedString.Key.font:
                                                    UIFont.boldSystemFont(ofSize: 16),
                                                             NSMutableAttributedString.Key.foregroundColor: color])

    let attrString2 = NSMutableAttributedString(string: substring2,
                                                attributes: [NSMutableAttributedString.Key.font:
                                                    UIFont.boldSystemFont(ofSize: 14),
                                                             NSMutableAttributedString.Key.foregroundColor: color])

    attrString1.append(attrString2)
    button.setAttributedTitle(attrString1, for: [])
}

func setTextForFollowingButton() {

    let button = self.followingButton

    let buttonText = "Following\n\(String(numOfFollowing))" as NSString
    // other code for 2 lines of text

    var color = UIColor.blue

    if selectedSegmentIndex == 0 {
        color = UIColor.gray
    } else {
        color = UIColor.blue
    }

    let attrString1 = NSMutableAttributedString(string: substring1,
                                                attributes: [NSMutableAttributedString.Key.font:
                                                    UIFont.boldSystemFont(ofSize: 16),
                                                             NSMutableAttributedString.Key.foregroundColor: color])

    let attrString2 = NSMutableAttributedString(string: substring2,
                                                attributes: [NSMutableAttributedString.Key.font:
                                                    UIFont.boldSystemFont(ofSize: 14),
                                                             NSMutableAttributedString.Key.foregroundColor: color])

    attrString1.append(attrString2)
    button.setAttributedTitle(attrString1, for: [])
}
ios swift uibutton nsmutableattributedstring
1个回答
0
投票

您应该在UIView.animate的内部插入DispatchQueue.main.async {}函数以进行平滑过渡。

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