码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-Button 图片与文字位置

时间:2020-05-23 19:59:38      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:sel   float   elf   max   break   nta   活着   height   port   

最近忙的很久没写点啥了,哪怕写一写没什么价值的东西,也能体现出我还活着,不要板shi了,写过的东西写了一遍又一遍,呆zhi;

设置button的图片与文字位置,用系统自带的控制其实也可以;

.h

typedef NS_ENUM(NSUInteger, KLXKLayoutButtonStyle) {
    ///图左字右
    KLXKLayoutButtonStyleLeftImageRightTitle,
    ///图右字左
    KLXKLayoutButtonStyleLeftTitleRightImage,
    ///图上字下
    KLXKLayoutButtonStyleUpImageDownTitle,
    ///图下字上
    KLXKLayoutButtonStyleUpTitleDownImage
};

@interface KLXKLayoutButton : UIButton
/// 布局方式
@property (nonatomic, assign) KLXKLayoutButtonStyle layoutStyle;
/// 图片和文字的间距,默认值 5
@property (nonatomic, assign) CGFloat imageTitleSpacing;
/// 图片 size 指定
@property (nonatomic, assign) CGSize imageSize;

+ (KLXKLayoutButton *)buttonWithLayoutButtonStyle:(KLXKLayoutButtonStyle)style
                                            title:(NSString *)title
                                            image:(UIImage *)image;

.m

#import "KLXKLayoutButton.h"

@implementation KLXKLayoutButton
+ (KLXKLayoutButton *)buttonWithLayoutButtonStyle:(KLXKLayoutButtonStyle)style
                                            title:(NSString *)title
                                            image:(UIImage *)image{
    KLXKLayoutButton *button = [KLXKLayoutButton buttonWithType:UIButtonTypeCustom];
    button.adjustsImageWhenHighlighted = NO;
    [button setImage:image forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor kl_colorBlack2] forState:UIControlStateNormal];
    button.backgroundColor = [UIColor whiteColor];
    button.layoutStyle = style;
    return button;
}


/// 初始化可用
/// @param frame KLXKLayoutButton
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.imageTitleSpacing = 5;
        self.imageSize = CGSizeZero;
    }
    return self;
}

/// 初始化可用
- (instancetype)init{
    self = [super init];
    if (self) {
        self.imageTitleSpacing = 5;
        self.imageSize = CGSizeZero;
    }
    return self;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (CGSizeEqualToSize(CGSizeZero, self.imageSize)) {
        [self.imageView sizeToFit];
    }
    else {
        self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,
                                          self.imageView.frame.origin.y,
                                          self.imageSize.width,
                                          self.imageSize.height);
    }
    [self.titleLabel sizeToFit];
    
    switch (self.layoutStyle) {
        case KLXKLayoutButtonStyleLeftImageRightTitle:
            [self layoutHorizontalWithLeftView:self.imageView rightView:self.titleLabel];
            break;
        case KLXKLayoutButtonStyleLeftTitleRightImage:
            [self layoutHorizontalWithLeftView:self.titleLabel rightView:self.imageView];
            break;
        case KLXKLayoutButtonStyleUpImageDownTitle:
            [self layoutVerticalWithUpView:self.imageView downView:self.titleLabel];
            break;
        case KLXKLayoutButtonStyleUpTitleDownImage:
            [self layoutVerticalWithUpView:self.titleLabel downView:self.imageView];
            break;
        default:
            break;
    }
}

- (void)layoutHorizontalWithLeftView:(UIView *)leftView rightView:(UIView *)rightView {
    CGRect leftViewFrame = leftView.frame;
    CGRect rightViewFrame = rightView.frame;
    
    CGFloat totalWidth = CGRectGetWidth(leftViewFrame) + self.imageTitleSpacing + CGRectGetWidth(rightViewFrame);
    
    leftViewFrame.origin.x = (CGRectGetWidth(self.frame) - totalWidth) / 2.0;
    leftViewFrame.origin.y = (CGRectGetHeight(self.frame) - CGRectGetHeight(leftViewFrame)) / 2.0;
    leftView.frame = leftViewFrame;
    
    rightViewFrame.origin.x = CGRectGetMaxX(leftViewFrame) + self.imageTitleSpacing;
    rightViewFrame.origin.y = (CGRectGetHeight(self.frame) - CGRectGetHeight(rightViewFrame)) / 2.0;
    rightView.frame = rightViewFrame;
}

- (void)layoutVerticalWithUpView:(UIView *)upView downView:(UIView *)downView {
    CGRect upViewFrame = upView.frame;
    CGRect downViewFrame = downView.frame;
    
    CGFloat totalHeight = CGRectGetHeight(upViewFrame) + self.imageTitleSpacing + CGRectGetHeight(downViewFrame);
    
    upViewFrame.origin.y = (CGRectGetHeight(self.frame) - totalHeight) / 2.0;
    upViewFrame.origin.x = (CGRectGetWidth(self.frame) - CGRectGetWidth(upViewFrame)) / 2.0;
    upView.frame = upViewFrame;
    
    downViewFrame.origin.y = CGRectGetMaxY(upViewFrame) + self.imageTitleSpacing;
    downViewFrame.origin.x = (CGRectGetWidth(self.frame) - CGRectGetWidth(downViewFrame)) / 2.0;
    downView.frame = downViewFrame;
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state {
    [super setImage:image forState:state];
    [self setNeedsLayout];
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state {
    [super setTitle:title forState:state];
    [self setNeedsLayout];
}

- (void)setImageTitleSpacing:(CGFloat)imageTitleSpacing{
    _imageTitleSpacing = imageTitleSpacing;
    [self setNeedsLayout];
}
- (void)setImageSize:(CGSize)imageSize {
    _imageSize = imageSize;
    [self setNeedsLayout];
}

@end

 

iOS-Button 图片与文字位置

标签:sel   float   elf   max   break   nta   活着   height   port   

原文地址:https://www.cnblogs.com/wangkejia/p/12943851.html

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