您的位置:首页 > 产品设计 > UI/UE

UIButton 设置image ,title和setBackgroundImage

2015-10-23 00:01 495 查看
定制一个image 和title 有一定距离的UIButton

// the space between the image and text
CGFloat spacing = 6.0;

// lower the text and push it left so it appears centered
//  below the image
CGSize imageSize = button.imageView.image.size;
button.titleEdgeInsets = UIEdgeInsetsMake(
0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);

// raise the image and push it right so it appears centered
//  above the text
CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: button.titleLabel.font}];
button.imageEdgeInsets = UIEdgeInsetsMake(
- (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);


UIButton的
setBackgroundImage
是设置背景图片,而
setImage
则是设置一个挨着title的一个icon 或者image。

就是如果你设置的是backgroundImage 而不是image的话,那么如果这个图片过小的话这个图片是会被拉伸的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: