3

is there a way to draw multiline text with drawAtPoint? I have tried UILineBreakModeWordWrap but doesnt seem to be working?

How would you convert this code to a working multiline text??

point = CGPointMake(boundsX, 50);
[self.heading drawAtPoint:point forWidth:labelWidth withFont:mainFont minFontSize:12.0 actualFontSize:NULL lineBreakMode:UILineBreakModeWordWrap baselineAdjustment:UIBaselineAdjustmentAlignBaselines];

Thank you!

George Asda
  • 2,119
  • 2
  • 28
  • 54

3 Answers3

10

drawAtPoint: doesn't support multiline text. You can use drawInRect: method instead.

Edit: (Copying @George Asda's comment below to here)

[self.heading drawInRect:(contentRect) withFont:mainFont    
        lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
  • 1
    Something like this? [self.heading drawInRect:(contentRect) withFont:mainFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter]; – George Asda Aug 16 '11 at 10:08
1
[_text drawWithRect:_textRect options:**NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine** attributes:attributes context:nil];
Bhavesh Hirpara
  • 22,255
  • 15
  • 63
  • 104
pikachu
  • 119
  • 10
-1

This cannot be done with the NSString drawAtPoint method. From the documentation:

Draws the string in a single line at the specified point in the current graphics context using the specified font and attributes.

Could you perhaps use a simple UILabel?

EDIT

You can calculate the height of a UILabel like this:

//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font 
                    constrainedToSize:maximumLabelSize 
                    lineBreakMode:yourLabel.lineBreakMode]; 

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
Mundi
  • 79,884
  • 17
  • 117
  • 140