139

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 property to set background color in such way:

self.textField.backgroundColor = textFieldColor;

But I have to change color of UITextField border too. And my question was about how to change the border color.

ColinE
  • 68,894
  • 15
  • 164
  • 232
dasha
  • 1,419
  • 2
  • 11
  • 5
  • Lots of helpful answers but only one (https://stackoverflow.com/a/5387607/826946) mentioned something I found key: textField.borderStyle = UITextField.BorderStyle.none. Without that I get traces of the built-in border. Seemingly once you don't use the built-in border and start defining your own, you need to say you don't want it using borderStyle = none and then define all of the parameters (color, cornerRadius, and borderWidth) – Andy Weinstein May 31 '20 at 19:04

9 Answers9

276

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
Test At
  • 58
  • 2
  • 11
Gary
  • 4,198
  • 2
  • 21
  • 26
  • 3
    It's #import instead of QuartCore (you forgot the z) – cldrr Mar 09 '12 at 09:00
  • 57
    In iOS 7 you must set a border width or the color does not take effect. – Micah Mar 26 '14 at 19:46
  • 1
    As a beginner this makes no sense to me. If I start a blank, empty app, go to the storyboard and add a TextView field. Where do I import the quartzcore? Where do I add the borderwidth info above? What is "textField" and how does it know which text field I'm talking about? – Nathan McKaskle Nov 13 '14 at 20:18
  • 1
    @Sephethus if you haven't figured it out yet, this is what you gotta do: you gotta "hook up" the textfield you created in the storyboard and change these properties **programmatically**. Once you've hooked up the storyboard, you go into your code (for example in `viewDidLoad`) and change these properties by saying `self.myTextField.layer.cornerRadius` etc. These changes will take effect as soon as you launch your app, but you cannot see the changes in the Storyboard. If this doesn't make any sense to you, I suggest you go to a website, for example Ray Wenderlich, and read some beginner tuts. – Aleksander Jan 02 '15 at 14:25
  • @NathanMcKaskle *"Where do I import the quartzcore?"* You add it on the top of your ViewController.h file. *"What is "textField" and how does it know which text field I'm talking about?"* You can create a 'local' textfield IBOutlet var and connect it to the TextField (not TextView) you've just put on the Storyboard. For more info, check here: http://hubpages.com/technology/IOS-5-A-Beginners-Guide-to-Storyboard-Connection *"Where do I add the borderwidth info above?"* Anywhere, but preferably, at viewDidLoad function. – Chen Li Yong May 13 '16 at 04:19
21

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>
avelis
  • 1,143
  • 1
  • 9
  • 18
iOSPawan
  • 2,884
  • 2
  • 25
  • 50
17

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.

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
james lobo
  • 463
  • 4
  • 10
16

Update for swift 5.0

textField.layer.masksToBounds = true
textField.layer.borderColor = UIColor.blue.cgColor
textField.layer.borderWidth = 1.0
Hugo Jordao
  • 786
  • 8
  • 9
14

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.

Jovin J
  • 462
  • 5
  • 15
10

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];
skywinder
  • 21,291
  • 15
  • 93
  • 123
10

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];
StefanLdhl
  • 796
  • 7
  • 11
4

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. :)

Rameswar Prasad
  • 1,331
  • 17
  • 35
  • Please don't post identical answers to multiple questions. Post one good answer, then vote/flag to close the other questions as duplicates. If the question is not a duplicate, _tailor your answers to the question_. – josliber Dec 14 '15 at 23:25
3
extension UIView {
    func addBorder(_ width: CGFloat = 1, color: UIColor = .black, cornerRadius: CGFloat = 4) {
        layer.borderWidth = width
        layer.borderColor = color.cgColor
        layer.cornerRadius = cornerRadius
    }
}

Call this like: email.addBorder(1.0, color: .blue, cornerRadius: 5).

SmileBot
  • 19,393
  • 7
  • 65
  • 62
meow2x
  • 2,056
  • 22
  • 27