iOS-Button 图片与文字位置
Posted 小K’s Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于iOS-Button 图片与文字位置的主要内容,如果未能解决你的问题,请参考以下文章
android怎样实现在图片上任意位置添加文字 要代码 急求。。
分别使用docx4j,jacob将文字与图片插入word中书签位置