iOS Button 上文字图片位置的设置

参考地址:

https://www.cnblogs.com/xujiahui/p/6909317.html

 

 

  1. 1. 添加图片+文字/文字+图片 ,不分前后,图片默认在文字前边 加空格隔开
UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake(30, 200, 300, 50)];
    button.backgroundColor =[UIColor grayColor];
    //图片
    [button setImage:[UIImage imageNamed:@"but"] forState:UIControlStateNormal];
    
    //文字
    [button setTitle:@"  But文字" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font =[UIFont boldSystemFontOfSize:40];
    
    [self.view addSubview:button];

 

  1. 2.设置文字图片 显示整体位置:居中、左、右; 单独设置文字的位置
  1. top : 为正数的时候,是往下偏移,为负数的时候往上偏移; 
  1. left : 为正数的时候往右偏移,为负数的时候往左偏移;
  1.  bottom : 为正数的时候往上偏移,为负数的时候往下偏移;
  1.  right :为正数的时候往左偏移,为负数的时候往右偏移;
UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake(30, 200, 300, 50)];
    button.backgroundColor =[UIColor grayColor];
    //文字
    [button setTitle:@"But文字" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font =[UIFont boldSystemFontOfSize:40];
    //图片
    [button setImage:[UIImage imageNamed:@"but"] forState:UIControlStateNormal];
    
    /修改///
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    /*
     UIControlContentHorizontalAlignmentCente
     UIControlContentHorizontalAlignmentLeft
     UIControlContentHorizontalAlignmentRight
     UIControlContentHorizontalAlignmentFill */
    //文字 可以显示在 button 视图之外,但不接收点击事件响应
    [button setTitleEdgeInsets:UIEdgeInsetsMake(button.imageView.frame.size.height ,button.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度,距离左边框减少imageView的宽度,距离下边框和右边框距离不变
    ///
    
    
       [self.view addSubview:button];

 

UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake(30, 200, 300, 50)];
    button.backgroundColor =[UIColor grayColor];
    //文字
    [button setTitle:@"But文字" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font =[UIFont boldSystemFontOfSize:40];
    //图片
    [button setImage:[UIImage imageNamed:@"xia"] forState:UIControlStateNormal];
    
    /修改///
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    button.imageEdgeInsets = UIEdgeInsetsMake(0,300-50, 0, 0); //上左下右
//button.titleEdgeInsets = UIEdgeInsetsMake(0,300-50, 0, 0); // 设置文字的位置 上左下右
///  
[self.view addSubview:button]; 

 

4/  但是图片尺寸大了  位置会错乱, 如有遇到的请评论指点

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值