views:

92

answers:

1

I'm using the following to render some text in a UIView.

- (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];
}

None of the [text drawInRect]'s wrap the text like I expect. TextSize is computed properly but the draw is just rendering a single line.

Update:

Solved.

Settings CGContextSetTextDrawingMode(context, kCGTextFillStrokeClip); will cause the text to clip regardless of UILineBreak mode selected. Setting CGContextSetTextDrawingMode(context, kCGTextFillStroke); solved this issue.

A: 

Its possible you don't have the vertical size in your textRect. Check out this similar question:

http://stackoverflow.com/questions/419222/how-do-i-get-nsstring-sizewithfontforwidthlinebreakmode-to-work

Ben
If I log the height and width of textRect I get: width: 255.000000, height: 96.000000
Broonix
Which is correct.
Broonix
The rect it is drawing into is 300.00 by 300.00
Broonix
Doesn't sound like thats a problem then. You can set a background color to textRect to make sure its all being drawn, but it sounds like its just not wrapping for some reason. What happens when you use the constrain to size version of the method?
Ben
When I fill the rect it does cover the entire 255x96 but the text does not wrap. I am using 'constrainToSize' since 'forWidth' will not wrap text. CGSize textSize = [text sizeWithFont:font constrainedToSize:textMaxSize lineBreakMode:UILineBreakModeWordWrap];
Broonix
drawInRect:withAttributes is not available from the iOS SDK.
Broonix
Yeah I looked it up and realized that, sorry. What happens if you use UILineBreakModeCharacterWrap instead. This is a very mysterious problem.
Ben
It's almost the same using character wrap. Only one line is rendered but my width is a bit bigger now (277) which makes sense since it would now wrap mid character.
Broonix
Even more fun: [text drawInRect] returns the size of the drawn text. This exactly matches the 255x96 area I want to render the text to, but does not wrap
Broonix
Beyond mysterious. Have you tried a different font or any other changes that really shouldn't matter but might inexplicably have some affect...
Ben
The issue was CGContextSetTextDrawingMode(context, kCGTextFillStrokeClip); it should have been CGContextSetTextDrawingMode(context, kCGTextFillStroke);
Broonix