In one of my apps I have a styles document with methods for different text styles, for example:
+(UIFont*)h1{
return [UIFont fontWithName:@"Helvetica" size:48.0];
}
Then, in the viewDidLoad
methods of each my view controllers, I set the text styles programmatically. It's been a really great way to keep styles across the app consistent and easy to tweak.
Here's my question: is there any way to have the XIB files/Storyboards reflect these text styles? If not, is there any way to implement similar functionality (i.e., have all the styles defined in one place, and have the XIB/Storyboard elements pull from there)? Thanks for reading.
EDIT:
To clarify, here's the desired end-result:
UILabels
in my various views to any of these text styles. This could be done in code, in Interface Builder (e.g., in User Defined Runtime Attributes as Luan suggested), or wherever.OK, so it turns out this is possible to do! Here's how:
Add a styles class, where you can put all your style info in one place:
import UIKit
class MyStyles: NSObject {
static func fontForStyle(style:String)->UIFont{
switch style{
case "p":
return UIFont.systemFontOfSize(18);
case "h1":
return UIFont.boldSystemFontOfSize(36);
case "h2":
return UIFont.boldSystemFontOfSize(24);
default:
return MyStyle.fontForStyle("p");
}
}
}
Make a subclass of any objects you'd like to implement the styles, say UILabel, and enter the following code:
import UIKit
@IBDesignable class MyLabel: UILabel {
@IBInspectable var style:String="p"{
didSet{self.font=MyStyle.fontForStyle(style)}
}
}
This is going to be a huge time-saver, I hope some of you find it useful too!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With