2010-10-12 12 views
5

Sto usando quanto segue per rendere il testo in un UIView.iPad/iPhone - NSString drawInRect non word wrapping

- (void) drawRect:(CGRect)rect 
{ 

    NSString* text = @"asdf asdf asdf asdf asdf asdf asdf"; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]); 

    CGContextFillRect(context, rect); 

    CGContextSetTextDrawingMode(context, kCGTextFillStrokeClip); 

    CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]); 

    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]); 

    CGContextSetShouldSmoothFonts(context, YES); 

    UIFont* font = [UIFont fontWithName:@"ArialRoundedMTBold" size:20.0f]; 

    CGSize textMaxSize = CGSizeMake(rect.size.width - 20.0f, rect.size.height); 

    CGSize textSize = [text sizeWithFont:font constrainedToSize:textMaxSize lineBreakMode:UILineBreakModeWordWrap]; 

    CGRect textRect = CGRectMake(10.0f, 10.0f, textSize.width, textSize.height); 

    [text drawInRect:textRect withFont:font]; 

    [text drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeWordWrap]; 

    [text drawInRect:textRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter]; 
} 

Nessuno dei [text drawInRect] 's avvolgere il testo come mi aspetto. TextSize è calcolato correttamente ma l'estrazione è solo il rendering di una singola riga.

Aggiornamento:

Risolto.

Le impostazioni CGContextSetTextDrawingMode(context, kCGTextFillStrokeClip); causano il testo da ritagliare indipendentemente dalla modalità UILineBreak selezionata. L'impostazione CGContextSetTextDrawingMode(context, kCGTextFillStroke); ha risolto questo problema.

+1

Ha funzionato alla grande! – user523234

risposta

0

risolto.

Impostazioni CGContextSetTextDrawingMode (context, kCGTextFillStrokeClip); farà sì che il testo si agganci indipendentemente dalla modalità UILineBreak selezionata. Impostazione CGContextSetTextDrawingMode (context, kCGTextFillStroke); risolto questo problema.

Problemi correlati