iOS-Button 图片与文字位置
最近忙的很久没写点啥了,哪怕写一写没什么价值的东西,也能体现出我还活着,不要板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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】