代码之家  ›  专栏  ›  技术社区  ›  arachide

在UITableView上显示徽章信息

  •  4
  • arachide  · 技术社区  · 14 年前

    alt text

    我尝试使用以下代码:

    UILabel *labelCell1 =[ [UILabel alloc]init];
    labelCell1.frame = CGRectMake(160.9f,10.0f,60.0f,30.0f) ;
    [labelCell1 setBackgroundColor:[UIColor 
        colorWithPatternImage:[[UIImage imageNamed:@"block.png"] 
        stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0]]]; 
    

    3 回复  |  直到 14 年前
        1
  •  6
  •   coneybeare    14 年前

    扔掉你的代码,不要重新发明轮子。这是由几个开源的人完成的。 Here here 是另一个。

        2
  •  2
  •   RedBlueThing    14 年前

    您可以使用图层来确保标签具有圆角,如示例所示。您还需要根据文本的宽度调整标签的矩形:

    // set the text on the badge
    labelCell1.text=[NSString stringWithFormat:@"%d", value];
    
    // set the rounded corners on the layer
    CALayer* layer=labelCell1.layer;
    layer.masksToBounds = YES;
    layer.cornerRadius = 9.0;
    
    // adjust the width of the badge rect based on the text width
    CGFloat textWidth=[labelCell1.text sizeWithFont:labelCell1.font].width+12;
    
    labelCell1.frame=CGRectMake(
        labelCell1.frame.origin.x + labelCell1.frame.size.width - textWidth,
        labelCell1.frame.origin.y,
        textWidth,
        labelCell1.frame.size.height);
    
        3
  •  1
  •   Ashok    11 年前

    您可以使用中的代码将以下内容输出 Link

    enter image description here