UITextField border color

后端 未结 9 1469
一生所求
一生所求 2020-11-28 02:43

I have really great wish to set my own color to UITextField border. But so far I could find out how to change the border line style only.

I\'ve used background prope

9条回答
  •  北海茫月
    2020-11-28 03:22

    To simplify this actions from accepted answer, you can also create Category for UIView (since this works for all subclasses of UIView, not only for textfields:

    UIView+Additions.h:

    #import 
    
    @interface UIView (Additions)
    - (void)setBorderForColor:(UIColor *)color 
                        width:(float)width 
                       radius:(float)radius;
    @end
    

    UIView+Additions.m:

    #import "UIView+Additions.h"
    
    @implementation UIView (Additions)
    
    - (void)setBorderForColor:(UIColor *)color 
                        width:(float)width
                       radius:(float)radius
    {
        self.layer.cornerRadius = radius;
        self.layer.masksToBounds = YES;
        self.layer.borderColor = [color CGColor];
        self.layer.borderWidth = width;
    }
    
    @end
    

    Usage:

    #import "UIView+Additions.h"
    //...
    [textField setBorderForColor:[UIColor redColor]
                           width:1.0f
                          radius:8.0f];
    

提交回复
热议问题