码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-06-03 01:36:45      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   http   

参考: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

有点困惑,有兴趣的朋友可以来解惑下。我看好你汗清。

UIButton上同时显示图片和文字的方法,布布扣,bubuko.com

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

标签:c   class   blog   code   a   http   

原文地址:http://blog.csdn.net/quanqinayng/article/details/27553879

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!