Ios-uibutton (uiedgeinsets)/Set the text and picture on the button to align vertically and horizontally between the top and bottom _ios

Source: Internet
Author: User
Tags button type

Uiedgeinsets

typedef struct UIEDGEINSETS {
  cgfloat top, left, bottom, right;//Specify amount to inset (positive) for each of the Edges. Values can be negative to ' outset '
} uiedgeinsets;

There are three sets of 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 Imagee Dgeinsets;        Default is Uiedgeinsetszero

Uiedgeinsetsmake

The four parameters inside the

Indicate the distance from the top, left, bottom, and right edges, and the default is zero, title/image in the center of the button

Uikit_static_inline uiedgeinsets Uiedgeinsetsmake (cgfloat top, cgfloat left, CGFloat bottom, cgfloat right) {UIEdgeIns
  ETS 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); Frame Button.backgroundcolor = [Uicolor Cyancolor];//button background color//button Setbackgroundimage:[uiimage image
named:@ "Man_64.png"] forstate:uicontrolstatenormal]; There are three settings for edgeinsets in UIButton: Contentedgeinsets, Titleedgeinsets, imageedgeinsets [button setimage:[uiimage imagenamed:@ "IconHome@2x.png"] forstate:uicontrolstatenormal];//add image to button button.imageedgeinsets = Uiedgeinsetsmake (5,13,21,button.titlelabel.bounds.size.width);/Set the position of the image on the button (top, left, bottom bottom,
  right) here can write negative, write to 5, then image is like moving 5 pixels [button settitle:@ "Home" forstate:uicontrolstatenormal];//set the button's title Button.titleLabel.font = [Uifont systemFontofsize:16];//title Font Size button.titleLabel.textAlignment = nstextalignmentcenter;//font centered on Title [button Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatenormal];//set title to the General white Font [button settitlecolor:[ Uicolor Graycolor] forstate:uicontrolstatehighlighted];//setting title is the gray font when the button is selected Button.titleedgeinsets = Uiedgeinsetsmake (-button.titlelabel.bounds.size.width-50, 0, 0);/Set the position of title on the button (top, left, bottom bottom, right)//[button setcontentedgeinsets:uiedgeinsetsmake (0, 0, 0)];////button.contenthorizontalalignment = Uicontr olcontenthorizontalalignmentcenter;//sets the contents of the button horizontally centered ... Set content is changed with title and Image [button addtarget:self action: @selector (TAP) forControlEvents:
  UIControlEventTouchUpInside];
[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];}

 

Effect Chart:

The image I use here is 64*64.

--

That is a button, the background picture of this button are both pictures and text can be, find a graphic to help you should be very realistic ...!!

The above is the article to share the Ios-uibutton (uiedgeinsets)/Set button on the text and the picture up and down vertically center align, I hope you like.

Related Article

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.