How to make the button (UIbutton) text left-justified on iOS

Source: Internet
Author: User

  1. //button.titleLabel.textAlignment = Nstextalignmentleft; This sentence is invalid.   
  2. Button. contenthorizontalalignment = uicontrolcontenthorizontalalignmentleft;
  3. button. Titleedgeinsets= Uiedgeinsetsmake (0, 10, 0, 0);

The UI interface has been confused with how to make the button's text left-aligned display, re-encountered today. Decide to solve the problem completely

First we'll think of the following code

Button.titleLabel.textAlignment = Nstextalignmentleft; This line of code is not effective. This is simply to align the text in the label to the left, but

Does not change the alignment of the label in the button.


So. We will first use

button. Contenthorizontalalignment = uicontrolcontenthorizontalalignmentleft; this line of code that puts the contents of the button (the control)

Alignment changes to horizontal left-aligned, but these will be tightly on the left, not good-looking,

So we can also change the properties:

button. Titleedgeinsets = uiedgeinsetsmake(0, 8, 0, 0);

This line of code allows the button's content (control) to be 8 pixels from the left. Adjust according to your needs. It can also be negative (--8) so it looks much better. I hope it's helpful to everyone.

2015.7.31 changes

The following is a button and text up and down alignment display

int origin_y;    origin_y=64;    for (int i=0; i<4; i++) {        UIButton *btn = [[UIButton alloc]initwithframe:cgrectmake (+, origin_y,50, +)];        Btn.backgroundcolor = [Uicolor greencolor];        [Btn setimage:[uiimage imagenamed:@ "BBC"] forstate:uicontrolstatenormal];        [BTN settitle:@ "test" forstate:uicontrolstatenormal];        [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];        Btn.titleLabel.backgroundColor = [Uicolor bluecolor];        Btn.contentverticalalignment = Uicontrolcontentverticalalignmenttop;        Btn.imageedgeinsets = Uiedgeinsetsmake (5,10,21,0);        Btn.titleedgeinsets = Uiedgeinsetsmake (30,-(btn.frame.size.width-10*2), 0, 0);        [Btn addtarget:self Action: @selector (click) forcontrolevents:uicontroleventtouchupinside];        [Self.view addsubview:btn];        origin_y+= (BTN.FRAME.SIZE.HEIGHT+10);    }


If you have any questions, please do not hesitate to enlighten!

How to make the button (UIbutton) text left-justified on iOS

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.