首页 > 解决方案 > 有什么方法可以为 iOS 10 及更高版本将字距设置为 SKLabelNode?

问题描述

我想将字距设置为SKLabelNode. 我使用NSAttributedString了这个解决方案,并且在 iOS 11 或更高版本上运行良好。

但我也需要支持 iOS 10。有没有办法设置支持 iOS 10 及更高版本的字距调整?

class Caption: SKLabelNode {

    static func addNode(at position: CGPoint, isBold: Bool, text: String) -> Caption {

        let font = isBold ? "Roboto-Bold" : "Roboto-Regular"

        let attr: [NSAttributedString.Key: Any] = [
            .font: UIFont(name: font, size: 16)!,
            .foregroundColor: SKColor.white,
            .kern: 2.0
        ]

        let caption = Caption(attributedText: NSAttributedString(string: text.uppercased(), attributes: attr))
        caption.horizontalAlignmentMode = .center
        caption.verticalAlignmentMode = .top
        caption.position = position   
        return caption
    }
}

Xcode 说:'init(attributedText:)' is only available on iOS 11 or newer.

let caption = Caption(attributedText: NSAttributedString(string: text.uppercased(), attributes: attr))

标签: swiftsprite-kit

解决方案


答案是不。您将不得不想出一种不同的方法,例如使用 UILabel 渲染,从中提取 UIImage,在 SKSpriteNode 上使用 UIImage。

当然,最简单的解决方案就是放弃对 iOS 10 的支持(大约 5% 的 iOS 用户使用 iOS 10)


推荐阅读