2015-03-24 15 views
19

Voglio che il mio UILabel visualizzi il testo nel modo seguente 6.022 * 10 . Quali fumi sono rapidi per pedici e apici?Come utilizzare pedice e apice in Swift

+0

Vedi http://stackoverflow.com/questions/8555735/how-to-make-subscripts-and-superscripts-using-nsattributedstring. –

risposta

45

La maggior parte delle risposte + esempi sono in ObjC, ma questo è come farlo in Swift.

let font:UIFont? = UIFont(name: "Helvetica", size:20) 
let fontSuper:UIFont? = UIFont(name: "Helvetica", size:10) 
let attString:NSMutableAttributedString = NSMutableAttributedString(string: "6.022*1023", attributes: [.font:font!]) 
attString.setAttributes([.font:fontSuper!,.baselineOffset:10], range: NSRange(location:8,length:2)) 
labelVarName.attributedText = attString 

Questo mi dà:

SuperScript Example

In una spiegazione più dettagliata:

  1. Diventa UIFont si vuole sia il default e lo stile apice, apice deve essere più piccolo.
  2. Creare uno NSMutableAttributedString con la stringa completa e il carattere predefinito.
  3. Aggiungere un attributo ai caratteri che si desidera modificare (NSRange), con il più piccolo/pedice UIFont e il valore NSBaselineOffsetAttributeName è l'importo che si desidera compensare verticalmente.
  4. assegnarlo al proprio UILabel

Speriamo che questo aiuta altri sviluppatori Swift come ho bisogno di questo pure.

+0

Risposta perfetta, grazie !! –

+0

Arresta in modo anomalo se Helvetica non è disponibile nelle dimensioni in punti 10 o 20. Anche il codice di esempio deve essere scritto con un minimo di gestione degli errori. Anche i nomi delle variabili lo rendono confuso e non viene compilato. –

5

Se si può andare d'accordo con il testo che non sembra perfetto, e solo bisogno di un sottoinsieme di caratteri che si possono fare uso della apice unicode e numeri pedice: ⁰ ¹ ² ³ ⁴ ⁵ ⁶ ⁷ ⁸ ⁹ ₀ ₁ 2 ₃ ₄ ₅ ₆ ₇ ₈ ₉ Questo ha il vantaggio di essere molto meno ingombrante.

+0

Un altro vantaggio di questo è che quando si hanno più linee, questo non incasina con il linepacing come lo spostamento della linea di base nella stringa attribuita. –

7

Come un approccio diverso, ho scritto una funzione che prende in una stringa in cui gli esponenti sono prefissati con ^ quali 2^2•3•5^2 e restituisce 2²•3•5²

func exponentize(str: String) -> String { 

    let supers = [ 
     "1": "\u{00B9}", 
     "2": "\u{00B2}", 
     "3": "\u{00B3}", 
     "4": "\u{2074}", 
     "5": "\u{2075}", 
     "6": "\u{2076}", 
     "7": "\u{2077}", 
     "8": "\u{2078}", 
     "9": "\u{2079}"] 

    var newStr = "" 
    var isExp = false 
    for (_, char) in str.characters.enumerate() { 
     if char == "^" { 
      isExp = true 
     } else { 
      if isExp { 
       let key = String(char) 
       if supers.keys.contains(key) { 
        newStr.append(Character(supers[key]!)) 
       } else { 
        isExp = false 
        newStr.append(char) 
       } 
      } else { 
       newStr.append(char) 
      } 
     } 
    } 
    return newStr 
} 

E 'un po' di un metodo di forza bruta, ma funziona se non vuoi gestire le string attribuite o vuoi che la tua stringa sia indipendente da un font.

4

Ho scritto la seguente estensione o puoi usarla come una funzione, sta funzionando bene per me. è possibile modificarlo saltando le parti che non sono essenziali per voi

extension NSMutableAttributedString 
{ 
enum scripting : Int 
{ 
    case aSub = -1 
    case aSuper = 1 
} 

func characterSubscriptAndSuperscript(string:String, 
             characters:[Character], 
             type:scripting, 
             fontSize:CGFloat, 
             scriptFontSize:CGFloat, 
             offSet:Int, 
             length:[Int], 
             alignment:NSTextAlignment)-> NSMutableAttributedString 
{ 
    let paraghraphStyle = NSMutableParagraphStyle() 
    // Set The Paragraph aligmnet , you can ignore this part and delet off the function 
    paraghraphStyle.alignment = alignment 

    var scriptedCharaterLocation = Int() 
    //Define the fonts you want to use and sizes 
    let stringFont = UIFont.boldSystemFont(ofSize: fontSize) 
    let scriptFont = UIFont.boldSystemFont(ofSize: scriptFontSize) 
    // Define Attributes of the text body , this part can be removed of the function 
    let attString = NSMutableAttributedString(string:string, attributes: [NSFontAttributeName:stringFont,NSForegroundColorAttributeName:UIColor.black,NSParagraphStyleAttributeName: paraghraphStyle]) 

    // the enum is used here declaring the required offset 
    let baseLineOffset = offSet * type.rawValue 
    // enumerated the main text characters using a for loop 
    for (i,c) in string.characters.enumerated() 
    { 
     // enumerated the array of first characters to subscript 
     for (theLength,aCharacter) in characters.enumerated() 
     { 
      if c == aCharacter 
      { 
       // Get to location of the first character 
       scriptedCharaterLocation = i 
       //Now set attributes starting from the character above  
       attString.setAttributes([NSFontAttributeName:scriptFont, 
       // baseline off set from . the enum i.e. +/- 1   
       NSBaselineOffsetAttributeName:baseLineOffset, 
       NSForegroundColorAttributeName:UIColor.black], 
       // the range from above location 
     range:NSRange(location:scriptedCharaterLocation, 
     // you define the length in the length array 
     // if subscripting at different location 
     // you need to define the length for each one 
     length:length[theLength])) 

      } 
     } 
    } 
    return attString} 
    } 

esempi:

let attStr1 = NSMutableAttributedString().characterSubscriptAndSuperscript(
       string: "23 x 456", 
       characters:["3","5"], 
       type: .aSuper, 
       fontSize: 20, 
       scriptFontSize: 15, 
       offSet: 10, 
       length: [1,2], 
       alignment: .left) 

enter image description here

let attStr2 = NSMutableAttributedString().characterSubscriptAndSuperscript(
      string: "H2SO4", 
      characters: ["2","4"], 
      type: .aSub, 
      fontSize: 20, 
      scriptFontSize: 15, 
      offSet: 8, 
      length: [1,1], 
      alignment: .left) 

enter image description here

+0

Molto bello. L'ho esteso un po 'aggiungendo un parametro 'textColor'. E ha sostituito 'UIFont.boldSystemFont' con' UIFont.systemFont'. – Isuru

+0

Posso vedere la versione estesa @Isuru? –

1

Per un semplice da usare Swift soluzione, tu m ight vuole pagare HandyUIKit. Dopo averlo importato nel tuo progetto (ad es.via Cartagine - vedere le istruzioni in README) si può fare qualcosa di simile:

import HandyUIKit 

"6.022*10^{23}".superscripted(font: UIFont.systemFont(ofSize: 20, weight: .medium)) 

Questa linea restituirà un NSAttributedString che apparire esattamente come quello che stai cercando per. Solo assegnalo a a UILabel s attributedText proprietà e questo è tutto!


Se siete alla ricerca di indicizzazione di un testo, è sufficiente utilizzare subscripted(font:) invece. Riconoscerà strutture come CO_{2}. C'è anche superAndSubscripted(font:) se si desidera combinare entrambi.

Vedere docs per ulteriori informazioni e ulteriori esempi.

+0

questo pod è molto utile. grazie per questo –

0

Ecco una versione semplice che ha la corretta gestione degli errori e verrà compilata nel parco giochi.

import UIKit 

func setMyLabelText(myLabel: UILabel) { 
    if let largeFont = UIFont(name: "Helvetica", size: 20), let superScriptFont = UIFont(name: "Helvetica", size:10) { 
     let numberString = NSMutableAttributedString(string: "6.022*10", attributes: [.font: largeFont]) 
     numberString.append(NSAttributedString(string: "23", attributes: [.font: superScriptFont, .baselineOffset: 10])) 
     myLabel.attributedText = numberString 
    } 
} 

let myLabel = UILabel() 
setMyLabelText(myLabel: myLabel) 
Problemi correlati