表情符号支持NSAttributedString属性(字距调整/段落样式)

时间:2018-03-30 14:52:28

标签: ios swift unicode nsattributedstring

我在UILabel上使用kerning属性来显示带有一些自定义字母间距的文本。不幸的是,当我显示用户生成的字符串时,我有时会看到以下内容:

/Users/mick/Desktop/Capture d’écran 2018-03-30 à 16.45.55.png

即有时候没有显示一些表情符号字符。

如果我注释掉字距但是应用了一些段落样式,我会得到同样的错误渲染。

我在文档中找不到任何明确拒绝支持特殊unicode字符的内容。我做错了什么还是iOS错误?

重现错误的代码可在此处作为游乐场使用:https://github.com/Bootstragram/Playgrounds/tree/master/LabelWithEmoji.playground

并复制到这里:

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

extension NSAttributedString {
    static func kernedSpacedText(_ text: String,
                                    letterSpacing: CGFloat = 0.0,
                                    lineHeight: CGFloat? = nil) -> NSAttributedString {
        // TODO add the font attribute

        let attributedString = NSMutableAttributedString(string: text)
        attributedString.addAttribute(NSAttributedStringKey.kern,
                                      value: letterSpacing,
                                      range: NSRange(location: 0, length: text.count))

        if let lineHeight = lineHeight {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = lineHeight

            attributedString.addAttribute(NSAttributedStringKey.paragraphStyle,
                                          value: paragraphStyle,
                                          range: NSRange(location: 0, length: text.count))
        }

        return attributedString
    }
}

//for familyName in UIFont.familyNames {
//    for fontName in UIFont.fontNames(forFamilyName: familyName) {
//        print(fontName)
//    }
//}

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let myString = "1⚽⚾️‍♂️\n2  "

        let label = UILabel()
        label.frame = CGRect(x: 150, y: 200, width: 200, height: 100)
        label.attributedText = NSAttributedString.kernedSpacedText(myString)
        label.numberOfLines = 0
        label.textColor = .black

        view.addSubview(label)
        self.view = view
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

感谢。

1 个答案:

答案 0 :(得分:4)

TL,DR:

String.count != NSString.length。只要您看到NSRange,就必须将String转换为UTF-16:

static func kernedSpacedText(_ text: String,
                                letterSpacing: CGFloat = 0.0,
                                lineHeight: CGFloat? = nil) -> NSAttributedString {
    // TODO add the font attribute

    let attributedString = NSMutableAttributedString(string: text)
    attributedString.addAttribute(NSAttributedStringKey.kern,
                                  value: letterSpacing,
                                  range: NSRange(location: 0, length: text.utf16.count))

    if let lineHeight = lineHeight {
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineHeight

        attributedString.addAttribute(NSAttributedStringKey.paragraphStyle,
                                      value: paragraphStyle,
                                      range: NSRange(location: 0, length: text.utf16.count))
    }

    return attributedString
}

更长的解释

你的常见问题是在Swift的String和ObjC的NSString之间进行转换。 String的长度是扩展字形集群的数量;在ObjC中,它是编码该字符串所需的UTF-16代码点数。

以拇指向上的角色为例:

let str = ""
let nsStr = str as NSString

print(str.count)    // 1
print(nsStr.length) // 2

当谈到标志表情符号时,事情会变得更加奇怪:

let str = ""
let nsStr = str as NSString

print(str.count)    // 1
print(nsStr.length) // 4    

虽然这篇文章是在2003年写成的,但它今天仍然是一本很好的读物: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets