【问题标题】:Swift UIFont IBInspectable - is it possible?Swift UIFont IBInspectable - 有可能吗?
【发布时间】:2017-08-22 16:35:05
【问题描述】:

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

我的一种方法是:

@IBInspectable var fontName: UIFont

但它不会起作用。

总结一下:我想为UIView 获取这个:

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

【问题讨论】:

  • 如果没有黑客攻击,这是不可能的,真是太可惜了。 Apple 应该在 Xcode 9 中添加这个。

标签: swift uiview attributes interface-builder uifont


【解决方案1】:

想法

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

详情

xCode 8.2.1,斯威夫特 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

结果




【讨论】:

    【解决方案2】:

    我是这样做的

    fileprivate var _fontSize:CGFloat = 18
        @IBInspectable
        var font:CGFloat
        {
            set
            {
                _fontSize = newValue
                lblPlaceholder.font = UIFont(name: _fontName, size: _fontSize)
            }
            get
            {
                return _fontSize
            }
        }
    
        fileprivate var _fontName:String = "Helvetica"
        @IBInspectable
        var fontName:String
        {
            set
            {
                _fontName = newValue
                lblPlaceholder.font = UIFont(name: _fontName, size: _fontSize)
            }
            get
            {
                return _fontName
            }
        }
    

    【讨论】:

      【解决方案3】:
        @IBInspectable var isShowfontName : Bool  = false
          @IBInspectable var fontFamilyIndex : CGPoint = CGPoint(x: -1, y: -1 )
          var  leoFontName : String? {
      
              if Int(fontFamilyIndex.x) >= 0 && Int(fontFamilyIndex.x) < UIFont.familyNames.sorted().count {
      
                  let fontNames = UIFont.fontNames(forFamilyName: UIFont.familyNames.sorted()[Int(fontFamilyIndex.x)])
      
                  if Int(fontFamilyIndex.y) >= 0 && Int(fontFamilyIndex.y) < fontNames.count {
      
                      return  fontNames[Int(fontFamilyIndex.y)]
                  }
      
      
                  return fontNames.first ?? nil
      
              }
      
      
      
              return nil
          }
      

      【讨论】:

        猜你喜欢
        • 2012-07-30
        • 1970-01-01
        • 1970-01-01
        • 2018-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多