Aram Boyajyan
Aram Boyajyan

Reputation: 844

Standardizing styles for programmatically generated elements

I started creating all elements programmatically and I repeat most of the code for styling the fields (e.g. borderStyle, keyboardType, keyboardAppearance, spellCheckingType, etc.).

What is the most acceptable way of doing this from one place?

Analogue would be CSS - I would define the general styles in one place, and all generated elements would use them automatically.

Thanks!

Upvotes: 1

Views: 26

Answers (1)

Douglas Hill
Douglas Hill

Reputation: 1547

If I kept wanting UITextFields with a certain setup, my preferred approach would be to add a factory method to UITextField:

@implementation UITextField (DHFactory)

+ (instancetype)dh_textField {

    UITextField *textField = [[self alloc] init];

    [textField setKeyboardAppearance:UIKeyboardAppearanceAlert];
    [textField setSpellCheckingType:UITextSpellCheckingTypeNo];

    return textField;
}

@end

I prefixed the category method name, following the advice in Apple’s Programming with Objective-C.

Upvotes: 2

Related Questions