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

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -