In my iOS application, I would like to define text style with font name, font size, color etc. (same as in CSS
) and reuse it everywhere in my code.
I tried to find it in Storyboard without any luck. Do you know any way to achieve it?
In my iOS application, I would like to define text style with font name, font size, color etc. (same as in CSS
) and reuse it everywhere in my code.
I tried to find it in Storyboard without any luck. Do you know any way to achieve it?
Here's method for doing this programmatically.
You can set global appearance settings for each class. It's done using UIAppearance proxy, available since iOS 5.
By calling this code:
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:17.0]];
Somewhere at the start of your app, you'll set this font for UILabels, that would be created later.
There are same UIAppearance interfaces for UINavigationBar, and some other classes.
P.S. There's a bad situation here, you'll still have to set font size everywhere, since I think you don't want to have same size everywhere...
So, another way is to create category for UIFont:
#import <UIKit/UIKit.h>
@interface UIFont (SytemFontOverride)
@end
@implementation UIFont (SytemFontOverride)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"fontName" size:fontSize];
}
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize {
return [UIFont fontWithName:@"fontName" size:fontSize];
}
#pragma clang diagnostic pop
@end
Took that answer from this thread. Don't forget to set plus there :)
You can't create style for text in iOS like you do in CSS. Only solution I would suggest is create a new class for the label, which inherits from UILabel, than create a class method which defines the font for it like this:
myLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
You can make sub-class of UILabel and override methods like awakeFromNib
, -(id)init
, -(id)initWithFrame
, etc. In these methods you can call a common method where you can customise your label.
On storyBoard, you can drag and drop label and change its class from UILabel
to your Custom Class
-(id)init
{
self = [super init];
if (self)
{
[self customiseLabel];
}
return self;
}
-(void)customiseLabel
{
self.layer.cornerRadius = 4.0;
}
Similarly you override other methods also. Doing as shown, you don't need to customise your label all the time.