- //button.titleLabel.textAlignment = Nstextalignmentleft; This sentence is invalid.
- Button. contenthorizontalalignment = uicontrolcontenthorizontalalignmentleft;
- 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