简体   繁体   English

访问Interface Builder和/或Storyboard中的“文本样式”

[英]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: 为了澄清,这是期望的最终结果:

  1. Define some constant text styles like h1, h2, p somewhere in the project. 在项目的某处定义一些常量文本样式,如h1,h2,p。 Each text style has its own font, font size, colour, and so on. 每种文本样式都有自己的字体,字体大小,颜色等。
  2. Be able to set the style of 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. 这可以在代码中,在Interface Builder中完成(例如,在Luan建议的用户定义的运行时属性中),或者在任何地方。
  3. Be able to see the styles applied to each UILabel in Interface Builder / Storyboard without having to run the app every time. 能够在Interface Builder / Storyboard中查看应用于每个UILabel的样式, 而无需每次都运行应用程序

OK, so it turns out this is possible to do! 好的,事实证明这是可能的! Here's how: 这是如何做:

  1. 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"); } } } 
  2. 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)} } } 
  3. Add a UILabel to your view in Interface Builder and set its class to MyLabel. 在Interface Builder中将UILabel添加到视图中,并将其类设置为MyLabel。 Then in the Attributes Inspector, you'll see a Style field where you can type h1, h2 or whatever, and the label's font will update right away. 然后在属性检查器中,您将看到一个样式字段,您可以在其中键入h1,h2或其他内容,标签的字体将立即更新。 Want to change all your h1 labels to have a size of 48? 想要将所有h1标签更改为48的大小? Just update MyStyles and you'll see the changes in your XIBs/Storyboards right away. 只需更新MyStyles,您就会立即看到XIB / Storyboard中的更改。

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.

相关问题 Interface Builder中的“文本样式”是什么? - What is this Text Styles thing in Interface Builder? 在Interface Builder中使用Storyboard隐藏导航栏 - Hide Navigation Bar in Interface Builder w/ Storyboards Storyboards 和 Interface Builder 之间的关系是什么? - What is the relationship between Storyboards and the Interface Builder? iOS Interface Builder自定义样式? - iOS Interface Builder Custom Styles? 使用Interface Builder和故事板构建控制器之间的关系 - Using Interface Builder and storyboards to build relationships between controllers 如何使用故事板在界面构建器中连接自定义UIView? - How to connect custom UIView in interface builder using storyboards? Interface Builder会以较小的增量降低故事板,调整视图大小和重新定位视图 - Interface Builder degrades storyboards, resizes and repositions views in small increments 在 Interface Builder 中访问 animationDuration 和 animationRepeatCount - Accessing to animationDuration and animationRepeatCount in Interface Builder 从视图控制器访问接口构建器对象 - Accessing interface builder object from view controller 以编程方式访问界面生成器中添加的 UIView 的子视图 - Programmatically accessing subviews of UIView added in Interface Builder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM