2014-01-23 159 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; 
    } 


希望這有助於ü:)