Style all UITextFields at once, and not in every ViewController?

I am trying to create all text fields as follows:

CGRect frameRect2 = _lastname_field.frame; frameRect2.size.height = 30; _lastname_field.font = [UIFont fontWithName:@"AppleGothic" size:15]; _lastname_field.frame = frameRect2; _lastname_field.backgroundColor= [UIColor whiteColor]; _lastname_field.layer.cornerRadius = 5.0; [_lastname_field.layer setBorderColor: [[UIColor grayColor] CGColor]]; [_lastname_field.layer setBorderWidth: 1.0]; _lastname_field.clipsToBounds = YES; 

This field looks great. The only problem is that there are more than 50 text fields in the entire application. Is there an easy way to focus them all the same way? Some of them were not even synthesized / named.

Thanks!

+6
source share
5 answers

1) If you create your files inside your code , I would create a factory class with this interface:

 @interface MyTextFieldFactory : NSObject + (UITextField*)createStyledTextField; @end 

2) If you create TextFields using Interface Builder , you can write a category in UITextField that implements only awakeFromNib: There you make all your settings. This does not require code changes, and does not require changes to nib files.

+9
source

Subclass UITextField and add your customization to the initWithFrame method. Add the initWithCoder method to the user class where you call super and return [self initWithFrame: [self frame]]. Then change the class in Interface Builder for each text field.

It will be something like this:

 // MyUITextField.h @interface MyUITextField : UITextField { // Your IVars } @property (retain, readonly) float value; @end // MyUITextField.m @implementation MyClass - (id)initWithCoder:(NSCoder *)decoder { [super initWithCoder:decoder]; return [self initWithFrame:[self frame]]; } - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Your customization } } @end 
+3
source

You should be able to do this for all properties except the layer material ...

in AppDelegate add a method similar to ...

 - (void)changeAppearances { [[UITextField appearance] setFont:[UIFont fontWithName:@"AppleGothic" size:15]]; [[UITextField appearance] setBackgroundColor:[UIColor whiteColor]]; } 

etc.

This will set the look for all instances of UITextField in your application. (until you overwrite them in your code).

+2
source

OK, if looks won't work, then your best bet is to write a category to do it for you.

Subclassification is possible, but not ideal, because it is possible to redefine what you should not (or vice versa).

Add the file to your project and select the category type and set the class as UITextField.

In the category add a function something like ...

 - (void)configureTextFieldWithFrame:(CGRect)frame; 

Then in the .m file you can ...

 - (void)configureTextFieldWithFrame:(CGRect)frame { self.font = [UIFont fontWithName:@"AppleGothic" size:15]; self.frame = frame; self.backgroundColor= [UIColor whiteColor]; self.layer.cornerRadius = 5.0; [self.layer setBorderColor: [[UIColor grayColor] CGColor]]; [self.layer setBorderWidth: 1.0]; self.clipsToBounds = YES; } 

Then you just need #import UITextField+Configure.h (or whatever you called categoyr.

Then in your code, replace your code with ...

 [_lastname_field configureTextFieldWithFrame:frameRect2]; 

This will launch your category function.

+1
source

I agree with Fogmeister for text fields that were created in code. But if you put text fields in the Storiesboard, this approach will not work (since each field explicitly defines its properties). But there is an easy way that really works.

Right-click on your storyboard and "Open As .." Source Code. This puts the SB XML view in the editor window. There you can change the properties of the text field globally (and / or selectively) using the editor (or copy to the XML editor of your choice).

A fair warning, you can kill your project if you introduce errors in the SB that will not allow it to compile, so be very careful and make sure that you have a backup for your SB. But if you check after each change, this method may work very well.

Search for " <textField " to find something like this:

 <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="name" minimumFontSize="17" clearButtonMode="whileEditing" id="F9N-Tb-KTd"> <rect key="frame" x="176" y="301" width="472" height="31"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <fontDescription key="fontDescription" type="system" pointSize="14"/> <textInputTraits key="textInputTraits" autocapitalizationType="words" enablesReturnKeyAutomatically="YES"/> <connections> <action selector="changeName:" destination="4" eventType="editingDidEnd" id="bLg-iM-m8a"/> </connections> </textField> 

Find one text box that has fontDescription properties that you like and one that doesn't. Then replace the fontDescription properties you want to change with the appropriate properties from the good. Remember to limit your changes to things like font, size and background. Do not change id, rect or anything else that should be unique to the text field.

I hope this works for you, it was a very convenient technique for me to make sure that all of my text fields have consistent typography.

(To return to normal view, "Open As ..." Interface Builder - Storyboard)

Good luck

+1
source

Source: https://habr.com/ru/post/927202/


All Articles