Używam następujących elementów do renderowania tekstu w UIView
.iPad/iPhone - NSString drawInRect nie pakuje słów
- (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];
}
Żadne z [text drawInRect]
nie owija tekstu tak jak oczekuję. TextSize
jest obliczane poprawnie, ale losowanie renderuje tylko jedną linię.
Aktualizacja:
rozwiązany.
Ustawienia CGContextSetTextDrawingMode(context, kCGTextFillStrokeClip);
spowoduje, że tekst zostanie przycięty niezależnie od wybranego trybu UILineBreak
. Ustawienie CGContextSetTextDrawingMode(context, kCGTextFillStroke);
rozwiązało ten problem.
Udało się! – user523234