访问“文本样式”在Interface Builder和/或Storyboards中 [英] Accessing "text styles" in Interface Builder and/or Storyboards

查看:94
本文介绍了访问“文本样式”在Interface Builder和/或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];
}

然后,在 viewDidLoad 每个视图控制器的方法,我以编程方式设置文本样式。这是保持应用程序样式一致且易于调整的一种非常棒的方式。

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.

这是我的问题:有没有办法让XIB文件/故事板反映这些文字样式?如果没有,有没有办法实现类似的功能(即,在一个地方定义所有样式,并从那里拉出XIB / Storyboard元素)?感谢您的阅读。

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.

编辑:

澄清一下,这是所需的最终结果:

To clarify, here's the desired end-result:


  1. 在项目的某处定义一些常量文本样式,如h1,h2,p。每种文本样式都有自己的字体,字体大小,颜色等。

  2. 能够设置 UILabels 的样式我对这些文本样式的各种看法。这可以在代码中,在Interface Builder中完成(例如,在Luan建议的用户定义的运行时属性中),或者在任何地方。

  3. 能够在Interface Builder中看到应用于每个UILabel的样式/ Storyboard ,无需每次都运行应用

  1. Define some constant text styles like h1, h2, p somewhere in the project. 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. This could be done in code, in Interface Builder (e.g., in User Defined Runtime Attributes as Luan suggested), or wherever.
  3. Be able to see the styles applied to each UILabel in Interface Builder / Storyboard without having to run the app every time.


推荐答案

好的,事实证明这是可能的!方法如下:

OK, so it turns out this is possible to do! Here's how:


  1. 添加样式类,您可以将所有样式信息放在一个位置:

  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");
    }
  }
}


  • 创建一个子类你想要实现样式的任何对象,比如UILabel,并输入以下代码:

  • 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)} 
      }
    }
    


  • 在Interface Builder中将UILabel添加到视图中,并将其类设置为MyLabel。然后在属性检查器中,您将看到一个样式字段,您可以在其中键入h1,h2或其他内容,标签的字体将立即更新。想要将所有h1标签更改为48的大小?只需更新MyStyles,您就会立即看到XIB / Storyboard中的更改。

  • 这将是一个巨大的时间 - 保护,我希望你们中的一些人觉得它也很有用!

    This is going to be a huge time-saver, I hope some of you find it useful too!

    这篇关于访问“文本样式”在Interface Builder和/或Storyboards中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆