拉伸和字距型,不能在带有@IBDesignable的Storyboard中工作

这是一个跟踪/拉伸字体的IBLabel

它在构build中完美地工作。 但是这个改变不会在Storyboard中显示。

 // UILabel, but you can set // the tracking (that's the overall amount of space between all letters) // and streching (actually squeeze or stretch the letters horizontally) // Note: it's very common that typographers need you to adjust these. import UIKit @IBDesignable class StyledLabel: UILabel { @IBInspectable var tracking:CGFloat = 0.8 // values between about 0.7 to 1.3. one means normal. @IBInspectable var stretching:CGFloat = -0.1 // values between about -.5 to .5. zero means normal. override func awakeFromNib() { let ats = NSMutableAttributedString(string: self.text!) let rg = NSRange(location: 0, length: self.text!.characters.count) ats.addAttribute( NSKernAttributeName, value:CGFloat(tracking), range:rg ) ats.addAttribute( NSExpansionAttributeName, value:CGFloat(stretching), range:rg ) self.attributedText = ats } } 

在右边的模拟器工作完美。

在这里输入图像说明

实际上不会在Storyboard上显示(请参阅左侧)。

疯狂的猜测,我错过了一个初始化函数?

或者问题是什么?


注 – 设置字体大小以适应高度:

您可能需要设置字体大小以填充所有设备上的标签框。 为了节省哟打字这里有一个类,“指向高度”,跟踪和拉伸: https : //stackoverflow.com/a/372​​77874/294884

你也应该把你的代码放在prepareForInterfaceBuilder()里面。 它只在接口生成器中调用,而不是在运行时调用。

 override func prepareForInterfaceBuilder() { let ats = NSMutableAttributedString(string: self.text!) let rg = NSRange(location: 0, length: self.text!.characters.count) ats.addAttribute( NSKernAttributeName, value:CGFloat(tracking), range:rg ) ats.addAttribute( NSExpansionAttributeName, value:CGFloat(stretching), range:rg ) self.attributedText = ats }