IOS总结_实现UIButton的图文混排(二)

    非常久没有写博客了,之前写过一篇关于UIButton图文混排的,可是有点复杂,今天来一个比較简单地。相信大家回用得着


    UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(60, 90, 100, 40)];

    //加文字

    [button setTitle:@"描写叙述文字" forState:UIControlStateNormal];

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    //加图片

    [button setImage:[UIImage imageNamed:@"pulldown"] forState:UIControlStateNormal];

    //加边框

    button.layer.borderColor=[UIColor redColor].CGColor;

    button.layer.borderWidth=0.5;


   这就是不改变位置的情况下混排的效果,依据大家不同的须要大家肯定会改变其位置

    

    

    //改变图文位置 左右并排

    button.titleEdgeInsets=UIEdgeInsetsMake(0, -button.imageView.frame.size.width, 0, button.imageView.frame.size.width);

    button.imageEdgeInsets=UIEdgeInsetsMake(0, button.titleLabel.frame.size.width+5, 0, 0);

    


说明:EdgeInsets的位置是相对改变的。详细大家能够更具代码去理解


    [self.view addSubview:button];


原文地址:https://www.cnblogs.com/llguanli/p/6961582.html