IOS Set the UIButton text display position and font size, color method _ios

Source: Internet
Author: User

Objective

As we all know, the UIButton button is the most commonly used control in iOS development, and as a basic learning tutorial for iOS, beginners need to understand their basic definitions and common settings to be proficient in development.

One, iOS set UIButton font size

Btn.frame = CGRectMake (x, y, width, height);
[BTN Settitle: @ "search" forstate:uicontrolstatenormal];
Set the size of the self on the button
//[btn setfont: [Uifont systemfontsize:14.0]];//This can be used to set the size of the font, but may be removed in a future SDK version
//should be used
Btn.titleLabel.font = [Uifont systemfontofsize:14.0];
[Btn sebackgroundcolor: [Uicolor Bluecolor]];
Finally, add the button to the specified view Superview
[Superview addsubview:btn];

Second, the iOS settings UIButton text display location

Tvnamelabel=[[uibutton Alloc]initwithframe:cgrectmake (5,5,200,40)];

So initialized, the button text default color is white, all if the background is also white, can not see the text,

Btn.contenthorizontalalignment=uicontrolcontenthorizontalalignmentleft//Set text position, now set to left, the default is centered
[btn settitle: @ "title" forstate:uicontrolstatenormal];//Add text

There are times when we want to get UIButton title left aligned and we set

Btn.textLabel.textAlignment = Uitextalignmentleft

is not going to work we need to set

Btn.contenthorizontalalignment = Uicontrolcontenthorizonalignmentleft;

But the problem comes out again, the text will cling to the border, we can set

Btn.contentedgeinsets = Uiedgeinsetsmake (0,10, 0, 0);

Keep the text distance from the border by 10 pixels.

Third, iOS set UIButton font color

Set the color of the font on the UIButton color set on the font UIButton , not by:

[Btn.titlelabel Settextcolor:[uicolorblackcolor]];
Btn.titlelabel.textcolor=[uicolor Redcolor];

But by:

[Btn Settitlecolor:[uicolor Blackcolor]forstate:uicontrolstatenormal];

Summarize

This is the full content of this article, I hope you can help iOS developers, if you have questions you can message exchange.

Related Article

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.