UITabeViews---设置字体格式,大小,颜色
2015-05-23 12:58
357 查看
效果图:
![](http://img.blog.csdn.net/20150523130313018?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjczNjQ0MzE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
UITableView设置每行显示的内容,字体格式,大小,颜色
首先设置根视图控制器:
AppDelegate.m文件
#import "AppDelegate.h"
#import "JRTableViewController.h"
@interface
AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
JRTableViewController * tableVC=[[JRTableViewController
alloc]init];
self.window.rootViewController=tableVC;
return
YES;
}
自定义的JRTableViewController.m文件
#import "JRTableViewController.h"
//定义宏
#define jrRandomColor [UIColor colorWithRed:arc4random_uniform(10)*0.1 green:arc4random_uniform(10)*0.1 blue:arc4random_uniform(10)*0.1
alpha:1]
@interface
JRTableViewController ()
//数据存储
@property (nonatomic,strong)
NSArray * dataArray;
@end
@implementation JRTableViewController
- (void)viewDidLoad {
[super
viewDidLoad];
self.tableView.rowHeight=100;
//加载数据
[self
_loadData];
}
#pragma mark - 加载 tableView
数据
- (void) _loadData
{
self.dataArray=[UIFont
familyNames]; //每行cell内显示的内容
}
//创建JRTableViewController时,自动生成代理方法
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
//返回数组的行数
}
#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString * identy=@"JRTable";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:identy];
if (!cell)
{
cell=[[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identy];
}
cell.textLabel.text=self.dataArray[indexPath.row];
cell.textLabel.font=[UIFont
fontWithName:cell.textLabel.text
size:16];
//设置字体颜色
if(indexPath.row%2==0)
{
cell.textLabel.textColor=jrRandomColor;
//宏
}
return cell;
}
//设置每一行的高度
/*
第 0行
高度 100
第 1行
高度 50
第 2行
高度 100
第 3行
高度 50
第 4行
高度 100
第 5行
高度 50
*/
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
if (indexPath.row%2==0)
{
return 100;
}
else
{
return 50;
}
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
}
@end
UITableView设置每行显示的内容,字体格式,大小,颜色
首先设置根视图控制器:
AppDelegate.m文件
#import "AppDelegate.h"
#import "JRTableViewController.h"
@interface
AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
JRTableViewController * tableVC=[[JRTableViewController
alloc]init];
self.window.rootViewController=tableVC;
return
YES;
}
自定义的JRTableViewController.m文件
#import "JRTableViewController.h"
//定义宏
#define jrRandomColor [UIColor colorWithRed:arc4random_uniform(10)*0.1 green:arc4random_uniform(10)*0.1 blue:arc4random_uniform(10)*0.1
alpha:1]
@interface
JRTableViewController ()
//数据存储
@property (nonatomic,strong)
NSArray * dataArray;
@end
@implementation JRTableViewController
- (void)viewDidLoad {
[super
viewDidLoad];
self.tableView.rowHeight=100;
//加载数据
[self
_loadData];
}
#pragma mark - 加载 tableView
数据
- (void) _loadData
{
self.dataArray=[UIFont
familyNames]; //每行cell内显示的内容
}
//创建JRTableViewController时,自动生成代理方法
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
//返回数组的行数
}
#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString * identy=@"JRTable";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:identy];
if (!cell)
{
cell=[[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identy];
}
cell.textLabel.text=self.dataArray[indexPath.row];
cell.textLabel.font=[UIFont
fontWithName:cell.textLabel.text
size:16];
//设置字体颜色
if(indexPath.row%2==0)
{
cell.textLabel.textColor=jrRandomColor;
//宏
}
return cell;
}
//设置每一行的高度
/*
第 0行
高度 100
第 1行
高度 50
第 2行
高度 100
第 3行
高度 50
第 4行
高度 100
第 5行
高度 50
*/
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
if (indexPath.row%2==0)
{
return 100;
}
else
{
return 50;
}
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
}
@end
相关文章推荐
- MFC——设置静态文本 大小 字体格式 颜色
- android设置Spinner字体颜色,格式,大小
- android设置Spinner字体颜色,格式,大小
- MFC操作——设置静态文本 大小 字体格式 颜色
- 怎么设置eclipse默认编码格式 调整字体大小
- 设置UIButton的文字显示位置、字体的大小、字体的颜色
- android 一个 textview 设置不同的字体大小和颜色
- 一个TextView 设置两种字体颜色和大小
- ios设置字体-黑体,加粗等 修改textField的placeholder的字体颜色、大小
- eclipse设置背景颜色、字体大小、行号、XML字体大小和颜色等操作
- phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护
- CSS如何设置字体的类型、大小、颜色
- Android 使用 html设置字体颜色大小
- SpannableString 动态设置textView中字体颜色和大小
- 设置UIButton的文字显示位置、字体的大小、字体的颜色
- Builder模式设置AlertDialog字体大小、颜色等属性
- Qt下设置QLabel字体的大小和颜色
- gtk 设置标签(label)里字体的大小 颜色等属性的方法
- Builder模式设置AlertDialog字体大小、颜色等属性
- 设置navigationBar的title的字体大小与颜色