【问题标题】:Pixelated UILabel in table cell表格单元格中的像素化 UILabel
【发布时间】:2012-08-12 12:31:02
【问题描述】:

要将 UILabel 添加到我使用的表格单元格中

UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 10, 40, 12)];
timeLabel.text = @"2s";
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.font = [UIFont systemFontOfSize:12];
timeLabel.textColor = [UIColor lightGrayColor];
timeLabel.highlightedTextColor = [UIColor whiteColor];
timeLabel.textAlignment = UITextAlignmentRight;
timeLabel.frame = CGRectIntegral(timeLabel.frame);
[cell.contentView addSubview:timeLabel]; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.

在我滚动表格或选择一个单元格之前,这可以正常工作。然后标签变得像素化。

加载时:

操作后:

我还尝试通过子类化 UITableViewCell 来添加标签并将其加载到 - (void) layoutSubviews.

我已经找到了相关问题 herehere,但没有任何效果。

编辑:无法使用标准单元格标签,因为它们已经在使用中。我需要添加一个额外的标签。

【问题讨论】:

  • 您使用的是哪个版本的 iOS?如果是 6.x,它可能是软件中的错误(如果您不在 5.x 上,它甚至可能是错误……没有人是完美的)。是否可以让背景不透明?
  • 我在 iOS 5.1.1 和 iOS 6b4 上测试过。我无法想象这是一个错误,因为包括 Facebook 和 Twitter 在内的许多应用程序都将 UILabel 添加到表格单元格中。
  • 是的,可以使背景不透明,但这并不能解决问题。

标签: ios ios5 uitableview uilabel


【解决方案1】:

我终于得到了一个肮脏的修复。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我设置了

cell.selectionStyle = UITableViewCellSelectionStyleNone;.

UITableViewCell 的子类中,我在 initWithStyle 中加载 timeLabel,如下所示:

timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 10, 40, 12)];
timeLabel.text = @"2s";
timeLabel.backgroundColor = [UIColor whiteColor];
timeLabel.font = [UIFont systemFontOfSize:12];
timeLabel.textColor = [UIColor lightGrayColor];
timeLabel.highlightedTextColor = [UIColor whiteColor];
timeLabel.textAlignment = UITextAlignmentRight;
[self.contentView addSubview:timeLabel];

然后我重写这两个函数:

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    if(highlighted == YES){
        UIImage *image = [UIImage imageNamed:@"cellBg@2x.png"];
        //scale custom cell background to necessary height
        UIImage *scaledImage = [image scaleToSize:CGSizeMake(1,self.contentView.frame.size.height)];
        //set cell background
        self.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
        //set textcolor for default labels
        self.textLabel.textColor = [UIColor whiteColor];
        self.detailTextLabel.textColor = [UIColor whiteColor];
        //set textcolor for custom label
        timeLabel.textColor = [UIColor whiteColor]; 
        //cope background for custom label background since timeLabel.backgroundColor = [UIColor clearColor] doesnt work
        CGImageRef ref = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(0, 10, 12, 20));
        UIImage *img = [UIImage imageWithCGImage:ref];
        //set custom label background
        timeLabel.backgroundColor = [UIColor colorWithPatternImage:img];
    } else {
        //set unselected colors
        self.backgroundColor = [UIColor whiteColor];
        self.textLabel.textColor = [UIColor darkGrayColor];
        self.detailTextLabel.textColor = UIColorFromRGB(0x808080);
        timeLabel.textColor = UIColorFromRGB(0x808080);
        //white background works without the label pixelates
        timeLabel.backgroundColor = [UIColor whiteColor];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if(selected == YES){
        UIImage *image = [UIImage imageNamed:@"cellBg@2x.png"];
        UIImage *scaledImage = [image scaleToSize:CGSizeMake(1,self.contentView.frame.size.height)];
        self.backgroundColor = [UIColor colorWithPatternImage:scaledImage];
        self.textLabel.textColor = [UIColor whiteColor];
        self.detailTextLabel.textColor = [UIColor whiteColor];
        timeLabel.textColor = [UIColor whiteColor];
        CGImageRef ref = CGImageCreateWithImageInRect(scaledImage.CGImage, CGRectMake(0, 10, 12, 20));
        UIImage *img = [UIImage imageWithCGImage:ref];  
        timeLabel.backgroundColor = [UIColor colorWithPatternImage:img];
    } else {
        self.backgroundColor = [UIColor whiteColor];
        self.textLabel.textColor = [UIColor darkGrayColor];
        self.detailTextLabel.textColor = UIColorFromRGB(0x808080);
        timeLabel.textColor = UIColorFromRGB(0x808080);
        timeLabel.backgroundColor = [UIColor whiteColor];
    }
}

希望这可以帮助一些人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 1970-01-01
    相关资源
    最近更新 更多