故事板中包含自定义字体的归因字符串无法正确加载 [英] Attributed string with custom fonts in storyboard does not load correctly

查看:109
本文介绍了故事板中包含自定义字体的归因字符串无法正确加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在项目中使用自定义字体。它在Xcode 5中运行良好。在Xcode 6中,它以纯文本形式工作,在代码中归因于字符串。但是在故事板中设置的那些归因字符串在模拟器或设备上运行时都会恢复到Helvetica,尽管它们在故事板中看起来很好。

We are using custom fonts in our project. It works well in Xcode 5. In Xcode 6, it works in plain text, attributed string in code. But those attributed strings set in storyboard all revert to Helvetica when running on simulator or device, although they look all right in storyboard.

我不确定它是不是一个bug在Xcode 6 / iOS 8中更改了Xcode 6或iOS 8 SDK,或者使用自定义字体的方式?

I'm not sure if it's a bug of Xcode 6 or iOS 8 SDK, or the way to use custom fonts is changed in Xcode 6 / iOS 8?

推荐答案

修复我的是使用 IBDesignable 类:

import UIKit

@IBDesignable class TIFAttributedLabel: UILabel {

    @IBInspectable var fontSize: CGFloat = 13.0

    @IBInspectable var fontFamily: String = "DIN Light"

    override func awakeFromNib() {
        var attrString = NSMutableAttributedString(attributedString: self.attributedText)
        attrString.addAttribute(NSFontAttributeName, value: UIFont(name: self.fontFamily, size: self.fontSize)!, range: NSMakeRange(0, attrString.length))
        self.attributedText = attrString
    }
}

在界面生成器中为您提供:

Giving you this in the Interface Builder:

您可以设置belongsstring就像你一样,但是你必须在新的可用属性中再次设置你的fontsize和fontfamily。

You can set up your attributedstring just as you normal do, but you'll have to set your fontsize and fontfamily once again in the new available properties.

因为Interface Builder正在使用自定义默认情况下,这会产生一个你看到的就是你得到的东西,在构建应用时我更喜欢它。

As the Interface Builder is working with the custom font by default, this results in a what you see is what you get, which I prefer when building apps.

注意

我使用它而不是普通版本的原因是我在属性标签上设置属性,如linespacing,这些属性不可用使用普通样式时。

The reason I'm using this instead of just the plain version is that I'm setting properties on the attributed label like the linespacing, which are not available when using the plain style.

这篇关于故事板中包含自定义字体的归因字符串无法正确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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