0

EDIT #2

It seems based on the responses I've gotten that I'm confusing people (and subsequently myself). So let's try to simplify this question some -

I wish to give all TextFields in a given ViewController the following:

textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES;
[[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]];
textField.layer.borderWidth= 1.0f;

Where should I implement this and how so that it does not give me errors for the layer property (ie when I try to do this in or after -(void)viewDidLoad I get an error for every line stating that "Property 'layer' not found on object of type ViewController"?

EDIT #1 Full section of subclass code to assist in identifying the issue:

@interface InputTextField : UITextField
@end
@implementation InputTextField

- (CGRect)textRectForBounds:(CGRect)bounds {
int margin = 5;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
int margin = 5;
CGRect inset = CGRectMake(bounds.origin.x + margin, bounds.origin.y, bounds.size.width - margin, bounds.size.height);
return inset;

InputTextField *textField=[[InputTextField alloc]init];
textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES;
[[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]];
textField.layer.borderWidth= 1.0f;
}

@end

ORIGINAL POST

I'm having an issue changing the Border Style and Color of a range of Text Fields within a particular View Controller. I have a bunch of Text Fields in a view that I want to adjust. They all have been given the custom class 'InputTextField'. However the solutions brought up in this thread: UITextField border color do not solve my issue.

Here's the Style and Color I'd like to achieve:

textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES;
[[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]];
textField.layer.borderWidth= 1.0f;

I've also imported QuartzCore/QuartzCore.h. I want to set this so every TextField in my app with the custom class InputTextField will appear with this background. At this point, whenever I run this in the app, the fields all take the background value I've set in Storyboards (currently No Border).

Thanks for the assistance!

Community
  • 1
  • 1
Slider257
  • 69
  • 1
  • 12

2 Answers2

1

The problem is here:

- (CGRect)editingRectForBounds:(CGRect)bounds {

after

return inset;

nothing gets called. Move your code above return.

EDIT #1

Add this to your subclass:

- (void)layoutSubviews {
    CALayer *layer = self.layer;
    layer.cornerRadius = 8;
    layer.borderWidth = 1;
    layer.borderColor = [UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0].CGColor;
    [super layoutSubviews];
}
Stas Zhukovskiy
  • 799
  • 9
  • 21
  • Sadly that did not fix the problem. Moving it between the editing RectForBounds section does not affect the fields at all. Moving it between the RectForBounds section at the top causes a throwback error. – Slider257 Jan 14 '14 at 19:13
  • I also attempted to remove the following line: InputTextField *textField=[[InputTextField alloc]init]; in the hopes that it would work since we're already working with the InputTextField however it causes errors with each line. Defining textField as InputTextField causes errors stating "Property 'layer' not found on object of type 'UITextField'. – Slider257 Jan 14 '14 at 19:17
  • For clarification - I am no longer using a subclass to define other attributes for UITextFields. So your - (void)layoutSubviews unfortunately does not work when called. – Slider257 Jan 23 '14 at 20:33
1

You are instantiating an InputTextField inside a method that just asking you a CGRect to draw a rectangle, that already doesn't make much sense but the code will never get called anyway since it's located after the return statement.

If you want all your InputTextField to look a specific way set the layer when the InputTextField is instantiated.

Using storyboards that would look like :

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.layer.cornerRadius=8.0f;
        self.layer.masksToBounds=YES;
        [self.layer setBorderColor:[[UIColor colorWithRed:171.0/255.0 green:171.0/255.0 blue:171.0/255.0 alpha:1.0] CGColor]];
        self.layer.borderWidth= 1.0f;
    }
    return self;
}
Jkmn
  • 571
  • 5
  • 11
  • Hey @Jkmn - this does not work due to the layer property not being found on the VC. – Slider257 Jan 23 '14 at 20:22
  • This code is meant for a class that is subclassing UITextField (the one you wrote `@interface InputTextField : UITextField`). – Jkmn Jan 24 '14 at 11:25