很久沒有寫博客了,之前寫過一篇關於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];