欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐

程序员文章站 2022-07-04 09:23:41
uiedgeinsets typedef struct uiedgeinsets { cgfloat top, left, bottom, right; /...

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;

在uibutton中有三个对edgeinsets的设置: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 imageedgeinsets;        // default is uiedgeinsetszero

uiedgeinsetsmake

里面的四个参数表示距离上边界、左边界、下边界、右边界的距离,默认都为零,title/image在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的类型
  button.frame = cgrectmake(100, 100,90, 90);//button的frame
  button.backgroundcolor = [uicolor cyancolor];//button的背景颜色
//  [button setbackgroundimage:[uiimage imagenamed:@"man_64.png"] forstate:uicontrolstatenormal];
//  在uibutton中有三个对edgeinsets的设置:contentedgeinsets、titleedgeinsets、imageedgeinsets
  [button setimage:[uiimage imagenamed:@"iconhome@2x.png"] forstate:uicontrolstatenormal];//给button添加image
  button.imageedgeinsets = uiedgeinsetsmake(5,13,21,button.titlelabel.bounds.size.width);//设置image在button上的位置(上top,左left,下bottom,右right)这里可以写负值,对上写-5,那么image就象上移动5个像素
  [button settitle:@"首页" forstate:uicontrolstatenormal];//设置button的title
  button.titlelabel.font = [uifont systemfontofsize:16];//title字体大小
  button.titlelabel.textalignment = nstextalignmentcenter;//设置title的字体居中
  [button settitlecolor:[uicolor whitecolor] forstate:uicontrolstatenormal];//设置title在一般情况下为白色字体
  [button settitlecolor:[uicolor graycolor] forstate:uicontrolstatehighlighted];//设置title在button被选中情况下为灰色字体
   button.titleedgeinsets = uiedgeinsetsmake(71, -button.titlelabel.bounds.size.width-50, 0, 0);//设置title在button上的位置(上top,左left,下bottom,右right)
//  [button setcontentedgeinsets:uiedgeinsetsmake(70, 0, 0, 0)];//
//  button.contenthorizontalalignment = uicontrolcontenthorizontalalignmentcenter;//设置button的内容横向居中。。设置content是title和image一起变化
  [button addtarget:self action:@selector(tap) forcontrolevents:uicontroleventtouchupinside];
  [self.view addsubview:button];
//button相应的事件
-(void)tap {
  nslog(@"tap a button");
  uialertview *alertview = [[uialertview alloc] initwithtitle:@"hello" message:@"willingseal" delegate:self cancelbuttontitle:@"ok" otherbuttontitles: nil];
  [alertview show];
}

效果图:

我这里使用的image是64*64的

iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐

iOS - UIButton(UIEdgeInsets)/设置button上的文字和图片上下垂直居中对齐

--

那就一个button,这个button的背景图片中既有图片又有文字就可以了,找个美工帮忙弄一下应该很逼真的。。。!!

以上就是本文给大家分享的ios - uibutton(uiedgeinsets)/设置button上的文字和图片上下垂直居中对齐,希望大家喜欢。