【问题标题】:Unexpected Y coordinate Of UILabel in UITableViewUITableView中UILabel的意外Y坐标
【发布时间】:2012-05-03 16:14:21
【问题描述】:

我有一个由 Customs Cells 组成的 tableview。 每个 Cell 共有三个 Label。

主要问题是单元格三个标签的中间标签的形成。

我正在创建一个具有相同 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"];

...将为单元格中的每个标签调用此标签,但它会泄露第一个标签和中间标签之间的意外距离。

查看图片中红色高亮区域

【问题讨论】:

    标签: ios5 xcode4 uitableview uilabel custom-cell


    【解决方案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;
    

    【讨论】:

      【解决方案2】:

      UILabel 的内容不是顶部对齐(而是在中间居中),这就是您看到此内容的原因。

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

      【讨论】:

        【解决方案3】:
        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];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多