带有drawAtPoint的多行文字(wordwrap)?

时间:2011-08-16 09:45:01

标签: iphone sdk ios4 word-wrap cgpoint

有没有办法用drawAtPoint绘制多行文字? 我试过UILineBreakModeWordWrap,但似乎没有工作?

如何将此代码转换为有效的多行文字?

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

谢谢!

3 个答案:

答案 0 :(得分:10)

drawAtPoint:不支持多行文字。您可以改用drawInRect:方法。

修改(将@George Asda的评论复制到此处)

[self.heading drawInRect:(contentRect) withFont:mainFont    
        lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter];

答案 1 :(得分:1)

[_text drawWithRect:_textRect options:**NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine** attributes:attributes context:nil];

答案 2 :(得分:-1)

使用NSString drawAtPoint方法无法做到这一点。来自文档:

  

单行中的指定点处绘制字符串   使用指定字体和属性的当前图形上下文。

您是否可以使用简单的UILabel

修改

您可以像这样计算UILabel的高度:

//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;