2013-05-10 39 views
-1

我想爲tableView中的每個單元製作註釋的標籤..問題是每當更改文本字符串時都改變「y」位置。帶有固定的x和y在tableView.cell中的標籤

我使用CGSize來計算寬度爲&高度的文本字符串的大小..所以如果評論太長,y會更底。如果評論太短,y也會改變。

如何爲每個文本字符串的大小修復x & y。這裏是我的代碼:

static CGFloat messageTextSize = 14.0; 

+(CGFloat)maxTextWidth { 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { 
     return 220.0f; 
    } else { 
     return 400.0f; 
    } 
} 

+(CGSize)messageSize:(NSString*)message { 
    return [message sizeWithFont:[UIFont systemFontOfSize:messageTextSize] constrainedToSize:CGSizeMake([FirstViewController maxTextWidth], CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping]; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSString *text = @"After a power struggle with the board of directors in 1985, Jobs left Apple and founded NeXT, a computer platform development company specializing in the higher-education and business markets. In 1986, he acquired the computer graphics division of Lucasfilm, which was spun off as Pixar.[13] He was credited in Toy Story (1995) as an executive producer. He served as CEO and majority shareholder until Disney's purchase of Pixar in 2006.[14] In 1996, after Apple had failed to deliver its operating system, Copland, Gil Amelio turned to NeXT Computer, and the NeXTSTEP platform became the foundation for the Mac OS X.[15] Jobs returned to Apple as an advisor, and took control of the company as an interim CEO. Jobs brought Apple from near bankruptcy to profitability by 1998"; 
    CGSize textSize = [FirstViewController messageSize:text]; 
    // NSLog(@"%f",textSize.width); 
    return 460 + (textSize.height - 100); 

} 

而這個標籤:

NSString *comment = @"After a power struggle with the board of directors in 1985, Jobs left Apple and founded NeXT, a computer platform development company specializing in the higher-education and business markets. In 1986, he acquired the computer graphics division of Lucasfilm, which was spun off as Pixar.[13] He was credited in Toy Story (1995) as an executive producer. He served as CEO and majority shareholder until Disney's purchase of Pixar in 2006.[14] In 1996, after Apple had failed to deliver its operating system, Copland, Gil Amelio turned to NeXT Computer, and the NeXTSTEP platform became the foundation for the Mac OS X.[15] Jobs returned to Apple as an advisor, and took control of the company as an interim CEO. Jobs brought Apple from near bankruptcy to profitability by 1998"; 

    CGSize textSize = [FirstViewController messageSize:comment]; 
UILabel *label6 = [[UILabel alloc] initWithFrame:CGRectMake(20, 330, cell.frame.size.width - 30, textSize.height)]; 
    label6.textColor = [UIColor colorWithRed:61.0/255.0 green:113.0/255.0 blue:154.0/255.0 alpha:1.0]; 
    label6.backgroundColor = [UIColor clearColor]; 
    label6.font = [UIFont fontWithName:@"Helvetica-Bold" size:11]; 
    label6.numberOfLines = 0; 
    label6.lineBreakMode = UILineBreakModeCharacterWrap; 
    label6.text = [NSString stringWithFormat:@"%@: %@",username,comment]; 
    [cell addSubview:label6]; 

在這裏你可以看到截圖獲得分數。我想解決下喜歡標籤註釋或讓說的起始位置定製

http://d.pr/i/RBid

回答

0
  1. 嘗試使用一個UITextView,而不是的UILabel,因爲標籤是個應該是單線。
  2. 確保在尺寸計算和TextView中使用相同的字符串,字體,字體大小和環繞。
+0

這裏的解決方案:http://stackoverflow.com/questions/10767913/uilabel-with-fixed-width-and-flexible-height感謝所有 – SimpleojbC 2013-05-10 20:59:05

+0

歡迎你,我的「解決方案」 – nooitaf 2013-05-10 21:09:31

+0

不同意爲什麼你不同意它的魅力! – SimpleojbC 2013-05-11 10:17:19