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


      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];

      2.设置文字图片 显示整体位置:居中、左、右; 单独设置文字的位置
      top : 为正数的时候,是往下偏移,为负数的时候往上偏移; 
      left : 为正数的时候往右偏移,为负数的时候往左偏移;
       bottom : 为正数的时候往上偏移,为负数的时候往下偏移;
       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];
        
        

    3.效果:

    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/  但是图片尺寸大了  位置会错乱, 如有遇到的请评论指点

     

  • 相关阅读:
    SPOJ913 Query on a tree II
    SPOJ375 Query on a tree
    HDU4348 To the moon
    Bzoj2753 [SCOI2012]滑雪与时间胶囊
    HDU4612 Warm up
    Uva11374 Airport Express
    Uva1624 Knots
    DevExpress的GridControl的使用以及怎样添加列和绑定数据源
    C#中使用Path、Directory、Split、Substring实现对文件路径和文件名的常用操作实例
    ZedGraph的曲线的LineItem对象的Tag属性存储信息进而在鼠标悬浮时进行显示
  • 原文地址:https://www.cnblogs.com/xujiahui/p/6909317.html
Copyright © 2020-2023  润新知