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] view plaincopyprint?
NSString *path = [[NSBundlemainBundle]pathForResource:@"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;  
}  

猜你喜欢

转载自dreamahui.iteye.com/blog/1843567