UITextField border color

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

    this question shows up pretty high on a Google search and worked for the most part! I did find that Salman Zaidi's answer was partially correct for iOS 7.

    You need to make a modification to the "reverting" code. I found that the following for reverting worked perfectly:

    textField.layer.cornerRadius = 0.0f;
    textField.layer.masksToBounds = YES;
    textField.layer.borderColor = [[UIColor blackColor] CGColor];
    textField.layer.borderWidth = 0.0f;
    

    I understand that this is most likely due to changes in iOS 7.

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

    If you use a TextField with rounded corners use this code:

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

    To remove the border:

    self.TextField.layer.masksToBounds=NO;
    self.TextField.layer.borderColor=[[UIColor clearColor]CGColor];
    
    0 讨论(0)
  • 2020-11-28 03:20

    Update for swift 5.0

    textField.layer.masksToBounds = true
    textField.layer.borderColor = UIColor.blue.cgColor
    textField.layer.borderWidth = 1.0
    
    0 讨论(0)
  • 2020-11-28 03:20

    borderColor on any view(or UIView Subclass) could also be set using storyboard with a little bit of coding and this approach could be really handy if you're setting border color on multiple UI Objects.

    Below are the steps how to achieve it,

    1. Create a category on CALayer class. Declare a property of type UIColor with a suitable name, I'll name it as borderUIColor .
    2. Write the setter and getter for this property.
    3. In the 'Setter' method just set the "borderColor" property of layer to the new colors CGColor value.
    4. In the 'Getter' method return UIColor with layer's borderColor.

    P.S: Remember, Categories can't have stored properties. 'borderUIColor' is used as a calculated property, just as a reference to achieve what we're focusing on.

    Please have a look at the below code sample;

    Objective C:

    Interface File:

    #import <QuartzCore/QuartzCore.h>
    #import <UIKit/UIKit.h>
    
    @interface CALayer (BorderProperties)
    
    // This assigns a CGColor to borderColor.
    @property (nonatomic, assign) UIColor* borderUIColor;
    
    @end
    

    Implementation File:

    #import "CALayer+BorderProperties.h"
    
    @implementation CALayer (BorderProperties)
    
    - (void)setBorderUIColor:(UIColor *)color {
        self.borderColor = color.CGColor;
    }
    
    - (UIColor *)borderUIColor {
        return [UIColor colorWithCGColor:self.borderColor];
    }
    
    @end
    

    Swift 2.0:

    extension CALayer {
    var borderUIColor: UIColor {
        set {
            self.borderColor = newValue.CGColor
        }
    
        get {
            return UIColor(CGColor: self.borderColor!)
        }
    }
    }
    

    And finally go to your storyboard/XIB, follow the remaining steps;

    1. Click on the View object for which you want to set border Color.
    2. Click on "Identity Inspector"(3rd from Left) in "Utility"(Right side of the screen) panel.
    3. Under "User Defined Runtime Attributes", click on the "+" button to add a key path.
    4. Set the type of the key path to "Color".
    5. Enter the value for key path as "layer.borderUIColor". [Remember this should be the variable name you declared in category, not borderColor here it's borderUIColor].
    6. Finally chose whatever color you want.

    You've to set layer.borderWidth property value to at least 1 to see the border color.

    Build and Run. Happy Coding. :)

    0 讨论(0)
  • 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 <Foundation/Foundation.h>
    
    @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];
    
    0 讨论(0)
  • 2020-11-28 03:25

    Import the following class:

    #import <QuartzCore/QuartzCore.h> 
    

    //Code for setting the grey color for the border of the text field

    [[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0
                                                       green:171.0/255.0
                                                        blue:171.0/255.0
                                                       alpha:1.0] CGColor]];
    

    Replace 171.0 with the respective color number as required.

    0 讨论(0)
提交回复
热议问题