2013-01-07 28 views
1

我有一個條形圖,我需要爲每個酒吧寫地名,但有時名字很長。我怎麼可以把整個名稱,並根據酒吧的寬度正確包裝?圖中環繞的字

我使用下面的代碼並不如預期

NSString *percentage = [NSString stringWithFormat:@"%@",item.myTopNameStr]; 


[percentage drawAtPoint:CGPointMake(_histogramStartX + 5,_xaxisStart.y - _ySpacingScale*item.yValue - 25) forWidth:80 withFont:[UIFont boldSystemFontOfSize:10] lineBreakMode:NSLineBreakByClipping]; 

,但結果是,我無法看到它的全稱。

enter image description here

+0

你用過'UILineBreakModeWordWrap'在'lineBreakMode'? – Ankur

+0

我試過'UILineBreakModeWordWrap',但我沒有得到全名。 –

+1

我想你會更好通過使用' - drawInRect:withFont:lineBreakMode:alignment:';用'sizeWithFont:forWidth:lineBreakMode:' –

回答

1
NSString * percentage = [NSString stringWithFormat:@"%@",item.myTopNameStr]; 
CGSize percentageSize = [percentage sizeWithFont:[UIFont boldSystemFontOfSize:10] forWidth:80 lineBreakMode:UILineBreakModeWordWrap]; 

[percentage drawInRect:CGRectMake(_histogramStartX + 5,_xaxisStart.y - _ySpacingScale*item.yValue - 25, percentageSize.width, percentageSize.height) withFont:[UIFont boldSystemFontOfSize:10] lineBreakMode:UILineBreakModeWordWrap alignment:NSTextAlignmentLeft]; 
+0

我得到'percentageSize.height' = 13,這對於全名是不夠的,所以我把'150'而不是'percentageSize.height'。一切都很好。 –