處理UIButton的圖片和文字


最近項目做了閑着沒事,然后就針對項目的小功能封裝一些常用的小控件,個人認為對於一般的app還是比較實用一點,button的image和lable可以在layoutSubviews自定義它們的位置,直接上代碼:

 

 

ps:簡的的用法

#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 <UIKit/UIKit.h>

 

@interface YMTestButton : UIButton

 

@end

 

ps:下面我寫了三種情況,代碼都附上

 

 

 

 

 

#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

 

ps:UIView+YMExtension.h這是參考MJ大神的(根據自己項目修改)

 

#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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM