UIButton(左邊圖片右邊文字)

weixin_34006468發表於2015-03-02
262538-47814a9bfa38aa18.jpeg
normal

262538-db0ed257d4674bb2.jpeg
highlighted
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 50);
button.backgroundColor = [UIColor clearColor];
//設定button正常狀態下的圖片
[button setImage:[UIImage imageNamed:@"_star_normal.png"] forState:UIControlStateNormal];
//設定button高亮狀態下的圖片
[button setImage:[UIImage imageNamed:@"_star_highlighted.png"] forState:UIControlStateHighlighted];
//設定button正常狀態下的背景圖
[button setBackgroundImage:[UIImage imageNamed:@"_normal.png"] forState:UIControlStateNormal];
//設定button高亮狀態下的背景圖
[button setBackgroundImage:[UIImage imageNamed:@"_highlighted.png"] forState:UIControlStateHighlighted];
//button圖片的偏移量,距上左下右分別(10, 10, 10, 60)畫素點
button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 60);
[button setTitle:@"南瓜瓜" forState:UIControlStateNormal];
//button標題的偏移量,這個偏移量是相對於圖片的
button.titleEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
//設定button正常狀態下的標題顏色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//設定button高亮狀態下的標題顏色
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[self.view addSubview:button];

相關文章