2016-03-27 12 views
9

Ho bisogno di calcolare la dimensione del fotogramma per il testo che potrebbe essere in inglese/emoji/altre lingue.
Il modo in cui ho fatto è stato:Calcola altezza UILabel per il testo con diverse lingue ed emoji in Objective-C

NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
[style setLineBreakMode:NSLineBreakByWordWrapping]; 
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize], NSParagraphStyleAttributeName: style}; 
CGSize rect = [text boundingRectWithSize:CGSizeMake(containerWidth, CGFLOAT_MAX) 
           options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading 
          attributes:attributes 
           context:nil].size; 

Funziona bene finché io non uso emoji. Sembra che l'altezza della linea emoji/la dimensione del carattere sia diversa dai normali caratteri.

ho visto che la gente ha cercato di risolvere in modi diversi, quali: http://youbbe.xyz/issue/4987325/ios-emoji-messed-up-in-uilabel

questa soluzione funziona per emoji, ma non per l'inglese/altri tipi di carattere.

Vedere l'esempio di immagine:
enter image description here Come si può vedere, solo il testo viene calcolato bene (prima voce), ma di testo + emoji non è calcualted correttamente (secondo riquadro).

Sono davvero sorpreso che non ci sia una soluzione facile per questo problema. Il tuo aiuto è molto apprezzato.

+1

provare Con Di seguito la funzione, ma è necessario utilizzare Invia testo attribuito e la larghezza del TextVew e la funzione restituisce un'altezza di TextView '- (CGFloat) textViewHeightForAttributedText: (NSAttributedString *) Testo andWidth: (CGFloat) larghezza { UITextView * textView = [[UITextView alloc] init]; [textView setAttributedText: text]; Dimensioni CGSize = [dimensione del testo ViewThatFits: CGSizeMake (larghezza, FLT_MAX)]; //textView.textContainerInset = UIEdgeInsetsZero; return size.height + 10; } ' – Vvk

+0

controlla questo .. [articolo] (http://www.appcoda.com/self-sizing-cells/) – mustafa96m

+0

Che cos'è' containerWidth'? Dove lo stai ambientando? Che aspetto ha il codice in realtà imposta la cornice dell'etichetta? – beyowulf

risposta

4

Con un'UILabel si può semplicemente dire:

CGSize size = [label sizeThatFits:CGSizeMake(containerWidth, CGFLOAT_MAX)]; 

Hai provato questo? Non penso che le emoji abbiano un'altezza di linea diversa, ma in genere occupano l'intera altezza della linea.

Si può semplicemente dire [label sizeToFit]; e quindi ottenere la dimensione dal frame (ad esempio CGSize size = label.frame.size;). Questo sembra il modo più coerente per ottenere le giuste dimensioni per le diverse dimensioni dei caratteri.

C'è del codice here che è possibile copiare e incollare in un parco giochi e vedere i diversi risultati.

+0

L'ho provato ma non funziona ... Vedo che molte persone rispondono allo stesso modo ma niente che funzioni davvero ... –

+0

Dovresti mostrare cosa stai ricevendo e cosa ti aspetti. – beyowulf

+0

esempio allegato. –

0

Convertire la stringa in un NSAttributedString quindi utilizzare questa categoria:

- (CGFloat)heightForAttributedStringWithEmojisForWidth:(CGFloat)width { 
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)self); 
    CFRange fitRange; 
    CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [self length]), NULL, CGSizeMake(width, CGFLOAT_MAX), &fitRange); 
    CFRelease(framesetter); 
    return ceilf(frameSize.height); 
} 
0
//may be it will help for you 
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; 
    //set the line break mode 
    paragraphStyle.lineBreakMode = yourLabel.lineBreakMode; 

    NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:yourLabel.font, 
           NSFontAttributeName, 
           paragraphStyle, 
           NSParagraphStyleAttributeName, 
           nil]; 


    CGRect rect = [yourLabel.text boundingRectWithSize:CGSizeMake(yourLabel.frame.size.width, yourLabel) 
                options:NSStringDrawingUsesLineFragmentOrigin 
               attributes:attrDict 
                context:nil]; 
    CGSize size = rect.size; 
+0

come menzionato ad altre persone ha risposto qui. Questa soluzione non funziona. –

0
+ (CGFloat)heightForText:(NSString *)text { 

    CGFloat offset = 5.f; 

    UIFont *font = [UIFont systemFontOfSize:textHeight]; 
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init]; 
    paragraph.lineBreakMode = NSLineBreakByWordWrapping; 
    paragraph.alignment = NSTextAlignmentCenter; 
    NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys: 
          font, NSFontAttributeName, 
          paragraph, NSParagraphStyleAttributeName, nil]; 

    //size of one field 
    CGSize textSize = [text sizeWithAttributes:attributes]; 

    CGRect textRect = [text boundingRectWithSize:CGSizeMake(320.f - offset * 2.f, CGFLOAT_MAX) 
            options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading 
            attributes:attributes 
            context:nil]; 

    return CGRectGetHeight(textRect); 

} 
1

Una possibile soluzione è impostato il testo con la stringa di attributo e specificare lo stile di paragrafo

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
paragraphStyle.lineSpacing = 5; 
paragraphStyle.lineHeightMultiple = 1.1; 
paragraphStyle.maximumLineHeight = 16; 

maximumLineHeight può limite altezza della linea che ha emoji in essa.

Problemi correlati