Tôi đang sử dụng như sau để hiển thị một số văn bản trong một UIView
.iPad/iPhone - NSString drawInRect không phải gói từ
- (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];
}
Không có văn bản nào giống như tôi mong đợi. TextSize
được tính toán chính xác nhưng vẽ chỉ hiển thị một dòng.
Cập nhật:
Đã giải quyết.
Cài đặt CGContextSetTextDrawingMode(context, kCGTextFillStrokeClip);
sẽ khiến đoạn văn bản bị cắt bất kể chế độ UILineBreak
được chọn. Cài đặt CGContextSetTextDrawingMode(context, kCGTextFillStroke);
đã giải quyết được sự cố này.
Nó hoạt động rất tốt! – user523234