What is the best way to create re-usable styling for UI objects for iOS app?
Eg I want to use the same style for each UITextField - top/bottom line and thinking about creating a category to provide the styling and simply apply it in View Controller.
Is there any better solution to do this?
UITextField+TextFieldStyler.m
- (void)addTopBorderWithColor:(UIColor *)color
{
CGRect topBorderFrame = CGRectMake(0, 0, self.bounds.size.width, 1.0);
UIView *topBorder = [[UIView alloc] initWithFrame:topBorderFrame];
topBorder.backgroundColor = color;
[self addSubview:topBorder];
}
PXRLoginViewController.m
[self.nameFIeld addTopBorderWithColor:[UIColor whiteColor]];
[self.passwordField addTopBorderWithColor:[UIColor whiteColor]];
I believe that the approach you are using is completely fine. It's also used by Apple itself for the same use cases, looking at the doc
Note: Cocoa and Cocoa Touch include a variety of categories for some of the primary framework classes.
The string-drawing functionality mentioned in the introduction to this chapter is in fact already provided for NSString by the NSStringDrawing category for OS X, which includes the drawAtPoint:withAttributes: and drawInRect:withAttributes: methods. For iOS, the UIStringDrawing category includes methods such as drawAtPoint:withFont: and drawInRect:withFont:.
As you can see, they use the same pattern for extending the drawing behaviour of the NSString.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.