ios - Change NSAttributedString text color without having to set title again -
is possible change nsattributedstring color without having set title again?
i've tried:
for case let uibutton in myview.subviews { if i.backgroundcolor != uicolor.greencolor() && i.backgroundcolor != uicolor.graycolor() { i.titlelabel?.textcolor = color // i.settitlecolor(color, forstate: .normal) // i.settitlecolor(color, forstate: .selected) // i.tintcolor = color } } i.titlelabel.textcolor changes color of .normal state not .selected state
you can't set color without setting title can using attributedtitleforstate(.selected) , accessing string property. can create own setattributedtitlecolor method extending uibutton follow:
extension uibutton { func setattributedtitlecolor(color: uicolor, forstate: uicontrolstate) { guard let title = attributedtitleforstate(forstate)?.string else { return } setattributedtitle(nsattributedstring(string: title, attributes: [nsforegroundcolorattributename : color]), forstate: forstate) } }
Comments
Post a Comment