冀教网 - 河北教师网站 - 专注于冀教版课本资源

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 60|回复: 0

iOS-Button 图片与文字位置

[复制链接]

4万

主题

4万

帖子

12万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
124999
发表于 2020-5-23 18:50 | 显示全部楼层 |阅读模式
最近忙的很久没写点啥了,哪怕写一写没什么价值的东西,也能体现出我还活着,不要板shi了,写过的东西写了一遍又一遍,呆zhi;
设置button的图片与文字位置,用系统自带的控制其实也可以;
.h
  1. 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
  1. #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
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|冀教网 - 河北教师网站 - 专注于冀教版课本资源  

GMT+8, 2020-6-4 06:21 , Processed in 0.201759 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表