【问题标题】:Swift 3.1 - NSSuperScript in NSAttributedString not working as expectedSwift 3.1 - NSAttributedString 中的 NSSuperScript 没有按预期工作
【发布时间】:2017-07-17 15:36:03
【问题描述】:

我正在开发的应用程序在通过 XCode 8.3 beta 2 使用 iOS 10.3 模拟器进行测试时遇到问题,其中 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 格式,上面是文本“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 呈现 这是属性文本描述:

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 相关的问题吗?应该修复吗?

【问题讨论】:

  • 我最近在一个 macOS 应用中发现了同样的效果(NSSuperScript 似乎没有效果)。

标签: swift3 nsattributedstring superscript xcode8-beta6 ios10.3


【解决方案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)
        }
    }
}

【讨论】:

  • 即使我的标签只设置了text 属性,attributedText 也不为零(它包含相同的字符串,以及NSColorNSFontNSParagraphStyle 的属性) .因此上面代码中的guard语句总是返回true。
  • @Koen 虽然不能保证它是非零的。谁知道他们在随后的 iOS 版本中可能会发生什么变化,或者它是否是非零的 iOS 10 之前的版本。
  • 是的,好点。仍然很有趣为什么只设置了text 时设置了attributedText。但这超出了这个问题的范围。
猜你喜欢
  • 2019-01-30
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
相关资源
最近更新 更多