iOS 开发小技巧(一)

1.取字符串中的某段字符串
[plain] view plaincopyprint?
NSString *str =@"12sadfsdf56afsdf";
NSLog(@"%@",str);
NSString *thrStr = [strstringByReplacingOccurrencesOfString:@"56"withString:@"88"];
NSLog(@"%@",thrStr);
即把12sadfsdf56afsdf中的56替换为88,替换后字符串为:12sadfsdf88afsdf

2.简单plist获取
[plain][NSBundlemainBundle]athForResource:@"Array"ofType:@"plist"];
NSMutableDictionary *data = [[NSMutableDictionaryalloc]initWithContentsOfFile:path];
NSLog(@"%d",[datacount]);
NSLog(@"%@",[dataallValues]);
NSLog(@"------------") ;
NSArray *ary = [dataallKeys];
NSLog(@"%d",[arycount]);
NSArray *array = [NSArrayarrayWithArray:ary];
for(int i=0;i<[arraycount];i++)
{
NSLog(@"\n value = %@",[arrayobjectAtIndex:i]);
}
NSArray *getArray = [[dataallValues]objectAtIndex:5];
NSLog(@"get_array:%@",getArray);

3.状态栏和UINavigationBar设置成透明
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleBlackTranslucent]; //状态栏设置为透明
theNavigationController.navigationBar.barStyle =UIBarStyleBlackTranslucent; //设置navigationBar为透明

4.设置object圆角,如UIView、UIImageView等
简单几步:
一.导入QuartzCore.framework框架 #import <QuartzCore/QuartzCore.h>
二.object.layer.masksToBounds=YES;
三.object.layer.cornerRadius=XXX;
[plain] view plaincopyprint?
UIImageView *testImageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"1.jpg"]];
[testImageViewsetFrame:CGRectMake(20,20,100,100)];
testImageView.layer.masksToBounds =YES;
testImageView.layer.cornerRadius =50/2; //设置的角度,改变查看效果
testImageView.layer.borderWidth =5; //边框宽度
testImageView.layer.borderColor = [UIColoryellowColor].CGColor; //边框颜色
[self.viewaddSubview:testImageView];
[testImageViewrelease];

5.程序是启动状态时设置屏幕一直亮
UIApplication *appDelegate = [UIApplicationsharedApplication];
appDelegate.idleTimerDisabled =YES;

6.设置UITableViewCell显示子标题
初始化时:cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier];
此处样式应注意,即设置为显示子标题样式,默认样式为不显示子标题,设置完后可用:
cell.detailTextLabel.text =@"详细标签"; 设置子标题

7.给UIView设置阴影效果
[plain] view plaincopyprint?
UIView *showView = [[UIViewalloc]initWithFrame:CGRectMake(10,150,100,100)];
showView.backgroundColor = [UIColorredColor];
showView.layer.shadowOffset =CGSizeMake(15,5);
showView.layer.shadowOpacity =0.6;
showView.layer.shadowColor = [UIColorblackColor].CGColor;
[self.viewaddSubview:showView];
[showViewrelease];

8.设置UITableView选中行数的位置
[self.tableViewselectRowAtIndexPath:[NSIndexPathindexPathForRow:5inSection:0]animated:NOscrollPosition:UITableViewScrollPositionMiddle];
//selectRowAtIndexPath设置行数和section scrollPosition设置滚动到的位置(4项可供选择)

9.UITextField每次操作调用的代理,可根据需求作相应改变
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSMutableString *newString = [[textField.textmutableCopy]autorelease];
[newStringreplaceCharactersInRange:rangewithString:string]; //得到改变的字符串
}

10.如何用UIColor生成UIImage
[plain] view plaincopyprint?
- (void)viewDidLoad
{
[superviewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,100,100)];
UIImage *image = [self createImageWithColor:[UIColoryellowColor]]; //生成一张黄颜色的图片
[imageViewsetImage:image];
[self.viewaddSubview:imageView];
[imageViewrelease];
}
- (UIImage *) createImageWithColor: (UIColor *) color
{
CGRect rect = CGRectMake(0.0f,0.0f,1.0f,1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context =UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [colorCGColor]);
CGContextFillRect(context, rect);
UIImage *theImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code科技狂热者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值