UIButton in IOS settitle and Titlelabel use errors

Source: Internet
Author: User

The titl methods that are set up in UIButton include the following:

-(void) Settitle: (NSString *) title forstate: (uicontrolstate) state;         -(void) Setattributedtitle: (nsattributedstring *) title forstate: (uicontrolstate) state               @property ( Nonatomic,readonly, retain) NSString *currenttitle;             @property (nonatomic,readonly, retain) UILabel     *titlelabel;

In the definition of UIButton, often use Titlelabel.text set the value of UIButton, but run out does not show anything, does not work AH!!! , how is this going to happen? Is it the API bug??

1. In fact, the normal use of UIButton when the title is to correspond to the ControlState button, because UIButton inherit Uicontrol, in the setting is worth the object state, so it will be used

Settitle: (NSString *) title forstate: (uicontrolstate) state setting title;

2.setAttributedTitle is the method after iOS6, it is very simple to use, no special description. Examples are as follows:

[UIButton setattributedtitle:[[nsattributedstring alloc]initwithstring:@ "3333333"] Forstate:uicontrolstatenormal];

3. The corresponding Currenttitle is the title value of the/normal/highlighted/selected/disabled state, and the attribute is readonly;

4. As for the Titlelabel is set when why not show, more magical. After checking the official documents, I found out that the real reason was again: (Below is the result of I print the Titlelabel object using UIButton)

PO Uibutton.titlelabel
0x7575800; frame = (0000'11111111'0x7572980>>

See here, do you understand?

The default UIButton titlelable is not set frame, and Hidden=yes, as long as you set these 2 values can be displayed normally,

No matter how you produce UIButton:

UIButton *uibtn = [[UIButton alloc]initwithframe:cgrectmake (0)];  
*UIBTN = [UIButton buttonwithtype:uibuttontypecustom];

[Uibtn setframe:cgrectmake (0, 100, 100, 30)];

are the same;

In short, above is I encountered 2 times use Titlelabel not show summary, hope to be helpful later. It is recommended to use the first way to set the title without having so much trouble ...

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.