我正在使用的应用程序在通过XCode 8.3 beta 2测试iOS 10.3 Simulator时遇到问题,其中AttributedString中的上标显示在与普通文本相同的行上。对于iOS 10.2.x及更低版本,它显示正确。Swift 3.1 - NSAttributedString中的NSSuperScript未按预期工作
iOS的10.3截图: https://www.dropbox.com/s/p5v71g722cg5qhy/Screen%20Shot%202017-02-21%20at%2010.24.21%20AM.png?dl=0
的iOS 0.2.x中遇到和下面的截图: https://www.dropbox.com/s/lcfsic6xyz953qp/Screen%20Shot%202017-02-21%20at%2010.19.17%20AM.png?dl=0
以下是我处理的文本:
- 最初,该字符串是HTML格式,并标记文字“8,9”以上
<html>
<head>
<style> body { color: #554344 ; font-family: \'MyCustomFont\'; font-size: 18px; } sup { font-size: 13px; } </style>
</head>
<body>
ABCdef<sup>1,2,3,8,9</sup>
</body>
</html>
- HTML字符串然后转化成NSAttributedString使用以下脚本
private func convertHTMLToAttributedString(string: String) -> NSAttributedString {
guard let data = string.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return NSAttributedString() }
return try! NSAttributedString(
data: data,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
}
- 的NSAttributedString然后将被渲染六一个UILabel.attributedText 这是attributedText描述:
1,2,3,8,9{
NSColor = "kCGColorSpaceModelRGB 0.333333 0.262745 0.266667 1 ";
NSFont = "<UICTFont: 0x7fed0a469880> font-family: \"MyCustomFont\"; font-weight: normal; font-style: normal; font-size: 10.00pt";
NSKern = 0;
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 13/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
NSStrokeColor = "kCGColorSpaceModelRGB 0.333333 0.262745 0.266667 1 ";
NSStrokeWidth = 0;
NSSuperScript = 1;
}
注:我认为问题是关系到我们自定义的字体,但是当我们使用默认字体的问题还是发生了。
这是一个与Swift 3.1相关的问题,应该修复吗?
最近我发现同样的效果('NSSuperScript'似乎没有任何效果)内一个macOS应用程序。 – ixany