2010-09-11 8 views
4

j'espère afficher les informations de badge sur la ligne UITableView comme l'image ci-dessousinfo badge affichage sur UITableView

alt text

J'essaie d'utiliser les codes ci-dessous:

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]]]; 

mais je peux pas obtenir le résultat du bloc rond montré dans l'image ci-dessus.

Répondre

6

rejettera votre code, ne pas réinventer la roue. Cela a été fait par plusieurs personnes qui l'ont fait open source. Here est l'un, here est un autre.

2

Vous pouvez utiliser la couche pour vous assurer que votre étiquette possède des coins arrondis comme dans l'exemple. Vous devez également régler le rect de l'étiquette en fonction de la largeur du texte:

// 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); 
1

Vous pouvez obtenir les éléments suivants eteint en utilisant le code Link

enter image description here