参考:http://blog.csdn.net/qijianli/article/details/8152726
不过有个问题,就是我使用时不能改变文字的颜色,后来修改了一下方法,如下:
定义一个UIButton+Manager文件,在.h
#import <UIKit/UIKit.h> @interface UIButton (UIButtonImageWithLable) - (void) setImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType; @end
#import "UIButton+Manager.h" @implementation UIButton (UIButtonImageWithLable) - (void) setImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType { //UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) CGSize titleSize = [title sizeWithFont:[UIFont systemFontOfSize:12.0]]; [self.imageView setContentMode:UIViewContentModeCenter]; [self setImageEdgeInsets:UIEdgeInsetsMake(-8.0, 0.0, 0.0, -titleSize.width)]; [self setImage:image forState:stateType]; [self.titleLabel setContentMode:UIViewContentModeCenter]; [self.titleLabel setBackgroundColor:[UIColor clearColor]]; [self.titleLabel setFont:[UIFont systemFontOfSize:12.0]]; [self setTitleColor:[UIColor blueColor] forState:stateType];//用这个方法替换下面的方法,文字的颜色就可以显示了。 //[self.titleLabel setTextColor:[UIColor blueColor]]; if (stateType == UIControlStateSelected) { [self setTitleColor:[UIColor blueColor] forState:UIControlStateSelected]; }else{ [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; } [self setTitleEdgeInsets:UIEdgeInsetsMake(35.0, -image.size.width, 0.0, 0.0)]; [self setTitle:title forState:stateType]; } @end
UIButton上同时显示图片和文字的方法,布布扣,bubuko.com
原文地址:http://blog.csdn.net/quanqinayng/article/details/27553879