SWIFT UIFON IBInspectable-有可能吗? [英] Swift UIFont IBInspectable - is it possible?

查看:45
本文介绍了SWIFT UIFON IBInspectable-有可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIView子类。在此视图中,我创建了UIlabel的实例。 现在我想在故事板中设置这些标签的字体属性。是否可以为UIFont创建IBInspectable

我的方法之一是:

@IBInspectable var fontName: UIFont

但它不起作用。

总结:我正在尝试为UIView获取此信息:

希望有人能帮助我,谢谢!:)

推荐答案

想法

您可以使用Int枚举选择其中一种特定字体。

详细信息

Xcode 8.2.1,SWIFT 3

代码

枚举字体类型:int

import UIKit
enum FontType: Int {
    case Default = 0, Small, Large

    var fount: UIFont {
        switch self {
            case .Default:
                return UIFont.systemFont(ofSize: 17)
            case .Small:
                return UIFont.systemFont(ofSize: 12)
            case .Large:
                return UIFont.systemFont(ofSize: 24)
        }
    }


    static func getFont(rawValue: Int) -> UIFont  {
        if let fontType = FontType(rawValue: rawValue) {
            return fontType.fount
        }
        return FontType.Default.fount
    }
}

类视图:UIView

import UIKit
@IBDesignable
class View: UIView {

    private var label: UILabel!

    @IBInspectable var textFont:Int = 0

    override func draw(_ rect: CGRect) {
        super.draw(rect)
        label = UILabel(frame: CGRect(x: 20, y: 20, width: 120, height: 40))
        label.text = "Text"
        label.textColor = .black
        label.font = FontType.getFont(rawValue: textFont)
        addSubview(label)
    }

}

Main.Storyboard

结果




这篇关于SWIFT UIFON IBInspectable-有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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