UITextField border color

后端 未结 9 1443
一生所求
一生所求 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:26

    Import QuartzCore framework in you class:

    #import <QuartzCore/QuartzCore.h>
    

    and for changing the border color use the following code snippet (I'm setting it to redColor),

        textField.layer.cornerRadius=8.0f;
        textField.layer.masksToBounds=YES;
        textField.layer.borderColor=[[UIColor redColor]CGColor];
        textField.layer.borderWidth= 1.0f;
    

    For reverting back to the original layout just set border color to clear color,

        serverField.layer.borderColor=[[UIColor clearColor]CGColor];
    

    in swift code

        textField.layer.borderWidth = 1
        textField.layer.borderColor = UIColor.whiteColor().CGColor
    
    0 讨论(0)
  • 2020-11-28 03:28

    Here's a Swift implementation. You can make an extension so that it will be usable by other views if you like.

    extension UIView {
        func addBorderAndColor(color: UIColor, width: CGFloat, corner_radius: CGFloat = 0, clipsToBounds: Bool = false) {
            self.layer.borderWidth  = width
            self.layer.borderColor  = color.cgColor
            self.layer.cornerRadius = corner_radius
            self.clipsToBounds      = clipsToBounds
        }
    }
    

    Call this like: email.addBorderAndColor(color: UIColor.white, width: 0.5, corner_radius: 5, clipsToBounds: true).

    0 讨论(0)
  • 2020-11-28 03:29

    Try this:

    UITextField *theTextFiels=[[UITextField alloc]initWithFrame:CGRectMake(40, 40, 150, 30)];
        theTextFiels.borderStyle=UITextBorderStyleNone;
        theTextFiels.layer.cornerRadius=8.0f;
        theTextFiels.layer.masksToBounds=YES;
            theTextFiels.backgroundColor=[UIColor redColor];
        theTextFiels.layer.borderColor=[[UIColor blackColor]CGColor];
        theTextFiels.layer.borderWidth= 1.0f;
    
        [self.view addSubview:theTextFiels];
        [theTextFiels release];
    

    and import QuartzCore:

    #import <QuartzCore/QuartzCore.h>
    
    0 讨论(0)
提交回复
热议问题