UITabel中UILabel的意外Y坐标

时间:2012-05-03 16:14:22

标签: ios5 xcode4 uitableview uilabel custom-cell

我有一个由海关单元组成的tableview。 每个Cell总共有三个标签。

主要问题是细胞三个标签的中间标签的形成。

我正在使用与

相同的CGRect创建自定义标签
UILabel *msglabeltemp = [[[UILabel alloc]  initWithFrame:  CGRectMake(80,20.0,230.0,80.0)] autorelease];
[cell.contentView addSubview:msglabeltemp];
msglabeltemp.backgroundColor = [UIColor clearColor];
msglabeltemp.textColor = [UIColor grayColor];
msglabeltemp.numberOfLines=6;
[msglabeltemp setFont:[UIFont fontWithName:@"Helvetica Neue" size:12.0f]];
msglabeltemp.tag=1;
msglabeltemp.font=[UIFont systemFontOfSize:12.0f];
msglabeltemp.textAlignment=UITextAlignmentLeft ;
//Adding Label To Cell
UILabel *msglabel = (UILabel *)[cell.contentView viewWithTag:1];

msglabel.text = [data1 objectForKey:@"msg"];

...将为单元格中的每个标签调用此标签,但它会放弃第一个和中间标签之间的预期距离。

查看图像中红色突出显示的区域

enter image description here

3 个答案:

答案 0 :(得分:1)

这也很有用

CGSize maximumSize = CGSizeMake(300, 9999);
    NSString *dateString = @"The date today is January 1st, 1999";
    UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];
    CGSize dateStringSize = [dateString sizeWithFont:dateFont 
            constrainedToSize:maximumSize 
            lineBreakMode:self.dateLabel.lineBreakMode];

    CGRect dateFrame = CGRectMake(10, 10, 300, dateStringSize.height);

    self.dateLabel.frame = dateFrame;

答案 1 :(得分:0)

UILabel的内容不是顶部对齐的(而是在中间居中),这就是为什么你会看到这个。

请参阅Vertically align text to top within a UILabel获取有关如何更改此内容的灵感。

答案 2 :(得分:0)

CGRect lblFrame = CGRectMake(20, 20, 280, 150);
    UILabel *lbl = [[UILabel alloc] initWithFrame:lblFrame];
    [lbl setBackgroundColor:[UIColor orangeColor]];

    NSString *labelText = @"I am the very model of a modern Major-General, I've information vegetable, animal, and mineral";
    [lbl setText:labelText];

    // Tell the label to use an unlimited number of lines
    [lbl setNumberOfLines:0];
    [lbl sizeToFit];