Bose
Bose

Reputation: 253

iOS: How to apply same style to another UITextField?

Can anyone please help me, does it possible to apply the same styles(border color, border width, corner radius) from UITextField1 to UITextField2.

In Detail, I have applied above styles to "Password" field. I wish to apply the same styles to confirm password, email, country, etc fields.

[[self.passwordField layer] setBorderColor:[[UIColor darkGrayColor] CGColor]];
[[self.passwordField layer] setBorderWidth:0.9];
[[self.passwordField layer] setCornerRadius:5];

Please guide me if there is any easy way to do this, by avoiding code repeated code lines? Thanks in advance!

Upvotes: 1

Views: 103

Answers (4)

Rashad
Rashad

Reputation: 11197

Try this:

-(void)setTextFieldProperty:(UITextField *)myTextField{
    [[myTextField layer] setBorderColor:[[UIColor darkGrayColor] CGColor]];
    [[myTextField layer] setBorderWidth:0.9];
    [[myTextField layer] setCornerRadius:5];
}

just call this method by:

[self setTextFieldProperty:mytestField]

it will do the rest. Hope this helps.. :)

Upvotes: 3

ChintaN -Maddy- Ramani
ChintaN -Maddy- Ramani

Reputation: 5164

Try this. take view in which all your UITextField is added. Make sure this will affect all textfield of your view. you dont need to pass any textfield.

for (UITextField *txtFld in self.view.subviews)// set your textfield superview
{
    if ([txtFld isKindOfClass:[UITextField class]])
    {
        [[txtFld layer] setBorderColor:[[UIColor darkGrayColor] CGColor]];
        [[txtFld layer] setBorderWidth:0.9];
        [[txtFld layer] setCornerRadius:5];
    }
}

Maybe this will help you.

Upvotes: 1

Jonathan F.
Jonathan F.

Reputation: 2364

You can create a category on UITextField :

- (void) isPasswordField
{
    [[self layer] setBorderColor:[[UIColor darkGrayColor] CGColor]];
    [[self layer] setBorderWidth:0.9];
    [[self layer] setCornerRadius:5];
}

And call it like this :

[self.passwordField isPasswordField];

Upvotes: 1

bobnoble
bobnoble

Reputation: 5824

Create a collection of the views, then iterate over the collection, setting the layer properties as desired. E.g.:

NSArray *myTextFields = @[self.passwordField, self.emailField, self.countryField];
for (UIView *view in myTextFields){
    [[view layer] setBorderColor:[[UIColor darkGrayColor] CGColor]];
    [[view layer] setBorderWidth:0.9];
    [[view layer] setCornerRadius:5];
}

Upvotes: 2

Related Questions