2010-10-12 20 views
5

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.

+1

Nó hoạt động rất tốt! – user523234

Trả lời

0

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. Thiết lập CGContextSetTextDrawingMode (context, kCGTextFillStroke); giải quyết vấn đề này.

Các vấn đề liên quan