拉伸和字距调整类型,不能在带有@IBDesignable的Storyboard中工作 [英] Stretching and kerning type, not working in Storyboard with @IBDesignable

查看:116
本文介绍了拉伸和字距调整类型,不能在带有@IBDesignable的Storyboard中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is a IBLabel which tracks / stretches the font.

它在构建中完美运行。但是这个改变并没有在Storyboard中显示。

It works perfectly in the build. But the change doesn't show live in 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
        }
    }

右边的模拟器完美无缺。

Simulator on the right works perfect.

实际上并没有在故事板上显示直播(见左图)。

Does not actually show live on Storyboard (See on left).

狂野猜测,我是缺少初始化函数?

Wild guess, am I missing an initialization func?

或者问题是什么?

注意 - 设置字体siz e适合身高:

Note - set font size to fit height:

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

You may want to set the font size to fill the label frame on all devices. To save yo typing here's a class that does "point for height", tracking and stretching: https://stackoverflow.com/a/37277874/294884

推荐答案

你还应该把你的代码放在 prepareForInterfaceBuilder()。它仅在接口构建器中调用,而不是在运行时调用。

You should also put your code inside prepareForInterfaceBuilder(). It's called only in interface builder and not at runtime.

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
}

这篇关于拉伸和字距调整类型,不能在带有@IBDesignable的Storyboard中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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