To customize a button, to adjust the position of the image (note, not backgroundimage) and title text in the button, just rewrite the two methods that correspond to the button class alone:
First, let's create a Superbutton that inherits from UIButton
superbutton.h// superbutton//// Created by Yang bin on 14/12/25.// Copyright (c) 2014 Yang bin. All rights reserved.//#import <UIKit/UIKit.h> @interface superbutton:uibutton@end
Implementation file
////superbutton.m//superbutton////Created by Yang bin on 14/12/25.//Copyright (c) 2014 Yang Bin . All rights reserved.//#import "SuperButton.h" #import "UtilsFunctions.h" @interface Superbutton () {CGRect boundingrect; } @end @implementation superbutton//Custom Initialization Method-(Instancetype) initWithFrame: (cgrect) frame{self = [Super INITWITHFR Ame:frame]; if (self) {[Self settitle:@] Project Introduction "Forstate:uicontrolstatenormal"; [Self.titlelabel Setfont:[uifont Boldsystemfontofsize:font]; [Self Setbackgroundimage:[uiimage imagenamed:@ "[email protected]"] forstate:uicontrolstatenormal]; [Self Setimage:[uiimage imagenamed:@ "[email protected]"] forstate:uicontrolstatenormal]; Boundingrect=[self.titlelabel.text boundingrectwithsize:cgsizemake (320,font) options: Nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:[uifont SystemFontOfSize:font]} context: NIL]; } return self;}
1. Rewrite the method and change the position of the picture in titlerect. Method after execution-(cgrect) Imagerectforcontentrect: (cgrect) contentrect{ cgfloat imagex=self.frame.size.width/2+ BOUNDINGRECT.SIZE.WIDTH/2; UIScreen *s=[uiscreen Mainscreen]; CGRect rect=s.bounds; CGFloat imagey=contentrect.origin.y+14; CGFloat width=24; CGFloat height=24; Return CGRectMake (ImageX, imagey, width, height); } 2. Change the position of the title text, construct the title rectangle can-(cgrect) Titlerectforcontentrect: (cgrect) contentrect{ cgfloat imagex= ( Self.frame.size.width-boundingrect.size.width)/2; CGFloat imagey=contentrect.origin.y+10; CGFloat width=220; CGFloat height=25; Return CGRectMake (ImageX, imagey, width, height);} @end
We can adjust the position of the picture and text in button buttons simply by rewriting the two methods mentioned above.
Note: The two methods on the 1.IOS7 and iOS8 systems run differently, you can set the flag bit, or customize a button (do not integrate the button)
2. The code is truncated, the key is to rewrite the above two methods, redraw the rectangle, you can
iOS code adjust button picture Image text title position