纯色圆角按钮

来源:互联网 发布:淘宝按重量设置运费 编辑:程序博客网 时间:2024/06/02 22:26

#define kBtnBackgroundColor [UIColor colorWithRed:73.0/255.0 green:189.0/255.0 blue:204.0/255.0 alpha:1.0]

#define kBtnBackgroundColor1 [UIColor colorWithRed:73.0/255.0 green:200.0/255.0 blue:30.0/255.0 alpha:1.0]

#define kBtnFont    [UIFont fontWithName:@"arial" size:17]

    

    //纯色圆角按钮1

    UIButton *btnLogin = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btnLogin.frame =CGRectMake(30.0,275.0,260.0,45.0);

    [btnLogin setBackgroundColor:kBtnBackgroundColor];

    btnLogin.layer.cornerRadius =4.0;//关键点,设置btn的圆角大小

    btnLogin.titleLabel.font =kBtnFont;

    [UIFontfontWithName:@"arial"size:17];

    [btnLogin setTitle:@"ok"forState:UIControlStateNormal];

    [btnLogin setTintColor:[UIColorwhiteColor]];//关键点,设置btn的文字颜色

    [btnLogin addTarget:selfaction:@selector(changePw:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btnLogin];

    

    //纯色圆角按钮2

    UIButton *btnLogin1 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btnLogin1.frame =CGRectMake(30.0,200.0,260.0,45.0);

    [btnLogin1 setBackgroundColor:kBtnBackgroundColor1];

    btnLogin1.layer.cornerRadius =4.0;

    btnLogin1.titleLabel.font =kBtnFont;

    [UIFontfontWithName:@"arial"size:17];

    [btnLogin1 setTitle:@"ok"forState:UIControlStateNormal];

    [btnLogin1 setTintColor:[UIColorwhiteColor]];

    [btnLogin1 addTarget:selfaction:@selector(changePw:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:btnLogin1];




实战

项目中多个地方用到了自定义的圆角矩形,但是我想用XIB来实现自定义的按钮的创建

于是重写UIButton

内容:

- (id)initWithCoder:(NSCoder *)aDecoder

{

   if(self == [superinitWithCoder:aDecoder])

    {

        [selfinitlization];

    }

    return self;

}


- (void)initlization

{

    [selfsetBackgroundColor:kBtnBackgroundColor];

    self.layer.cornerRadius =4.0;//关键点,设置btn的圆角大小

    [selfsetTintColor:[UIColorblackColor]];//关键点,设置btn的文字颜色

}


在使用时,直接用XIB来拖一个按钮,

 使用即可,

现在还有一个问题,那个- (id)initWithCoder:(NSCoder *)aDecoder 方法不点明白,是什么时候才会用到的呢?

0 0
原创粉丝点击