简单设定Button Image和title的位置

集成两个Category文件调用一个方法设定button image和title的位置
文件地址:https://github.com/DaoPinWong/DPButtonTitlePostionType

UIButton+DPTitlePostionType.h
UIButton+DPTitlePostionType.m
typedef NS_ENUM(NSInteger, DPButtonTitlePostionType) {
    DPButtonTitlePostionTypeBottom = 1,        // text below the image
    DPButtonTitlePostionTypeTop,               // text above the image
    DPButtonTitlePostionTypeLeft,              // text on the left of the image
    DPButtonTitlePostionTypeRight              // text on the right of the image
};


@interface UIButton (DPTitlePostionType)
/**
 *  @param type  the title position
 *  @param space the space between the image and text
 */
- (void)setTitlePositionWithType:(DPButtonTitlePostionType)type space:(CGFloat)space;

@end

实例化后直接调用,传入位置枚举,间距即可。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.btn1 setTitlePositionWithType:DPButtonTitlePostionTypeTop space:5];
    [self.btn2 setTitlePositionWithType:DPButtonTitlePostionTypeBottom space:5];
    [self.btn3 setTitlePositionWithType:DPButtonTitlePostionTypeLeft space:5];
    [self.btn4 setTitlePositionWithType:DPButtonTitlePostionTypeRight space:5];
    
    UIButton *btn5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn5 setTitlePositionWithType:DPButtonTitlePostionTypeBottom space:10];
    [btn5 setTitle:@"Custom" forState:UIControlStateNormal];
    [btn5 setImage:[UIImage imageNamed:@"star"] forState:UIControlStateNormal];
    [btn5 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn5.titleLabel.font = [UIFont systemFontOfSize:12];
    btn5.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:btn5];
    btn5.frame = CGRectMake(0, 0, 100, 100);
    btn5.center = self.view.center;
}

内部使用Runtime绑定变量和方法,比较简单具体不再细说。想了解Runtime换函数的方法可以看我的另一篇文章:http://www.jianshu.com/p/5579d1056cbc

Simulator Screen Shot - iPhone SE - 2017-12-18 at 15.03.43.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,212评论 30 472
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,250评论 4 61
  • 我几次开玩笑,说要帮阿姨开个微店,专卖小馄饨,的确,阿姨的馄饨很好吃,好吃到我百吃不厌,每次连汤带水一滴不剩地喝下...
    狂奔的小鹿妹阅读 326评论 0 0
  • 我从来不会在车上睡觉 不知道为什么,每次坐车,我都喜欢望着窗外, 喜欢看窗外的山,天空的云,路过的人,跑过的狗。 ...
    石砳阅读 437评论 0 0
  • 真的不能忍了,小a同学终于哭出来了,她受不了在所有人面前装没事,受不了得在人前一直笑,一直装着像个没事人,一直以为...
    海浪一枚阅读 232评论 0 0