[英]Accessing “text styles” in Interface Builder and/or Storyboards
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. 然后,在每个视图控制器的
viewDidLoad
方法中,我以编程方式设置文本样式。 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? 这是我的问题:有没有办法让XIB文件/故事板反映这些文本样式? If not, is there any way to implement similar functionality (ie, have all the styles defined in one place, and have the XIB/Storyboard elements pull from there)?
如果没有,有没有办法实现类似的功能(即,在一个地方定义所有样式,并从那里拉出XIB / Storyboard元素)? Thanks for reading.
谢谢阅读。
EDIT: 编辑:
To clarify, here's the desired end-result: 为了澄清,这是期望的最终结果:
UILabels
in my various views to any of these text styles. UILabels
的样式设置为任何这些文本样式。 This could be done in code, in Interface Builder (eg, 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: 创建您想要实现样式的任何对象的子类,比如UILabel,并输入以下代码:
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! 这将是一个巨大的节省时间,我希望你们中的一些人发现它也很有用!
I don't really get you mean. 我真的不是你的意思。 But you want to set custom "text style" from Interface Builder you can do this.
但是,您希望从Interface Builder设置自定义“文本样式”,您可以执行此操作。 for
UILable
对于
UILable
1, Create category UILabel + MyCustomStyle. 1,创建类别UILabel + MyCustomStyle。
in UILabel + MyCustomStyle.h
在
UILabel + MyCustomStyle.h
#import <UIKit/UIKit.h>
@interface UILabel (MyCustomStyle)
@property (nonatomic, copy) NSString *mTextStyle;
@end
in UILabel + MyCustomStyle.m
在
UILabel + MyCustomStyle.m
#import "UILabel+MyCustomStyle.h"
@implementation UILabel (MyCustomStyle)
-(NSString *)mTextStyle {
return self.text;
}
-(void)setMTextStyle:(NSString *)txt{
if ([txt isEqualToString:@"h1"]) {
// custom style h1 code
self.font = ...;
}else if ([txt isEqualToString:@"h2"]){
// custom style h2 code
self.font = ...;
}
//... more custom style
}
2, Assign text style in IB 2,在IB中指定文本样式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.