Swift 3.1-NSAttributedString中的NSSuperScript无法正常工作 [英] Swift 3.1 - NSSuperScript in NSAttributedString not working as expected

查看:138
本文介绍了Swift 3.1-NSAttributedString中的NSSuperScript无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的应用程序在通过XCode 8.3 beta 2与iOS 10.3 Simulator进行测试时遇到问题,其中AttributedString中的上标与普通文本显示在同一行上.对于iOS 10.2.x及更低版本,它可以正确显示.

The application I am working on encountered an issue when testing with iOS 10.3 Simulator via XCode 8.3 beta 2, where the superscript in AttributedString displayed on the same line with normal text. For iOS 10.2.x and below, it is displaying correctly.

iOS 10.3截图: https://www.dropbox.com/s/p5v71g722cg5qhy/Screen%20Shot%202017-02-21%20at%2010.24.21%20AM.png?dl=0

iOS 10.3 screenshot: 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/Scr​​een%20Shot%202017-02-21%20at%2010.19.17%20AM.png?dl=0

iOS 10.2.x and below screenshot: https://www.dropbox.com/s/lcfsic6xyz953qp/Screen%20Shot%202017-02-21%20at%2010.19.17%20AM.png?dl=0

这是我处理文字的方式:

Here's how I handled the text:

  • 最初,该字符串为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描述:
  • The NSAttributedString then will be rendered via UILabel.attributedText This is the attributedText description:

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;  
}

注意:我认为该问题与我们的自定义字体有关,但是当我们使用默认字体时,该问题仍然会发生.

Note: I thought the issue was related to our custom font, but the issue still happen when we use the default font.

这是与Swift 3.1相关的问题,应该解决吗?

Is this an issue related with Swift 3.1 and should be fixed?

推荐答案

我们发现,这是iOS 10.3中UILabel属性文本渲染的问题,而不是与Swift 3.1相关的问题.影响我们的删除线样式.

We found that it's an issue in UILabel's attributed text rendering in iOS 10.3, not Swift 3.1-related. Affected strike-through style for us.

对于我们的特定情况(我们在NSAttributedString中指定了所有属性,而不使用UILabel的属性),这是解决方案:

For our specific scenario (we have all attributes specified in NSAttributedString, not using UILabel's properties) this is the solution:

/// 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)
        }
    }
}

这篇关于Swift 3.1-NSAttributedString中的NSSuperScript无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆