我正在尝试为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
}
}
当我在“界面生成器”中进行任何更改时,框架似乎保持不变,但是图标本身却按照我的意愿绘制得更大。
我认为从self.sizeToFit()
调用prepareForInterfaceBuilder()
应该调整视图的大小并更改15x18
的尺寸,但这似乎不是。
答案 0 :(得分:1)
我认为任何与尺寸有关的调整都应放在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
}
}