2013-04-06 50 views
1

我試圖動態改變的UILabel高度文本寬度的變化,但是當我嘗試使用此行的「白沃林頓霍爾」出現問題。基本上它計算寬度,該寬度在2行中的UILabel,但是當它示出了兩行它看起來像這樣:動力高度的UILabel

"white 
wallington h..." 

樣品的編號:

UILabel* lbl_Property = [[UILabel alloc]init]; 
    [lbl_Property setFont:[UIFont fontWithName:@"Arial" size:10]]; 
    [lbl_Property setNumberOfLines:0]; 
    [lbl_Property setText:[arr_SubDetail objectAtIndex:x]]; 
    UIFont* font = [UIFont fontWithName:@"Arial" size:10]; 
    CGSize stringSize = [lbl_Property.text sizeWithFont:font]; 
    CGFloat width = stringSize.width; 


    [lbl_Property setFrame:CGRectMake(lbl_Property.frame.origin.x, lbl_Property.frame.origin.y, lbl_Property.frame.size.width,5+lbl_Property.frame.size.height*(ceilf(width/110)))]; 

實際上後的「白」是有空間和「wallington」不在該行,因此它在新行,但它不顯示完整的文本

如何讓它顯示正確?

+0

添加您的參考碼 – NANNAV 2013-04-06 06:48:32

回答

3
int h=10;  
UILabel *lbl_headitem = [[UILabel alloc]initWithFrame:CGRectMake(3,h, 690, size_txt_overview1.height)];// See the 'h' value 
lbl_headitem.text = [headItemArray objectAtIndex:k]; 
[lbl_headitem setTextAlignment:UITextAlignmentLeft]; 
[lbl_headitem setBackgroundColor:[UIColor clearColor]]; 
[lbl_headitem setTag:k]; 
lbl_headitem.numberOfLines=0; 
[lbl_headitem setTextColor:[UIColor redColor]]; 
[lbl_headitem setFont:[UIFont fontWithName:@"Helvetica" size:18]]; 
[scrollAttributes addSubview:lbl_headitem]; 
h = h + size_txt_overview1.height; 

確保您label.numberOfLines被設置爲0

1

希望下面的代碼可能工作

CGSize size = [str sizeWithFont:lbl.font constrainedToSize:CGSizeMake(204.0, 10000.0) lineBreakMode:lbl.lineBreakMode]; 

這裏寬度或高度必須是固定的..

5

試試吧....

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

工程偉大,真正乾淨的解決方案。 – 2014-01-12 00:17:56

+0

謝謝@ Chirag ..它的作品。 – 2014-01-27 12:17:53

0
func makeSizeSender(name:UILabel){ 

    let attributes = [NSFontAttributeName : name.font] 
    let rect = name.text!.boundingRectWithSize(CGSizeMake(frame.size.width, CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: attributes, context: nil) 
    name.frame = CGRectMake(contentView.frame.origin.x+30, 0, 200, rect.height+30); 
    self.addSubview(name) 

}