IOS Button Settings AttributeString Adaptive dimension experience in different states

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.