5

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.

pablasso
  • 2,479
  • 2
  • 26
  • 32
Broonix
  • 1,135
  • 2
  • 11
  • 26

2 Answers2

0

Solved.

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

Broonix
  • 1,135
  • 2
  • 11
  • 26
0

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

How do I get -[NSString sizeWithFont:forWidth:lineBreakMode:] to work?

Community
  • 1
  • 1
Ben
  • 2,982
  • 1
  • 16
  • 12
  • If I log the height and width of textRect I get: width: 255.000000, height: 96.000000 – Broonix Oct 12 '10 at 14:58
  • The rect it is drawing into is 300.00 by 300.00 – Broonix Oct 12 '10 at 15:02
  • 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 Oct 12 '10 at 15:12
  • 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 Oct 12 '10 at 15:19
  • drawInRect:withAttributes is not available from the iOS SDK. – Broonix Oct 12 '10 at 15:33
  • Yeah I looked it up and realized that, sorry. What happens if you use UILineBreakModeCharacterWrap instead. This is a very mysterious problem. – Ben Oct 12 '10 at 15:37
  • 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 Oct 12 '10 at 15:52
  • 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 Oct 12 '10 at 16:08
  • Beyond mysterious. Have you tried a different font or any other changes that really shouldn't matter but might inexplicably have some affect... – Ben Oct 12 '10 at 16:39
  • 1
    The issue was CGContextSetTextDrawingMode(context, kCGTextFillStrokeClip); it should have been CGContextSetTextDrawingMode(context, kCGTextFillStroke); – Broonix Oct 12 '10 at 17:18