UIButton NSAttributedString 不会更新字体颜色

UIButton NSAttributedString won't update font color

我正在尝试将 string 放在同时具有小图像和文本的按钮上,因此我使用了 NSMutableAttributedStringNSAttributedString。据我所知,字典中的 foregroundColor 键应该更新文本的颜色,但它似乎不起作用。 fgColor 是 UIColor.white 并且 bgColor 是 UIColor.black 但我无法打印出来进行验证。谁能提供建议?代码是:

let myFont = UIFont(name: "HelveticaNeue-Light", size: 8)
let dict1:[NSAttributedString.Key:Any] = [
        NSAttributedString.Key.font:myFont!,
        NSAttributedString.Key.foregroundColor : fgColor,
        NSAttributedString.Key.strokeColor : fgColor,
        NSAttributedString.Key.backgroundColor : bgColor
]
let fullString = NSMutableAttributedString()
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named:"ThumbsUpGreen")
let imageString = NSAttributedString(attachment: imageAttachment)
fullString.append(imageString)
fullString.append(NSAttributedString(string: " "+String(upCount), attributes: dict1))
thumbUpButton?.setAttributedTitle(fullString, for: .normal)
contentView.addSubview(thumbUpButton!)

如果您不打算在文本中使用不同的颜色,您可以使用 UIButton 的方法来为内容着色: thumbUpButton?.titleLabel?.textColor = fgColor thumbUpButton?.backgroundColor = bgColor