Describe the following scenario: button displays different title styles in different states
[uifont Span class= "S3" >systemfontofsize:18.0f Weight:uifontweightregular]  color is [uicolor blackcolor
Select under Font is[uifont systemfontofsize:18.0f weight:uifontweightmedium] color is [uicolor Graycolor]
Start with this setting:
nsattributedstring *normal_title = [[Nsattributedstring alloc] initwithstring:@"Reviews 20320323"Attributes:@{nsfontattributename:[uifont systemfontofsize:18.0fWeight:uifontweightregular],nsforegroundcolorattributename:unselect_color}]; Nsattributedstring*select_title = [[Nsattributedstring alloc] initwithstring:@"Reviews 20320323"Attributes:@{nsfontattributename:[uifont systemfontofsize:18.0fWeight:uifontweightmedium],nsforegroundcolorattributename:select_color}]; UIButton*button1 =[UIButton Buttonwithtype:uibuttontypecustom]; Button1.backgroundcolor=[Uicolor Redcolor]; Button1.frame= CGRectMake (10.0,400.0,100.0,20.0f); [button1 Setattributedtitle:normal_title Forstate:uicontrolstatenormal]; [button1 Setattributedtitle:select_title forstate:uicontrolstateselected]; [Button1 SizeToFit]; [Button1 addtarget:self Action: @selector (Showselect:) forcontrolevents:uicontroleventtouchupinside];-(Ibaction) Showselect: (ID) sender{if([Sender Iskindofclass:[uibuttonclass]]) {UIButton*button = (UIButton *) sender; Button.selected= !button.selected; [Button SizeToFit];}
The size of the button cannot be changed after the setting is found
Google down find need to set button highlighted and uicontrolstateselected | uicontrolstatehighlighted Status See also: HTTP://WWW.JIANSHU.COM/P/57B2C41448BF http://rickytan.cn/blog/2015/07/06/uibutton-state/
Modify the code as follows:
Uicolor *select_color =[Uicolor Blackcolor]; Uicolor*unselect_color =[Uicolor Graycolor]; Nsattributedstring*normal_title = [[Nsattributedstring alloc] initwithstring:@"Reviews 20320323"Attributes:@{nsfontattributename:[uifont systemfontofsize:18.0fWeight:uifontweightregular],nsforegroundcolorattributename:unselect_color}]; Nsattributedstring*select_title = [[Nsattributedstring alloc] initwithstring:@"Reviews 20320323"Attributes:@{nsfontattributename:[uifont systemfontofsize:18.0fWeight:uifontweightmedium],nsforegroundcolorattributename:select_color}]; UIButton*button1 =[UIButton Buttonwithtype:uibuttontypecustom]; Button1.backgroundcolor=[Uicolor Redcolor]; Button1.frame= CGRectMake (10.0,400.0,100.0,20.0f); [button1 Setattributedtitle:normal_title Forstate:uicontrolstatenormal]; [button1 Setattributedtitle:select_title forstate:uicontrolstateselected]; [button1 Setattributedtitle:normal_title forstate:uicontrolstatehighlighted]; [Button1 setattributedtitle:select_title forstate:uicontrolstateselected | uicontrolstatehighlighted]; [Button1 SizeToFit]; [Button1 addtarget:self Action: @selector (Showselect:) forcontrolevents:uicontroleventtouchupinside];-(Ibaction) Showselect: (ID) sender{if([Sender Iskindofclass:[uibuttonclass]]) {UIButton*button = (UIButton *) sender; Button.selected= !button.selected; [Button SizeToFit];}
IOS Button Settings AttributeString Adaptive dimension experience in different states