2011-08-16 20 views
3

有沒有辦法用drawAtPoint繪製多行文字? 我試過UILineBreakModeWordWrap,但似乎沒有工作?帶drawAtPoint的多行文本(wordwrap)?

如何將此代碼轉換爲可用的多行文本?

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

謝謝!

回答

10

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

編輯:(以下複製到這裏@George阿斯達的評論)

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

這樣的事情? [self.heading drawInRect:(contentRect)withFont:mainFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter]; –

+0

是的..正確.. – EmptyStack

-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; 
+0

我怎麼能做到這一點? (UILabel) –

+0

看我上面的編輯。 – Mundi

+0

這也是正確的嗎? [self.heading drawInRect:(contentRect)withFont:mainFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentCenter]; –

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

請解釋你的代碼。 – bfontaine