Uiedgeinsets
typedef struct UIEDGEINSETS { cgfloat top, left, bottom, right; Specify amount to inset (positive) for each of the edges. Values can is negative to ' outset '} uiedgeinsets;
There are three settings for edgeinsets in UIButton: Contentedgeinsets, Titleedgeinsets, imageedgeinsets
@property (nonatomic) uiedgeinsets contentedgeinsets ui_appearance_selector;//default is Uiedgeinsetszero@property (nonatomic) uiedgeinsets titleedgeinsets; Default is Uiedgeinsetszero@property (nonatomic) BOOL reversestitleshadowwhenhighlighted;//default is NO. If YES, shadow reverses to shift between Engrave and Emboss Appearance@property (nonatomic) uiedgeinsets imageedgeinse TS; Default is Uiedgeinsetszero
Uiedgeinsetsmake
The four parameters inside indicate the distance from the upper boundary, the left boundary, the lower boundary, the right boundary, the default is zero, the title/image is in the center of the button
Uikit_static_inline uiedgeinsets Uiedgeinsetsmake (cgfloat top, cgfloat left, CGFloat bottom, cgfloat right) { Uiedgeinsets insets = {top, left, bottom, right}; return insets;}
Self.view.backgroundColor = [Uicolor blackcolor]; UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];//button type button.frame = CGRectMake (100, 100, 90, 90); Button's Frame Button.backgroundcolor = [Uicolor Cyancolor];//button background color//[button Setbackgroundimage:[uiimage I magenamed:@ "Man_64.png"] forstate:uicontrolstatenormal]; There are three settings for edgeinsets in UIButton: Contentedgeinsets, Titleedgeinsets, imageedgeinsets [button setimage:[uiimage imagenamed:@ "[email protected]"] forstate:uicontrolstatenormal];//add image to button button.imageedgeinsets = Uiedgeinsetsmake (5,13,21,button.titlelabel.bounds.size.width);//sets the position of the image on the button (top, left, bottom bottom, right) here can write negative values, write to 5, then the image moves like 5 pixels [button settitle:@ "Home" forstate:uicontrolstatenormal];//set the button's title but Ton.titleLabel.font = [Uifont systemfontofsize:16];//title font Size button.titleLabel.textAlignment = nstextalignmentcenter;//set the font for title to center [button Settitlecolor:[uicolor Whitecolor] Forstate:uicontrolstatenormal];//set title in the general case of white Font [button Settitlecolor:[uicolor Graycolor] forstate: uicontrolstatehighlighted];//set title in the case of a button is selected as a gray font button.titleedgeinsets = Uiedgeinsetsmake (71,- button.titlelabel.bounds.size.width-50, 0, 0);//Set the title position on the button (top, left, bottom bottom, right)//[button setcontent Edgeinsets:uiedgeinsetsmake (0, 0, 0)];////button.contenthorizontalalignment = Uicontrolcontenthorizontalalignme ntcenter;//the contents of the button are centered horizontally ... Setting content is changed with title and Image [button addtarget:self action: @selector (TAP) forcontrolevents:uicontroleventtouchupin Side]; [Self.view Addsubview:button];
button corresponding event-(void) Tap { NSLog (@ "tap a button"); Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "Hello" message:@ "Willingseal" delegate:self cancelbuttontitle:@ "OK" otherbuttontitles:nil]; [Alertview show];}
:
The image I'm using here is 64*64.
--
IOS UIButton (uiedgeinsets)/Set picture and text vertically center align up and down