UIButton NSAttributedString не будет обновлять цвет шрифта - PullRequest
0 голосов
/ 21 марта 2019

Я пытаюсь нанести string на кнопку с маленьким изображением и текстом, поэтому я использую NSMutableAttributedString и NSAttributedString.Из того, что я могу сказать, ключ 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!)

1 Ответ

1 голос
/ 21 марта 2019

Если вы не планируете иметь разные цвета в тексте, в качестве альтернативы вы можете использовать методы UIButton, чтобы раскрасить вещи: thumbUpButton?.titleLabel?.textColor = fgColor thumbUpButton?.backgroundColor = bgColor

...