Iphone Dev : Changer la font d'un UITableCell
Par Jeremie Engel le jeudi, décembre 6 2007, 12:22 - IPhone - Lien permanent
Vous développez une application IPhone qui utilise un tableau UITable pour afficher du texte et vous souhaitez changer la taille du texte ?
Voici une solution simple. Il s'agit d'ajouter à la vue de la cellule un objet UITextLabel dont la font sera modifiable.
-(void)showTable
{
UIView *v = [[[UIView alloc] initWithFrame: [window bounds]] autorelease];
float th = 115;
CGSize vs = [v bounds].size;
table = [[UITable alloc] initWithFrame: CGRectMake(0,th,vs.width,vs.height-s.height-th)];
[table addTableColumn: [[[UITableColumn alloc] initWithTitle:@"" identifier:@"col1" width:220] autorelease]];
[table addTableColumn: [[[UITableColumn alloc] initWithTitle:@"" identifier:@"col2" width:100] autorelease]];
[table setDataSource: self];
[v addSubview: table];
[window setContentView: v];
}
- (int)numberOfRowsInTable:(UITable*)table
{
return 1;
}
- (UITableCell*)table:(UITable*)table cellForRow:(int)row column:(UITableColumn *)column
{
id cell = [[[UIImageAndTextTableCell alloc] init] autorelease];
UITextLabel *textLabel = [UITextLabel alloc];
if ([column identifier] == @"col1") [textLabel initWithFrame:CGRectMake(10.0f,0.0f,210.0f,30.0f)];
else if ([column identifier] == @"col2") [textLabel initWithFrame:CGRectMake(0.0f,0.0f,100.0f,30.0f)];
if( row == 0 && [column identifier] == @"col1" ) [textLabel setText: @"text to print Col1"];
if( row == 0 && [column identifier] == @"col2" ) [textLabel setText: @"text to print Col2"];
// Modify the UITableCell Font with a subview UITextLabel
[textLabel setFont: [NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:16]];
[textLabel setCentersHorizontally:NO];
[cell addSubview: textLabel];
[textLabel release];
return cell;
}

Commentaires