我正在尝试为Font Awesome创建一个UILabel,到目前为止似乎效果很好。我遇到的问题是,当我更改prepareForInterfaceBuilder()中标签的字体大小时,它没有反映在Interface Builder中。

这是我的代码:

import Foundation
import UIKit

@IBDesignable
class FALabel : UILabel {

    @IBInspectable
    private var icon: String = ""

    @IBInspectable
    private var weight: NSInteger = 0

    /**
     * The type for the Font Awesome Label
     */
    enum FAType {
        case Brands
        case Light
        case Regular
        case Solid
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }

    /**
     * Prepare the view or the Interface Builder.
     */
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setupView()
    }

    /**
     * Set up the view.
     */
    func setupView() {
        let type: FAType =  {
            switch self.weight {
            case 0:  return .Light
            case 2:  return .Solid
            case 3:  return .Brands
            default: return .Regular
            }
        }()

        setFontAwesomeIcon(faType: type, size: self.font.pointSize, icon: FAIcons.iconNamed(name: self.icon))
        self.sizeToFit()
        self.frame = CGRect(x: self.frame.minX, y:self.frame.minY, width:self.frame.width, height:self.frame.height)
        self.invalidateIntrinsicContentSize()
    }

    /**
     * Calculate the intrinsic content size.
     */
    override var intrinsicContentSize: CGSize {
        return CGSize(width: self.frame.width, height: self.frame.height)
    }

    /**
     * Set the Font Awesome Icon for the label.
     *
     * - parameter fatype: The type to set.
     * - parameter size:   The size for the font.
     * - parameter icon:   The icon to use.
     */
    func setFontAwesomeIcon(faType: FAType, size: CGFloat, icon:String) {
        if faType == .Regular {
            self.font = UIFont(name: "FontAwesome5Pro-Regular", size: size)
        } else if faType == .Solid {
            self.font = UIFont(name: "FontAwesome5Pro-Solid", size: size)
        } else if faType == .Light {
            self.font = UIFont(name: "FontAwesome5Pro-Light", size: size)
        } else if faType == .Brands {
            self.font = UIFont(name: "FontAwesome5Brands-Regular", size: size)
        }
        self.text = icon
    }
}


当我在Interface Builder中更改任何内容时,框架似乎保持不变,但是图标本身却按我的意愿绘制得更大。

ios - 自定义UILabel View 未在Interface Builder中更新其框架-LMLPHP

我以为从self.sizeToFit()调用prepareForInterfaceBuilder()应该调整视图的大小并更改这些15x18的尺寸,但事实并非如此。

最佳答案

我认为任何与尺寸相关的调整都应放在layoutSubviews中。

override func layoutSubviews() {
   super.layoutSubviews()
   setupView()
}

您可能使用prepareForInterfaceBuilder填充了一些样本数据,例如在IB中预览,但您不应该回想起已经在init方法中进行过的设置(例如setupView())。

您还可以省略我猜得到的init方法,并将所有内容简化为:
import Foundation
import UIKit

@IBDesignable
class FALabel : UILabel {

    @IBInspectable
    public var icon: String = "" {
        didSet { setupView() }
    }

    @IBInspectable
    public var weight: NSInteger = 0 {
        didSet { setupView() }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        setupView()
    }

    /**
     * The type for the Font Awesome Label
     */
    enum FAType {
        case Brands
        case Light
        case Regular
        case Solid
    }

    /**
     * Set up the view.
     */
    func setupView() {
        let type: FAType =  {
            switch self.weight {
            case 0:  return .Light
            case 2:  return .Solid
            case 3:  return .Brands
            default: return .Regular
            }
        }()

        setFontAwesomeIcon(faType: type, size: self.font.pointSize, icon: FAIcons.iconNamed(name: self.icon))
        self.sizeToFit()
        self.frame = CGRect(x: self.frame.minX, y:self.frame.minY, width:self.frame.width, height:self.frame.height)
        self.invalidateIntrinsicContentSize()
    }

    /**
     * Calculate the intrinsic content size.
     */
    override var intrinsicContentSize: CGSize {
        return CGSize(width: self.frame.width, height: self.frame.height)
    }

    /**
     * Set the Font Awesome Icon for the label.
     *
     * - parameter fatype: The type to set.
     * - parameter size:   The size for the font.
     * - parameter icon:   The icon to use.
     */
    func setFontAwesomeIcon(faType: FAType, size: CGFloat, icon:String) {
        if faType == .Regular {
            self.font = UIFont(name: "FontAwesome5Pro-Regular", size: size)
        } else if faType == .Solid {
            self.font = UIFont(name: "FontAwesome5Pro-Solid", size: size)
        } else if faType == .Light {
            self.font = UIFont(name: "FontAwesome5Pro-Light", size: size)
        } else if faType == .Brands {
            self.font = UIFont(name: "FontAwesome5Brands-Regular", size: size)
        }
        self.text = icon
    }
}

关于ios - 自定义UILabel View 未在Interface Builder中更新其框架,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58384681/

10-12 04:37