UIButton internal text and picture layout is our daily code, the indispensable part of the button default left image to the right of the text, that to achieve the left side of the text, the right picture, how can we solve it, the above picture, the text below what to do?
Actually very simple, today summed up, currently mainly in two ways, one is the rewrite button, the other is through the Settitleedgeinsets and Setimageedgeinsets method to solve
is the graphic layout of the button by default
Left text, right picture
First introduce the rewrite button, create a new button to inherit UIButton,
- (void) layoutsubviews{ [super layoutsubviews]; CGRect imageRect = self.imageView.frame; imagerect.size = cgsizemake (30, 30); imageRect.origin.x = (self.frame.size.width - 30) ; imageRect.origin.y = (self.frame.size.height - 30)/ 2.0f; cgrect titlerect = self.titlelabel.frame; titleRect.origin.x = (self.frame.size.width - imagerect.size.width- TitleRect.size.width); titlerect.origin.y = (Self.frame.size.heighT - titlerect.size.height)/2.0f; self.imageView.frame = imageRect; self.titleLabel.frame = Titlerect;}
The effect is as follows:
Above picture, below text
Also use the Override button method
- (void) layoutsubviews{ [super layoutsubviews]; cgrect imagerect = self.imageview.frame; imagerect.size = cgsizemake (30, 30); imagerect.origin.x = (self.frame.size.width - 30)  * 0.5;    IMAGERECT.ORIGIN.Y = self.frame.size.height * 0.5 - 40; CGRect titlerect = self.titlelabel.frame; titlerect.origin.x = ( Self.frame.size.width - titlerect.size.width) * 0.5; titlerect.origin.y = self.frame.size.height * 0.5 ; Self.imageview.frame = imagerect; self.titlelabel.frame = titlerect ;}
The effect is as follows:
! [screenshot 2016-05-30 10.23.11.png] (http://upload-images.jianshu.io/upload_images/616981-34430e2f6f66b344.png?imageMogr2/auto-orient/strip% 7cimageview2/2/w/1240
Another approach is to solve the problem by Settitleedgeinsets and Setimageedgeinsets methods.
The biggest benefit of this approach is not to rewrite the UIButton, directly in the new UIButton to change the value of the above two properties can achieve the results we want
Left text right picture
The code is as follows:
uibutton *btn1 = [uibutton buttonwithtype:uibuttontypecustom]; Btn1.frame = cgrectmake (50, 100, 80, 40); [btn1 setimage: [uiimage imagenamed:@ "Icon_shouye"] forstate:uicontrolstatenormal]; [btn1 settitle:@ "Home" forstate:uicontrolstatenormal]; [btn1 settitlecolor:[ Uicolor blackcolor] forstate:uicontrolstatenormal]; btn1.backgroundcolor = [UIColor redColor]; UIButton *btn = [UIButton Buttonwithtype:uibuttontypecustom]; btn.frame = cgrectmake (50, 50, 80, 40); [btn setimage:[uiimage imagenamed:@ "Icon_shouye"] forState : uicontrolstatenormal]; [btn settitle:@ "Home" forState:UIControlStateNormal]; [btn settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal]; btn.backgroundcolor = [uicolor redcolor]; //left Right Btn.imageedgeinsets = uiedgeinsetsmake (0, btn.frame.size.width - btn.imageview.frame.origin.x - btn.imageview.frame.size.width, 0, 0); btn.titleedgeinsets = uiedgeinsetsmake (0, -(btn.frame.size.width - btn.imageview.frame.size.width ), 0, 0); [self.view addsubview:btn1 ]; [self.view addsubview:btn];
Completely upside-down effect
Above picture text below
The code is as follows:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = cgrectmake (50, 50, 80, 60); [btn setimage:[uiimage imagenamed:@ "Icon_shouye"] forstate:uicontrolstatenormal]; [btn settitle:@ "The first thing" forstate:uicontrolstatenormal]; [btn settitlecolor : [Uicolor blackcolor] forstate:uicontrolstatenormal]; btn.backgroundcolor = [uicolor redcolor]; btn.imageedgeinsets = uiedgeinsetsmake ( - (BTN.FRAME.SIZE.HEIGHT - BTN.TITLELABEL.FRAME.SIZE.HEIGHT- BTN.TITLELABEL.FRAME.ORIGIN.Y ), (btn.frame.size.width -btn.titlelabel.frame.size.width)/2.0f -btn.imageview.frame.size.width, 0, 0); btn.titleedgeinsets = uiedgeinsetsmake ( Btn.frame.size.height-btn.imageview.frame.size.height-btn.imageview.frame.origin.y, -btn.imageview.frame.size.width, 0, 0); [self.view addSubview:btn];
:
Here are some explanations for settitleedgeinsets and setimageedgeinsets:
UIButton has two controls Titlelabel and ImageView, which can be used to display a text and a picture, where the picture differs from the background image. After you set the title and image to UIButton, they will have the picture on the left and the text displayed on the right side of the picture. Both of them as a whole depend on the button of the contenthorizontalalignment left right or centered display.
Display format distinction:
1. When Button.width < Image.width, only the compressed image is displayed, the picture is compressed according to the Fillxy way.
2. When Button.width > Image.width, and Button.width < (image.width+text.width), the picture is displayed properly and the text is compressed.
3. When Button.width > (image.width+text.width), the two side-by-side default center display, you can change the alignment by the button's property contenthorizontalalignment.
To change the display position of the two child controls, you can do this by Settitleedgeinsets and Setimageedgeinsets respectively. Setting the offset for Titlelabel and ImageView is for his current position, not for the distance from the button border.
Typedefns_enum (Nsinteger, uicontrolcontenthorizontalalignment) {
Uicontrolcontenthorizontalalignmentcenter =0,//Center
Uicontrolcontenthorizontalalignmentleft =1,//Left
Uicontrolcontenthorizontalalignmentright =2,//Right
Uicontrolcontenthorizontalalignmentfill =3,//
Want to change the display position of the two child control, can be achieved by settitleedgeinsets and Setimageedgeinsets respectively. It is important to note that the offset is set for Titlelabel and ImageView, which works for its current position, not for its distance from the button border. Feel settings do not set Uicontrolcontenthorizontalalignmentcenter Center is not affected, this online also found some relevant information, feel did not say the point, I do not fully understand here, These are the results of the constant attempts to set settitleedgeinsets and setimageedgeinsets these parameters. Now this is my understanding, the code to achieve the final answer, hope can help everyone.
Article transferred from MIGI000 's Pinterest
The layout of Ios-uibutton text and pictures