UIButton的图片和文字位置

  • button的image和lable可以在layoutSubviews自定义它们的位置,直接上代码:

#import "ViewController.h"
#import "YMTestButton.h"
 
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    /* 按钮 */
    YMTestButton *button = [[YMTestButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    button.backgroundColor = [UIColor greenColor];
    [button setImage:[UIImage imageNamed:@"tabbar_huipin_selected@3x.png"] forState:UIControlStateNormal];
    [button setTitle:@"我是测试" forState:UIControlStateNormal];
    [self.view addSubview:button];
}
@end
复制代码
  • ps:这是继承UIButton,下面我写了三种情况,代码都附上


第一种:


#import "YMTestButton.h"
#import "UIView+YMExtension.h"
 
@implementation YMTestButton
 
- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return self;
}
 
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.imageView.ym_y = self.ym_height * 0.5 - self.imageView.ym_height;
    self.imageView.ym_x = self.ym_width * 0.5 - self.imageView.ym_width * 0.5;
    
    self.titleLabel.ym_y = self.imageView.ym_bottom + 10;
    self.titleLabel.ym_x = 0;
    self.titleLabel.ym_width = self.ym_width;
}
 
@end复制代码


第二种:


#import "YMTestButton.h"
#import "UIView+YMExtension.h"
 
@implementation YMTestButton
 
- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return self;
}
 
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.ym_y = self.ym_height * 0.5 - self.titleLabel.ym_height;
    self.titleLabel.ym_x = 0;
    self.titleLabel.ym_width = self.ym_width;
 
    self.imageView.ym_y = self.titleLabel.ym_bottom + 10;
    self.imageView.ym_x = self.ym_width * 0.5 - self.imageView.ym_width * 0.5;
 
}
 
@end

复制代码


第三种:



#import "YMTestButton.h"
#import "UIView+YMExtension.h"
 
@implementation YMTestButton
 
- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return self;
}
 
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.ym_y = self.ym_height * 0.5 - self.titleLabel.ym_height;
    self.titleLabel.ym_x = 0;
 
    self.imageView.ym_x = self.titleLabel.ym_right + 5;
    self.imageView.ym_y = self.titleLabel.ym_bottom + 10;
 
}
 
@end

复制代码


UIView+YMExtension.h

#import <UIKit/UIKit.h>
 
@interface UIView (YMExtension)
 
/* 控件的size */
@property (nonatomic, assign) CGSize ym_size;
/* 控件的宽度 */
@property (nonatomic, assign) CGFloat ym_width;
/* 控件的高度 */
@property (nonatomic, assign) CGFloat ym_height;
/* 控件的x值 */
@property (nonatomic, assign) CGFloat ym_x;
/* 控件的y值 */
@property (nonatomic, assign) CGFloat ym_y;
/* 控件的中心x值 */
@property (nonatomic, assign) CGFloat ym_centerX;
/* 控件的中心Y值 */
@property (nonatomic, assign) CGFloat ym_centerY;
/* 控制件的右边 */
@property (nonatomic, assign) CGFloat ym_right;
/* 控件的底部 */
@property (nonatomic, assign) CGFloat ym_bottom;
 
@end
复制代码


#import "UIView+YMExtension.h"
 
@implementation UIView (YMExtension)
 
- (CGSize)ym_size
{
    return self.frame.size;
}
 
- (void)setYm_size:(CGSize)ym_size
{
    CGRect frame = self.frame;
    frame.size = ym_size;
    self.frame = frame;
}
 
- (CGFloat)ym_width
{
    return self.frame.size.width;
}
 
- (void)setYm_width:(CGFloat)ym_width
{
    CGRect frame = self.frame;
    frame.size.width = ym_width;
    self.frame = frame;
}
 
- (CGFloat)ym_height
{
    return self.frame.size.height;
}
 
- (void)setYm_height:(CGFloat)ym_height
{
    CGRect frame = self.frame;
    frame.size.height = ym_height;
    self.frame = frame;
}
 
- (CGFloat)ym_x
{
    return self.frame.origin.x;
}
 
- (void)setYm_x:(CGFloat)ym_x
{
    CGRect frame = self.frame;
    frame.origin.x = ym_x;
    self.frame = frame;
}
 
- (CGFloat)ym_y
{
    return self.frame.origin.y;
}
 
- (void)setYm_y:(CGFloat)ym_y
{
    CGRect frame = self.frame;
    frame.origin.y = ym_y;
    self.frame = frame;
}
 
- (CGFloat)ym_centerX
{
    return self.center.x;
}
 
- (void)setYm_centerX:(CGFloat)ym_centerX
{
    CGPoint center = self.center;
    center.x = ym_centerX;
    self.center = center;
}
 
- (CGFloat)ym_centerY
{
    return self.center.y;
}
 
- (void)setYm_centerY:(CGFloat)ym_centerY
{
    CGPoint center = self.center;
    center.y = ym_centerY;
    self.center = center;
}
 
- (CGFloat)ym_right
{
    return CGRectGetMaxX(self.frame);
}
 
- (void)setYm_right:(CGFloat)ym_right
{
    self.ym_x = ym_right - self.ym_width;
}
 
- (CGFloat)ym_bottom
{
    return CGRectGetMaxY(self.frame);
}
 
- (void)setYm_bottom:(CGFloat)ym_bottom
{
    self.ym_y = ym_bottom - self.ym_height;
}
 
@end

复制代码


  • 这里仅提供了思路和demo,没有很好的封装代码,写的有的繁琐,有空会重新封装,在外部传入文字和图片的位置


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值