我正在处理的应用程序在通过XCode 8.3 beta 2使用iOS 10.3 Simulator进行测试时遇到问题,其中AttributedString中的上标显示在与普通文本相同的行上。对于iOS 10.2.x及更低版本,它正确显示。
iOS 10.3截图: https://www.dropbox.com/s/p5v71g722cg5qhy/Screen%20Shot%202017-02-21%20at%2010.24.21%20AM.png?dl=0
iOS 10.2.x及以下截图: https://www.dropbox.com/s/lcfsic6xyz953qp/Screen%20Shot%202017-02-21%20at%2010.19.17%20AM.png?dl=0
以下是我处理文字的方式:
<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>
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)
}
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相关的问题,应该修复吗?
答案 0 :(得分:1)
我们发现UILabel在iOS 10.3中归因于文本渲染,而不是与Swift 3.1相关的问题。我们受影响的罢工风格。
对于我们的特定场景(我们在NSAttributedString
中指定了所有属性,而不是使用UILabel的属性),这就是解决方案:
/// This UILabel subclass accomodates conditional fix for NSAttributedString rendering broken by Apple in iOS 10.3
final class PriceLabel: UILabel {
override func drawText(in rect: CGRect) {
guard let attributedText = attributedText else {
super.drawText(in: rect)
return
}
if #available(iOS 10.3, *) {
attributedText.draw(in: rect)
} else {
super.drawText(in: rect)
}
}
}