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

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:@"[email protected]"] 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的

--

时间: 2024-08-15 02:54:35

IOS UIButton(UIEdgeInsets)/设置图片和文字上下垂直居中对齐的相关文章

UIButton同时设置图片和文字 及 相关的title设置

1>同时设置图片和文字 UIButton *_backButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_backButton setFrame:CGRectMake(12, 8, 64, 28)]; [_backButton setTitle:@"返回" forState:UIControlStateNormal];        //设置button在没有选中的时候显示的字体 _backButton.titleLa

使UIButton中的图片和文字实现左对齐

UIButton setImage 和 setTitle之后,默认的 image和title 对齐方式是居中, 由于 title 长度不固定,所以如果要几个这样有image有title的按钮纵向排列对齐,无论你怎么调整 imageEdgeInsets和titleEdgeInsets,都有可能导致前面图片对得不整齐,所以,干脆来个向左对齐!!简化处理!!记住了,这么设置:btn.contentHorizontalAlignment =UIControlContentHorizontalAlignm

html 中 图片和文字一行 垂直居中对齐

效果:    代码:<div><img src='img/point_icon.png' width='35px' height='35px' style='float: left;'/><span style='height:35px;line-height:35px;'>[标记]</span></div> 注意:img的heigth 必须和sapn的height相同,span的height,line-height必须相同,img必须设置flo

UIButton设置图片和文字

在开发的过程中经常会遇到需要在button中放置图片和文字,比如将图片放置在button左边,文字放置在右边.因为UIButton也是继承自UIView,因此可以像其它的view一样添加subView, //创建button UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // 创建imageview UIImage *image = [UIImage imageNamed:@"yourImage.png

iOS UIButton同时设置title和image属性

在iOS开发中,使用UIButton设置title和image,达到tabBarItem的效果,即title在下,image在上: 目前,我发现有两种比较好的方法: 方法一,使用UIEdgeInsets UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setFrame:CGRectMake(100, 100, 60, 60)]; [button setBackgroundColor:[UIColor

ios代码调整button图片image文字title位置

自定义一个button,要调整 button中的image(注意,不是backgroundImage) 和  title 文字的位置,只需要重写  Button类独对应的两个方法即可: 首先,我们来创建一个 SuperButton继承自 UIButton // // SuperButton.h // SuperButton // // Created by 杨斌 on 14/12/25. // Copyright (c) 2014年 杨斌. All rights reserved. // #im

div css 图片和文字上下居中对齐

想要图片和文字水平居中对齐很容易,可今天做这个树的时候,遇到了要将图片上下居中.这下就不知道怎么搞. 拿手册看也看不出所以然来,不同的浏览器对DIV的支持不一个,目前最好的浏览器是Firefox. 这里涉及到两个属性,一个是line-height,另一个是vertical-align.在IE里面,只需要line-height就可以实现这个效果,不过要想在firefox和opea里面也实现这样的效果,必须用到vertical-align,而且这是属性一定要定义在image上面,而不是定义在div上

怎样让一行中的 文字 input输入框 按钮button它们在同一个高度上? 它们中的文字 是 垂直居中对齐

很多时候, 文字和 input和button它们的 顶端是在同一条先上, 但是并不是 垂直居中对齐, 这个 就不好看. 其时有很多理论, 讲的也很复杂, 说什么文本 的top, middle ,baseline , bottom 四根线, 等等 那些我们都不管, 可以这样简单的理解: 把文本/input输入框/button等 都看作是放在 各自一个盒子中的东西, 默认的时候: 当它们在同一行中时, 这些盒子的顶端是对齐的, 即顶端是在同一根线上, 顶端是对齐的, 是盒子不一定是文本顶端对齐; 而

iOS UIButton 常用设置

类方法buttonWithType UIButton *btn= [UIButton buttonWithType:UIButtonTypeRoundedRect]; 风格有如下 typedef enum { UIButtonTypeCustom = 0, //自定义,无风格 UIButtonTypeRoundedRect, //白色圆角矩形,类似偏好设置表格单元或者地址簿卡片 UIButtonTypeDetailDisclosure, //蓝色的披露按钮,可放在任何文字旁 UIButtonTy