1:配置导航栏中间字体颜色,字体的方法
[navVC.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];//后面的字典是语法糖写法 格式为@{key:value,key1:value1};
//NSForegroundColorAttributeName这个key是固定的,颜色就是这个 字体是 NSFontAttributeName
2:更改状态栏颜色(配合Plist文件) 白色
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
3:如何获得一个汉字名字的没有音调的拼音形式
- (NSString *)transformMandarinToLatin:(NSString *)string
{
NSMutableString *preString = [string mutableCopy];
CFStringTransform((CFMutableStringRef)preString, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((CFMutableStringRef)preString, NULL, kCFStringTransformStripDiacritics, NO);
if ([[(NSString *)string substringToIndex:1] compare:@"长"] == NSOrderedSame)
{
[preString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"chang"];
}
if ([[(NSString *)string substringToIndex:1] compare:@"沈"] == NSOrderedSame)
{
[preString replaceCharactersInRange:NSMakeRange(0, 4) withString:@"shen"];
}
if ([[(NSString *)string substringToIndex:1] compare:@"厦"] == NSOrderedSame)
{
[preString replaceCharactersInRange:NSMakeRange(0, 3) withString:@"xia"];
}
if ([[(NSString *)string substringToIndex:1] compare:@"地"] == NSOrderedSame)
{
[preString replaceCharactersInRange:NSMakeRange(0, 3) withString:@"di"];
}
if ([[(NSString *)string substringToIndex:1] compare:@"重"] == NSOrderedSame)
{
[preString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"chong"];
}
return preString;
}
4:自定义cell(明信片)的时候,需要重写自定义方法
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// //设置选中背景图
UIImageView * selectView = [[UIImageView alloc]initWithFrame:self.bounds];
selectView.backgroundColor = MAIN_COLOR;
selectView.alpha = 0.5;
[self.contentView addSubview:selectView];
}
return self;
}
5:懒加载方法(这里拿label做例子哦)(这里的nameLable是定义的属性)里面的方法是类方法,封装好的(如6)
//重写他的getter方法,实现懒加载(lazyLoading机制)
//优势
//对象在什么时候使用,什么时候才去创建,避免有些对象还未使用就提前初始化导致内存瞬时压力过大,使用的前提一定要使用getter方法才回触发懒加载(所以下面DidLoad加对象的时候要有self.tableView)
//对象在使用完销毁后指针置空,下次在使用时会再次被重建
- (UILabel *)nameLable
{
//if条件判断不能写self. 因为self.是getter方法,你刚进来,就有调getter(自己调自己),会一直调,崩溃
if (_nameLable == nil) {
//初始化必须用self.nameLable,目的是为了让引用计数加1,他的release在dealloc里面
self.nameLable = [UILabel lableWithFram:CGRectMake(_iconImageView.right + K_SPACE, _iconImageView.top, K_NAME_WIDTH, K_LABLE_HEIGHT) backgroundColor:DEBUG_COLOR font:TEXT_FONT textAliment:NSTextAlignmentCenter textColor:TEXT_COLOR];
}
return [[_nameLable retain] autorelease]; //不要用self. 因为用self.是getter方法,会一直调用这个方法
}
6:类目的使用,对label的一些方法进行封装,通过这个例子要学会实现类方法(+),例5中的类方法封装如下
+ (UILabel *)lableWithFram:(CGRect)rect
backgroundColor:(UIColor *)color
font:(UIFont *)font
textAliment:(NSTextAlignment)textAliment
textColor:(UIColor *)textColor
{
UILabel * aLable = [[UILabel alloc] initWithFrame:rect];
aLable.backgroundColor = color;
aLable.font = font;
aLable.textAlignment = textAliment;
aLable.textColor = textColor;
return [aLable autorelease];
}
7:回收键盘操作
在textField中,需要遵从UITextFieldDelegate协议
遵从协议,设置代理人,实现方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];//回收键盘
return YES;
}
8:模态转换页面的方法(任何一个UIView都可以模态出一个页面)
- (void)handleAddContact:(UIBarButtonItem *)barItem
{
//任何一个UIView都可以模态出一个页面
AddcontactViewController * addContactVC = [[AddcontactViewController alloc] init];
UINavigationController * addContactNAV = [[UINavigationController alloc] initWithRootViewController:addContactVC];
//设置模态过度样式,也是枚举值,进去粘贴
addContactVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//模态一个页面
[self presentViewController:addContactNAV animated:YES completion:^{
NSLog(@"完成模态");
}];
//设置标题栏的主颜色
addContactNAV.navigationBar.barTintColor = [UIColor yellowColor];
//配置导航控制器 中间字体是白色
[addContactNAV.navigationBar setTitleTextAttributes:
@{NSFontAttributeName:[UIFont systemFontOfSize:19],
NSForegroundColorAttributeName:[UIColor whiteColor]}];//这个方法例1中说明的有
[addContactNAV release];
}
取消模态方法
[self dismissViewControllerAnimated:YES completion:nil];
9:刷新数据的方法
- (void)viewWillAppear:(BOOL)animated {
//刷新数据
[self.tableView reloadData];
[super viewWillAppear:YES];
}
10:cell中内容如果想编辑,首先要实现的方法
[*** setEditing:YES];
11: 删除时提示的字符串,原本是英文,现在我们可以自定义
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
12:单例(这里面的alloc不能release或者autorelease,因为它贯穿于整个系统,不能清除)
+ (ContactHelper *)shareHelper
{
static ContactHelper * helper = nil;
if (helper == nil) {
helper = [[ContactHelper alloc] init];
[helper loadDataFromPlist];
}
return helper;
}
13:UILabel的各种属性
/*
UILabel:
1:创建UILable对象
2:设置UILable属性
3:添加UILable对象到指定的父视图
4:释放所有权
*/
//1:创建UILable对象
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];
//2:设置UILable属性
lable.backgroundColor = [UIColor greenColor];
//3:添加文字(核心功能) 默认显示的字体大小:17
lable.text = @"英雄联盟木木提姆拉克丝金克斯牛头卡牌大师武器大师钢铁大使安妮卡特琳娜";
//修改文字字体样式和大小
lable.font = [UIFont fontWithName:@"AcademyEngravedLetPlain" size:20];
//修改字体颜色
lable.textColor = [UIColor grayColor];
//对齐方式
lable.textAlignment = NSTextAlignmentCenter;
//指定lable显示文字的行数
lable.numberOfLines = 0;
//设置lable中文字的换行方式
lable.lineBreakMode = NSLineBreakByWordWrapping;
//设置文字的阴影颜色以偏移量
lable.shadowColor = [UIColor whiteColor];
lable.shadowOffset = CGSizeMake(4, 4);
//4:添加UILable对象到指定的父视图
[self.window addSubview:lable];
//5:释放所有权
[lable release];
14: UITextField的各种属性
/*
UITextField:文本输入框,核心作用:进行文字的编辑。同时可以显示文字
1:创建UITextField对象
2:配置UITextField对象的属性
3:添加到指定的父视图
4:释放所有权
*/
UITextField *textFile = [[UITextField alloc] initWithFrame:CGRectMake(35, 100, 250, 40)];
//1:设置背景颜色
textFile.backgroundColor = [UIColor whiteColor];
//2:设置提示文字
textFile.placeholder = @"请输入密码";
//3:设置textField文字
//textFile.text = @"123456";
//4:设置textField文字的颜色
textFile.textColor = [UIColor redColor];
//5:设置textField文字的对齐方式
textFile.textAlignment = NSTextAlignmentLeft;
//6:设置textField的编辑属性
textFile.enabled = YES;
//7:textField开始编辑的时候将原来的内容清空
textFile.clearsOnBeginEditing = YES;
//8:设置textField字体的样式和大小
textFile.font = [UIFont systemFontOfSize:40];
//9:设置textField的虚拟键盘的样式
//textFile.keyboardType = UIKeyboardTypeNumberPad;
//10:设置return键的样式
textFile.returnKeyType = UIReturnKeyDone;
//11:设置输入框的密码样式
textFile.secureTextEntry = YES;
//12:设置textField边框样式
textFile.borderStyle = UITextBorderStyleRoundedRect;
//13:设置点击textField弹出的视图,(应用场景:自定义键盘样式)
// UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
// aView.backgroundColor = [UIColor yellowColor];
// textFile.inputView = aView;
// [aView release];
//14:设置点击textField时弹出的辅助视图
// UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
// bView.backgroundColor = [UIColor redColor];
// textFile.inputAccessoryView = bView;
// [bView release];
//15:设置textField的清除按钮
textFile.clearButtonMode = UITextFieldViewModeWhileEditing;
//16:textField设置代理
textFile.delegate = self;
//添加UITextField到containView上
[containView addSubview:textFile];
//释放所有权
[textFile release];
15: UIButton的各种属性
//UIButton:响应用户的点击操作,按钮可以显示位子和图片
/*
1:创建UIButton对象
2:设置按钮属性
3:添加按钮到指定的父视图
*/
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//设置背景颜色
button.backgroundColor = [UIColor blueColor];
//设置frame
button.frame = CGRectMake(110, 400, 100, 40);
//设置按钮的标题文字
[button setTitle:@"按钮" forState:UIControlStateNormal];
//设置按钮的标题文字颜色
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//按钮核心操作:响应用户点击
//不管谁调用addTarget:action:forControlEvents:,执行的action操作对应的参数就是谁
[button addTarget:self action:@selector(handdle:) forControlEvents:UIControlEventTouchUpInside];
//给button添加背景图片
[button setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
//添加按钮到指定的父视图上
[containView addSubview:button];
网友评论