ios NSMutableAttributedString NSKernAttributeName 字距错误 "fl" 和 "fi" 字母

ios NSMutableAttributedString NSKernAttributeName wrong kerning with "fl" and "fi" letters

我在标签上使用 attributed strings 来更改字母之间的间距:

var attributedString=NSMutableAttributedString(string: "Fifi floflo")
attributedString.addAttribute(NSKernAttributeName, value: CGFloat(10), range: NSRange(location: 0, length: attributedString.length))
label.attributedText=attributedString

除了两组字母“fl”和“fi”(也许其他我没有'看到的时刻)。 我正在使用 "Avenir-medium" 字体,但我使用基本的 Arial 字体进行了测试,但问题仍然存在。

NSAttributedStrings 默认启用连字。连字将悬垂字符组合成单个字形。如果您正在应用字母间距,则需要禁用它们 - 否则这些字母对字形将不会分开。

attributedString.addAttribute(NSLigatureAttributeName, 
  value:0, 
  range: NSRange(location: 0, length: attributedString.length))

来自文档:

NSLigatureAttributeName

The value of this attribute is an NSNumber object containing an integer. Ligatures cause specific character combinations to be rendered using a single custom glyph that corresponds to those characters. The value 0 indicates no ligatures. The value 1 indicates the use of the default ligatures. The value 2 indicates the use of all ligatures. The default value for this attribute is 1. (Value 2 is unsupported on iOS.)