美文网首页
自定义图片和文字位置UIButton

自定义图片和文字位置UIButton

作者: crazy一笑 | 来源:发表于2018-04-18 16:32 被阅读4次
    #import <UIKit/UIKit.h>
    
    typedef enum : NSUInteger {
        ZXMTBButtonTypeHorizontal,//文字在左,图片在右
        ZXMTBButtonTypeVertical,//文字在下,图片在上
    } ZXMTBButtonType;
    
    @interface ZXMTBButton : UIButton
    
    
    -(instancetype)initWithFrame:(CGRect)frame type:(ZXMTBButtonType)type;
    
    @property(nonatomic, assign) CGSize imageSize;//设置图片的大小
    @property(nonatomic, assign) ZXMTBButtonType type;
    
    @end
    
    #import "ZXMTBButton.h"
    
    #define kScreenScale 1.0 / [UIScreen mainScreen].scale
    
    #define kDefaultPadding (10. * kScreenScale)
    
    @interface ZXMTBButton()
    
    //
    //@property(nonatomic, assign) ZXMTBButtonType type;
    
    @property(nonatomic, assign) CGFloat textW;
    
    @end
    
    @implementation ZXMTBButton
    
    -(void)awakeFromNib{
        [super awakeFromNib];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    -(instancetype)init{
        if (self = [super init]) {
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
        }
        return self;
    }
    
    -(instancetype)initWithFrame:(CGRect)frame type:(ZXMTBButtonType)type{
        if (self = [super initWithFrame:frame]) {
            self.type = type;
        }
        return self;
    }
    
    -(instancetype)initWithFrame:(CGRect)frame{
        if (self = [super initWithFrame:frame]) {
            self.titleLabel.textAlignment = NSTextAlignmentCenter;
        }
        return self;
    }
    
    
    
    -(CGRect)imageRectForContentRect:(CGRect)contentRect{
    
        if (self.type == ZXMTBButtonTypeHorizontal) {
            return CGRectMake((self.frame.size.width - self.imageSize.width - self.textW) * 0.5 + self.textW, (self.frame.size.height - self.imageSize.height) * 0.5, self.imageSize.width, self.imageSize.height);
        }else{
            return CGRectMake((self.frame.size.width - self.imageSize.width) * 0.5, 0, self.imageSize.width, self.imageSize.height);
        }
    }
    
    -(CGRect)titleRectForContentRect:(CGRect)contentRect{
        if (self.type == ZXMTBButtonTypeHorizontal) {
            return CGRectMake((self.frame.size.width - self.imageSize.width - self.textW) * 0.5, 0, self.textW, self.frame.size.height);
        }else{
            return CGRectMake(0, self.imageSize.height, self.frame.size.width, self.frame.size.height - self.imageSize.height);
        }
    }
    
    -(void)setImageSize:(CGSize)imageSize{
        
        if (self.frame.size.width > 0) {
            if (imageSize.width > self.frame.size.width) {
                imageSize.width = self.frame.size.width;
            }
            if (imageSize.height > self.frame.size.height) {
                imageSize.height = self.frame.size.height;
            }
        }
        
        _imageSize = imageSize;
        CGFloat w = [self.titleLabel.text boundingRectWithSize:CGSizeMake(self.frame.size.width - self.imageSize.width, self.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.titleLabel.font} context:nil].size.width;
        
        self.textW = w;
    }
    
    -(void)setType:(ZXMTBButtonType)type{
        _type = type;
    }
    
    -(void)setTitle:(NSString *)title forState:(UIControlState)state{
        [super setTitle:title forState:state];
        CGFloat w = [title boundingRectWithSize:CGSizeMake(self.frame.size.width - self.imageSize.width, self.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.titleLabel.font} context:nil].size.width;
       
        self.textW = w;
    
    }
    @end
    

    相关文章

      网友评论

          本文标题:自定义图片和文字位置UIButton

          本文链接:https://www.haomeiwen.com/subject/qwtbkftx.html