2017-02-27 40 views
1

我正在使用的应用程序在通过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相关的问题,应该修复吗?

+0

最近我发现同样的效果('NSSuperScript'似乎没有任何效果)内一个macOS应用程序。 – ixany

回答

1

我们发现这是iOS 10.3中UILabel的归属文本渲染问题,与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) 
     } 
    } 
} 
+0

即使我的标签只设置了text属性,属性文字不是零(它包含相同的字符串,加上NSColor,NSFont和NSParagraphStyle的属性)。因此上述代码中的guard语句总是返回true。 – Koen

+0

@Koen它不能保证不是零。谁知道他们可能会在随后的iOS版本中更改什么,或者它是否为iOS之前的非零版本。 – rshev

+0

是的,好点。仍然有趣的是,为什么只有'text'被设置时才会设置'attributedText'。但这超出了这个问题的范围。 – Koen