【问题标题】:Swift -How to change the color of a UIButton's NSMutableAttributedString after it has been setSwift - 如何在设置后更改 UIButton 的 NSMutableAttributedString 的颜色
【发布时间】:2020-01-13 01:16:51
【问题描述】:

我有 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: [])
}

【问题讨论】:

  • 您是否尝试在DispatchQueue.main.async {} 中插入UIView.animate 以顺利过渡?
  • @LewWinczynski 谢谢,你的回答有效。请张贴,以便我接受。我不得不将时间更改为 0.05 以使其看起来更好。
  • 很高兴听到这个消息!好的,会发的。 :)

标签: ios swift uibutton nsmutableattributedstring


【解决方案1】:

您应该在DispatchQueue.main.async {} 内插入UIView.animate 函数以进行平滑过渡。始终建议在主线程上异步制作任何类型的 UI 动画。

【讨论】:

    【解决方案2】:

    另一种实现平滑过渡的方法是使用UIView.performWithoutAnimation

    DispatchQueue.main.async {
        self.layoutIfNeeded()
        UIView.performWithoutAnimation {
    
            self.setTextForFollowersButton()
            self.setTextForFollowingButton()
    
            self.layoutIfNeeded()
        }
    }
    

    【讨论】:

    • @LewWinczynski 这是另一种不用动画的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2021-11-04
    • 2015-06-02
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多