2014-06-19 10 views
9

Come visualizzare il carattere apice% come stringa in UIlabel? So che% non esiste in unicode come apice, ma esiste un modo in cui possiamo mostrare% come apice invece di usare i tag html ??Come visualizzare il carattere apice% come stringa in UIlabel?

+2

Questo articolo potrebbe fare ciò che desideri: http://sketchytech.blogspot.co.uk/2013/12/drawing-nsattributostrings-with.html – trojanfoe

+0

Grazie per aver condiviso queste informazioni. Ora sono in grado di visualizzare la% come apice per l'etichetta usando quell'articolo –

risposta

17

ho trovato questo post su StackOverflow su testo styling apice con stringa attribuito:

NSAttributedString superscript styling

Quindi, utilizzando questo, ho inciso su questa demo:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UIFont *font = [UIFont fontWithName:@"Helvetica" size:20]; 

    UILabel *textBlock1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height/2.0)]; 
    textBlock1.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]; 
    textBlock1.textAlignment = NSTextAlignmentCenter; 
    textBlock1.font = font; 

    textBlock1.text = @"57%"; 





    UILabel *textBlock2 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height/2.0, self.view.bounds.size.width, self.view.bounds.size.height/2.0)]; 
    textBlock2.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]; 
    textBlock2.textAlignment = NSTextAlignmentCenter; 

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"57%" 
                         attributes:@{NSFontAttributeName: font}]; 
    [attributedString setAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:10] 
             , NSBaselineOffsetAttributeName : @10} range:NSMakeRange(2, 1)]; 

    textBlock2.attributedText = attributedString; 



    [self.view addSubview:textBlock1]; 
    [self.view addSubview:textBlock2]; 
} 

Il risultato:

enter image description here

+0

grazie per aver condiviso la fonte. Ho raggiunto lo stesso usando il tutorial http://sketchytech.blogspot.co.uk/2013/12/drawing-nsattributostrings-with.html suggerito da trojanfoe. –

+0

@ Zhang Riesci a trasformare una piccola traduzione in Swift? Poiché alcuni comandi sono stati modificati e io cerco di archiviare lo stesso. Grazie – Nicholas

+0

@Nicholas è praticamente la stessa cosa in Swift, non sono sicuro che cosa intendi con alcuni comandi, ma è una bella copia incolla/conversione traduzione .... – TheCodingArt

1

Per un semplice da usare soluzione Swift, si potrebbe voler controllare HandyUIKit. Dopo aver importato nel progetto (ad esempio via Cartagine - vedere le istruzioni in README) si può fare qualcosa di simile:

import HandyUIKit 

"57^{%}".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.

Problemi correlati