UIButton的圖片和文字位置

餘默發表於2018-07-06
  • 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,下面我寫了三種情況,程式碼都附上


第一種:

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複製程式碼


第二種:

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.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

複製程式碼


第三種:

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.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,沒有很好的封裝程式碼,寫的有的繁瑣,有空會重新封裝,在外部傳入文字和圖片的位置


相關文章