-1

我正在尝试为 tableView 中的每个单元格创建一个注释标签。问题是如果我更改文本字符串,每次更改“y”位置的标签。

我使用 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

4

1 回答 1

0
  1. 尝试使用 UITextView 而不是 UILabel,因为标签被认为是单行的。
  2. 确保在大小计算和 TextView 中使用相同的字符串、字体、字体大小和换行。
于 2013-05-10T20:51:21.083 回答