2014-01-23 93 views
-2

我希望为自定义tableviewcell中的标签设置自定义fontcolor。 我试着写代码cellForRowAtIndexPath,willDisplayCellcustom cell类,但他们不工作。如何在自定义tableviewcelll中为标签设置自定义字体?

谁能告诉正是编写相同的代码。

感谢

+0

显示一些代码,你试过吗? – Mani

+0

clubCellObj.clubAddress.font = [UIFont fontWithName:@“Champagne&Limousines”size:9]; clubCellObj.clubAddress.textColor = [的UIColor colorWithRed:0.545绿:0.773蓝:0.247的α:1.0]; objClubCell是自定义表格视图单元的一个对象 –

+0

你有textcolor吗?这似乎是不错的.. – Mani

回答

0
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (cell == nil) 
    { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    UILabel *label = (UILabel *)[cell viewWithTag:100]; 
    [label setTextColor:[UIColor blueColor]]; 
} 

不要忘记在原型细胞设置您的标签标记为100和设置您的小区标识为“细胞”

+0

非常感谢,它的工作:D –

+0

@ sreejith.virgo但你谈论自定义tableview单元格。它是如何为你工作的?这是正常的tableview单元格的正确代码。 – Mani

+0

@Mani ..我想他刚刚对原型单元格和自定义tableview单元格感到困惑 –

0

如果使用标准电池。在cellFoRowAtIndexpath:你可以做这样的事情:

cell.textLabel.font = [UIFont fontWithName:@"some_font_name" size:cell.textLabel.font.pointSize]; 
cell.textLabel.textColor = [UIColor redColor] //this will set color to red 
+0

我使用的是名为“objClubCell”自定义单元格和标签在其名为“clubAddress” –

0

如果你要设置的自定义字体在你的项目比首先你要添加的杂项文件或文件名为.ttf在您的项目。而且你需要将它添加到你这样的Info.plist文件...

enter image description here

,如果你想将其设置为您customcell比你可以用它做你的cellForRowAtIndexPath ... 。

[cell.lblData setFont:[UIFont fontWithName:@"SourceSansPro-Regular" size:28.0]]; 
+0

谢谢,但我已经用于其他项目的标签相同的字体做你所拥有的后提及。 –

+0

比有什么问题? – Ajay

+0

问题出在为自定义表格视图单元格中的标签做同样的事情。 –

0

OKY,让我们尝试这种方式,

custom cell.h文件

#import <UIKit/UIKit.h> 

    @interface CustomCelll : UITableViewCell 
    @property(nonatomic, retain)UILabel *label; //add a property 

    @end 


在文件

#import "CustomCelll.h" 
@implementation CustomCelll 
@synthesize label;//label 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

    UILabel *lab = [[UILabel alloc]init];//init it, 
    self.label = lab; 
    //now hear only set all the property 
    self.label.textColor = [UIColor greenColor];//set the color 
    self.label.font = [UIFont fontWithName:@"Noteworthy-Bold" size:13];//set the font 
    [self addSubview:self.label]; 
    [lab release];//under the non ARC 

    } 
    return self; 
} 


- (void)layoutSubviews 
    { 
     [super layoutSubviews]; 
     //set frame for ur label 
     self.label.frame = CGRectMake(20, 3, 100, 30); 

    } 


现在控制器


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     CustomCell *aCell = [self.aTableView dequeueReusableCellWithIdentifier:@"cell"]; 
     if(aCell == nil) 
     { 
      aCell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
     } 
     aCell.label.text = @"Hello"; //just set the text 
     return aCell; 
    } 


希望这有助于ü:)