iOS 17个常用代码整理

来源:互联网 发布:淘宝客服聊天用语 编辑:程序博客网 时间:2024/05/10 16:33
  1. 12.判断邮箱格式是否正确的代码:  
  2. //利用正则表达式验证  
  3. -(BOOL)isValidateEmail:(NSString *)email  
  4. {  
  5.     NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";  
  6.     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];  
  7.     return [emailTest evaluateWithObject:email];  
  8. }  
  9. 13.图片压缩  
  10. 用法:UIImage *yourImage= [self imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)];  
  11. //压缩图片  
  12. - (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize  
  13. {  
  14.     // Create a graphics image context  
  15.     UIGraphicsBeginImageContext(newSize);  
  16.     // Tell the old image to draw in this newcontext, with the desired  
  17.     // new size  
  18.     [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];  
  19.     // Get the new image from the context  
  20.     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();  
  21.     // End the context  
  22.     UIGraphicsEndImageContext();  
  23.     // Return the new image.  
  24.     return newImage;  
  25. }  
  26. 14.亲测可用的图片上传代码  
  27. - (IBAction)uploadButton:(id)sender {  
  28.     UIImage *image = [UIImage imageNamed:@"1.jpg"]; //图片名  
  29.     NSData *imageData = UIImageJPEGRepresentation(image,0.5);//压缩比例  
  30.     NSLog(@"字节数:%i",[imageData length]);  
  31.     // post url  
  32.     NSString *urlString = @"http://192.168.1.113:8090/text/UploadServlet";  
  33.     //服务器地址  
  34.     // setting up the request object now  
  35.     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;  
  36.     [request setURL:[NSURL URLWithString:urlString]];  
  37.     [request setHTTPMethod:@"POST"];  
  38.     //  
  39.     NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];  
  40.     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data;boundary=%@",boundary];  
  41.     [request addValue:contentType forHTTPHeaderField: @"Content-Type"];  
  42.     //  
  43.     NSMutableData *body = [NSMutableData data];  
  44.     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
  45.     [body appendData:[[NSString stringWithString:@"Content-Disposition:form-data; name=\"userfile\"; filename=\"2.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; //上传上去的图片名字  
  46.     [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
  47.     [body appendData:[NSData dataWithData:imageData]];  
  48.     [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];  
  49.     [request setHTTPBody:body];  
  50.     // NSLog(@"1-body:%@",body);  
  51.     NSLog(@"2-request:%@",request);  
  52.     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];  
  53.     NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];  
  54.     NSLog(@"3-测试输出:%@",returnString);  
  55.     15.给imageView加载图片  
  56.     UIImage *myImage = [UIImage imageNamed:@"1.jpg"];  
  57.     [imageView setImage:myImage];  
  58.     [self.view addSubview:imageView];  
  59.     16.对图库的操作  
  60.     选择相册:  
  61.     UIImagePickerControllerSourceTypesourceType=UIImagePickerControllerSourceTypeCamera;  
  62.     if (![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {  
  63.         sourceType=UIImagePickerControllerSourceTypePhotoLibrary;  
  64.     }  
  65.     UIImagePickerController * picker = [[UIImagePickerControlleralloc]init];  
  66.     picker.delegate = self;  
  67.     picker.allowsEditing=YES;  
  68.     picker.sourceType=sourceType;  
  69.     [self presentModalViewController:picker animated:YES];  
  70.     选择完毕:  
  71.     -(void)imagePickerController:(UIImagePickerController*)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info  
  72.     {  
  73.         [picker dismissModalViewControllerAnimated:YES];  
  74.         UIImage * image=[info objectForKey:UIImagePickerControllerEditedImage];  
  75.         [self performSelector:@selector(selectPic:) withObject:imageafterDelay:0.1];  
  76.     }  
  77.     -(void)selectPic:(UIImage*)image  
  78.     {  
  79.         NSLog(@"image%@",image);  
  80.         imageView = [[UIImageView alloc] initWithImage:image];  
  81.         imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);  
  82.         [self.viewaddSubview:imageView];  
  83.         [self performSelectorInBackground:@selector(detect:) withObject:nil];  
  84.     }  
  85.     detect为自己定义的方法,编辑选取照片后要实现的效果  
  86.     取消选择:  
  87.     -(void)imagePickerControllerDIdCancel:(UIImagePickerController*)picker  
  88.     {  
  89.         [picker dismissModalViewControllerAnimated:YES];  
  90.     }  
  91.     17.跳到下个View  
  92.     nextWebView = [[WEBViewController alloc] initWithNibName:@"WEBViewController" bundle:nil];  
  93.     [self presentModalViewController:nextWebView animated:YES];  
  94.       
  95.       
  96.     //创建一个UIBarButtonItem右边按钮  
  97.     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightButton)];  
  98.     [self.navigationItem setRightBarButtonItem:rightButton];  
  99.     设置navigationBar隐藏  
  100.     self.navigationController.navigationBarHidden = YES;//  
  101.     iOS开发之UIlabel多行文字自动换行 (自动折行)  
  102.     UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 180)];  
  103.     UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 150)];  
  104.     label.text = @"Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Helloworld!";  
  105.     //背景颜色为红色  
  106.     label.backgroundColor = [UIColor redColor];  
  107.     //设置字体颜色为白色  
  108.     label.textColor = [UIColor whiteColor];  
  109.     //文字居中显示  
  110.     label.textAlignment = UITextAlignmentCenter;  
  111.     //自动折行设置  
  112.     label.lineBreakMode = UILineBreakModeWordWrap;  
  113.     label.numberOfLines = 0;  
  114.     30.代码生成button  
  115.     CGRect frame = CGRectMake(0, 400, 72.0, 37.0);  
  116.     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  117.     button.frame = frame;  
  118.     [button setTitle:@"新添加的按钮" forState: UIControlStateNormal];  
  119.     button.backgroundColor = [UIColor clearColor];  
  120.     button.tag = 2000;  
  121.     [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];  
  122.     [self.view addSubview:button];  
  123.     31.让某个控件在View的中心位置显示:  
  124.     (某个控件,比如label,View)label.center = self.view.center;  
  125.     32.好看的文字处理  
  126.     以tableView中cell的textLabel为例子:  
  127.     cell.backgroundColor = [UIColorscrollViewTexturedBackgroundColor];  
  128.     //设置文字的字体  
  129.     cell.textLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:100.0f];  
  130.     //设置文字的颜色  
  131.     cell.textLabel.textColor = [UIColor orangeColor];  
  132.     //设置文字的背景颜色  
  133.     cell.textLabel.shadowColor = [UIColor whiteColor];  
  134.     //设置文字的显示位置  
  135.     cell.textLabel.textAlignment = UITextAlignmentCenter;  
  136.     33. ———————-隐藏Status Bar—————————–  
  137.     读者可能知道一个简易的方法,那就是在程序的viewDidLoad中加入  
  138.     [[UIApplication sharedApplication]setStatusBarHidden:YES animated:NO];  
  139.     33. 更改AlertView背景  
  140.       
  141.     UIAlertView *theAlert = [[[UIAlertViewalloc] initWithTitle:@"Atention"  
  142.                                                        message: @"I'm a Chinese!"  
  143.                                                       delegate:nil  
  144.                                              cancelButtonTitle:@"Cancel"  
  145.                                              otherButtonTitles:@"Okay",nil] autorelease];  
  146.     [theAlert show];  
  147.     UIImage *theImage = [UIImageimageNamed:@"loveChina.png"];  
  148.     theImage = [theImage stretchableImageWithLeftCapWidth:0topCapHeight:0];  
  149.     CGSize theSize = [theAlert frame].size;  
  150.     UIGraphicsBeginImageContext(theSize);  
  151.     [theImage drawInRect:CGRectMake(5, 5, theSize.width-10, theSize.height-20)];//这个地方的大小要自己调整,以适应alertview的背景颜色的大小。  
  152.     theImage = UIGraphicsGetImageFromCurrentImageContext();  
  153.     UIGraphicsEndImageContext();  
  154.     theAlert.layer.contents = (id)[theImage CGImage];  
  155.     34. 键盘透明  
  156.     textField.keyboardAppearance = UIKeyboardAppearanceAlert;  
  157.       
  158.     状态栏的网络活动风火轮是否旋转  
  159.     [UIApplication sharedApplication].networkActivityIndicatorVisible,默认值是NO。  
  160.       
  161.     35截取屏幕图片  
  162.     //创建一个基于位图的图形上下文并指定大小为CGSizeMake(200,400)  
  163.     UIGraphicsBeginImageContext(CGSizeMake(200,400));  
  164.       
  165.     //renderInContext 呈现接受者及其子范围到指定的上下文  
  166.     [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];  
  167.     //返回一个基于当前图形上下文的图片  
  168.     UIImage *aImage = UIGraphicsGetImageFromCurrentImageContext();  
  169.     //移除栈顶的基于当前位图的图形上下文  
  170.     UIGraphicsEndImageContext();  
  171.     //以png格式返回指定图片的数据  
  172.     imageData = UIImagePNGRepresentation(aImage);  
  173.     36更改cell选中的背景  
  174.     UIView *myview = [[UIView alloc] init];  
  175.     myview.frame = CGRectMake(0, 0, 320, 47);  
  176.     myview.backgroundColor = [UIColorcolorWithPatternImage:[UIImage imageNamed:@"0006.png"]];  
  177.     cell.selectedBackgroundView = myview;  
  178.     37显示图像:  
  179.     CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);  
  180.     UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];  
  181.     [myImage setImage:[UIImage imageNamed:@"myImage.png"]];  
  182.     myImage.opaque = YES; //opaque是否透明  
  183.     [self.view addSubview:myImage];  
  184.     38.能让图片适应框的大小(没有确认)  
  185.     NSString*imagePath = [[NSBundle mainBundle] pathForResource:@"XcodeCrash"ofType:@"png"];  
  186.     UIImage *image = [[UIImage alloc]initWithContentsOfFile:imagePath];  
  187.     UIImage *newImage= [image transformWidth:80.f height:240.f];  
  188.     UIImageView *imageView = [[UIImageView alloc]initWithImage:newImage];  
  189.     [newImagerelease];  
  190.     [image release];  
  191.     [self.view addSubview:imageView];  
  192.     39.实现点击图片进行跳转的代码:生成一个带有背景图片的button,给button绑定想要的事件!  
  193.     UIButton *imgButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 120, 120)];  
  194.     [imgButton setBackgroundImage:(UIImage *)[self.imgArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];  
  195.     imgButton.tag=[indexPath row];  
  196.     [imgButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];  
原创粉丝点击