【发布时间】:2019-08-12 13:19:09
【问题描述】:
我正在尝试将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!)
【问题讨论】:
标签: ios swift nsattributedstring nsmutablestring