zoukankan      html  css  js  c++  java
  • 处理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_(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

     

     

  • 相关阅读:
    HTML5中类jQuery选择器querySelector的使用
    java发布环境时,Xshell常用的命令(基础)
    java后端:实现导出excel,按其中一个列的数据生成二维码图片,显示在列表中
    SQL 函数:case when 的用法
    微服务-学习笔记
    初学笔记:存储过程的简单概念
    初学笔记:GROUP_CONCAT 的作用,和使用条件
    jsp练习
    数据库2
    数据库
  • 原文地址:https://www.cnblogs.com/ljj-Andrew-519/p/7183265.html
Copyright © 2011-2022 走看看