UIButton 设置image ,title和setBackgroundImage

来源:互联网 发布:linux 禁止删除文件夹 编辑:程序博客网 时间:2024/06/05 20:37

定制一个image 和title 有一定距离的UIButton

// the space between the image and textCGFloat spacing = 6.0;// lower the text and push it left so it appears centered //  below the imageCGSize 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 textCGSize 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的话,那么如果这个图片过小的话这个图片是会被拉伸的。

0 0
原创粉丝点击