Я пытаюсь нанести 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!)