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

UIButton上同时显示图片和文字的方法

2014-05-29 17:47 393 查看
参考: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


在.m中

#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


有点困惑,有兴趣的朋友可以来解惑下。我看好你汗清。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: