Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextField set border color using storyboard

I'd like to set border color using storyboard if possible. I've seen answer here: UITextField border color

And I followed answer in storyboard:

enter image description here

All properties set, but TextField doesn't show border. Any suggestions?

like image 242
katit Avatar asked Oct 06 '14 17:10

katit


People also ask

How do you put a border color on a storyboard?

borderColor needs a value with type CGColor . But only UIColor types can be set via Interface Builder's User Defined Runtime Attributes! So, you must set a UIColor to a proxy property via Interface Builder, then intercept that call to set the equivalent CGColor to the layer. borderColor property.

How do you color border TextField?

You can change the TextField border color globally by defining the inputDecorationTheme and then adding the OutlineInputBorder widget. Inside the OutlineInputBorder widget, you can specify which type of border you want to change. for example, enabledBorder , focusedBorder , and so on, and then assign the color.

What is UITextField in swift?

An object that displays an editable text area in your interface.


2 Answers

Well as Bogdan pointed out, you could very well do that with simple subclassing and just a few bits of code. After that everything will be editable in Storyboards.

  1. Subclass UITextField
  2. Create two properties, one for border width and one for border color
  3. Make those variables IBInspectable and entire class IBDesignable
  4. You'll be able to change color and width of border and see the change in real time.

Code for illustration (Swift 3.1):

@IBDesignable
class FormTextField: UITextField {

    @IBInspectable var borderColor: UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
    }

    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }
}

Edit: You'll see this in your Attributes Inspector

Attributes Inspector

like image 84
Markus Avatar answered Sep 29 '22 21:09

Markus


As Bogdan pointed out it's true that you can't find the layer.borderColor property in storyboard as it's a run time thing.

However still you can set the borderColor without using IB_DESIGNABLE, on any view(or UIView Subclass) with a little bit of coding.

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 3.1:

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.

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

Build and Run. Happy Coding. :)

like image 28
Rameswar Prasad Avatar answered Sep 29 '22 22:09

Rameswar Prasad