ios - Swift - 如何在设置后更改 UIButton 的 NSMutableAttributedString 的颜色

标签 ios swift uibutton nsmutableattributedstring

我有 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 函数以实现平滑过渡。始终建议在主线程上异步制作任何类型的 UI 动画。

关于ios - Swift - 如何在设置后更改 UIButton 的 NSMutableAttributedString 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59709576/

相关文章:

ios - 从 cocos2d-x 代码调用原生 ObjC 类

ios - 是否可以同时为 UIPickerView 和 UITableview 设置动画

ios - 苹果 map 和 MapKit 使用相同的 API 吗?

ios - 如果您在单元格上开始手势,则无法滑动以切换 UICollectionView 页面

ios - 使用 Xcode 5 时不显示按钮图像

ios - 如何从 UIImagepicker 中选取动画图像并将其添加到 UIimageview 并上传

ios - 如何在 objective-c 模型类中存储处理程序?

ios - 字符串不可转换为 [AnyObject]

ios - Swift - tableviewcell 中按钮的动态数量

ios - 调用中缺少参数 ‘coder’ 的参数