美文网首页
简单设定Button Image和title的位置

简单设定Button Image和title的位置

作者: 你猜我猜不猜你猜我猜不猜 | 来源:发表于2017-12-18 15:04 被阅读0次

集成两个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

相关文章

网友评论

      本文标题:简单设定Button Image和title的位置

      本文链接:https://www.haomeiwen.com/subject/rmfmwxtx.html