I am trying to put a string
on a button that has both a small image and text so I am using NSMutableAttributedString
and NSAttributedString
. From what I can tell the foregroundColor
key in the dictionary should update the color of text but it doesn't seem to be working. fgColor is UIColor.white
and bgColor is UIColor.black
but I have trouble printing that out to verify. Can anyone offer advice? Code is:
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!)
If you not planning having different colors in text, as alternative you can use UIButton's methods to color things out:
thumbUpButton?.titleLabel?.textColor = fgColor
thumbUpButton?.backgroundColor = bgColor