1

L'application sur laquelle je travaille a rencontré un problème lors du test avec iOS 10.3 Simulator via XCode 8.3 beta 2, où l'exposant dans AttributedString affiché sur la même ligne avec du texte normal. Pour iOS 10.2.x et ci-dessous, il s'affiche correctement.Swift 3.1 - NSSuperScript dans NSAttributedString ne fonctionne pas comme prévu

iOS 10.3 capture d'écran: https://www.dropbox.com/s/p5v71g722cg5qhy/Screen%20Shot%202017-02-21%20at%2010.24.21%20AM.png?dl=0

iOS 10.2.x et au-dessous capture d'écran: https://www.dropbox.com/s/lcfsic6xyz953qp/Screen%20Shot%202017-02-21%20at%2010.19.17%20AM.png?dl=0

Voici comment je le texte manipulé:

  • Dans un premier temps, la chaîne est au format HTML, et tag pour le texte "8,9" ci-dessus

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

  • La chaîne html puis convertie en NSAttributedString en utilisant le script suivant

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

  • Le NSAttributedString sera ensuite rendu vi un UILabel.attributedText Ceci est la description de 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; 
 
}

Note: Je pensais que la question était liée à notre police personnalisée, mais la question arrive toujours lorsque nous utilisons la police par défaut .

Est-ce un problème lié à Swift 3.1 et devrait être corrigé?

+0

J'ai découvert récemment le même effet ('NSSuperScript' semble avoir aucun effet) à l'intérieur une application macOS. – ixany

Répondre

1

Nous avons trouvé que c'est un problème dans le rendu de texte attribué par UILabel dans iOS 10.3, pas dans Swift 3.1. Style de traversée affecté pour nous.

Pour notre scénario spécifique (nous avons tous les attributs spécifiés dans NSAttributedString, ne pas utiliser les propriétés de UILabel) c'est la 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) 
     } 
    } 
} 
+0

Même si mon label n'a que la propriété 'text',' attributeText' n'est pas nul (il contient la même chaîne, plus les attributs de 'NSColor',' NSFont' et 'NSParagraphStyle'). Par conséquent, l'instruction guard dans le code ci-dessus renvoie toujours true. – Koen

+0

@Koen ce n'est pas garanti d'être non-nul cependant. Qui sait ce qu'ils pourraient changer dans la version iOS suivante ou si elle était non-pré-iOS 10. – rshev

+0

Ouais, bon point. Toujours intéressant pourquoi 'attributeText' est défini quand seul' text' est défini. Mais cela sort du cadre de cette question. – Koen