我希望爲自定義tableviewcell中的標籤設置自定義font
和color
。 我試着寫代碼cellForRowAtIndexPath
,willDisplayCell
和custom cell
類,但他們不工作。如何在自定義tableviewcelll中爲標籤設置自定義字體?
誰能告訴正是編寫相同的代碼。
感謝
我希望爲自定義tableviewcell中的標籤設置自定義font
和color
。 我試着寫代碼cellForRowAtIndexPath
,willDisplayCell
和custom cell
類,但他們不工作。如何在自定義tableviewcelll中爲標籤設置自定義字體?
誰能告訴正是編寫相同的代碼。
感謝
- (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和設置您的小區標識爲「細胞」
非常感謝,它的工作:D –
@ sreejith.virgo但你談論自定義tableview單元格。它是如何爲你工作的?這是正常的tableview單元格的正確代碼。 – Mani
@Mani ..我想他剛剛對原型單元格和自定義tableview單元格感到困惑 –
如果使用標準電池。在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
我使用的是名爲「objClubCell」自定義單元格和標籤在其名爲「clubAddress」 –
如果你要設置的自定義字體在你的項目比首先你要添加的雜項文件或文件名爲.ttf在您的項目。而且你需要將它添加到你這樣的Info.plist文件...
,如果你想將其設置爲您customcell比你可以用它做你的cellForRowAtIndexPath
... 。
[cell.lblData setFont:[UIFont fontWithName:@"SourceSansPro-Regular" size:28.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;
}
希望這有助於ü:)
顯示一些代碼,你試過嗎? – Mani
clubCellObj.clubAddress.font = [UIFont fontWithName:@「Champagne&Limousines」size:9]; clubCellObj.clubAddress.textColor = [的UIColor colorWithRed:0.545綠:0.773藍:0.247的α:1.0]; objClubCell是自定義表格視圖單元的一個對象 –
你有textcolor嗎?這似乎是不錯的.. – Mani