UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self action:@selector(xxxx:) forControlEvents:UIControlEventTouchUpInside];
btn.frame = CGRectMake(20, 20, 96, 96); //圖的大小在90x90左右
[btn setTitle:@"圖示" forState:UIControlStateNormal];
UIImage *btnImg = [UIImage imageNamed:@"icon.png"];
[btn setImage:btnImg forState:UIControlStateNormal];
//title從中間往下65px,靠左為btnImg的寬度,負數為放大
[btn setTitleEdgeInsets:UIEdgeInsetsMake(65, -btnImg.size.width, 0, 0)];
//image從中間往上20px,靠右為titleLabel的寬度
[btn setImageEdgeInsets:UIEdgeInsetsMake(-20, 0, 0, -btn.titleLabel.bounds.size.width)];
[btn setTitleEdgeInsets:UIEdgeInsetsMake(65, -btnImg.size.width, 0, 0)];
//image從中間往上20px,靠右為titleLabel的寬度
[btn setImageEdgeInsets:UIEdgeInsetsMake(-20, 0, 0, -btn.titleLabel.bounds.size.width)];
要特別注意的是,設定title及image的順序,若反過來,記得在寬高的設定上也要反過來,兩者是互相影响的。
參考網址:
http://blog.sina.com.cn/s/blog_6de1899201013bnv.html
沒有留言:
張貼留言