整理的一些iOS通用方法

验证号码是否符合规范(手机和座机)

- (BOOL)checkNumber:(NSString *)number{
       //验证输入的固话中不带 "-"符号
       NSString * strNum = @"^(0[0-9]{2,3})?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\\d{8}$)"; 
      //验证输入的固话中带 "-"符号
       //NSString * strNum = @"^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\\d{8}$)"; 
      NSPredicate *checktest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strNum]; 
      return [checktest evaluateWithObject:number];   
}

点击拨打电话

//1,这种方法,拨打完*****回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示
NSMutableString * str=[[NSMutableString alloc]initWithFormat:@"tel:%@",
@"186xxxx6979"];    
//            NSLog(@"str======%@",str);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

 //2,这种方法,打完*****后还会回到原来的程序,也会弹出提示,推荐这种
NSMutableString * str=[[NSMutableString alloc]initWithFormat:@"tel:%@",@"186xxxx6979"];
UIWebView * callWebview = [[UIWebView alloc]init];   
 [callWebview  loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];    
[self.view addSubview:callWebview]; 

//3,这种方法也会回去到原来的程序里(注意这里的telprompt),也会弹出提示
NSMutableString  * str=[[NSMutableString alloc]initWithFormat:@"telprompt://%@",@"186xxxx6979"];
//            NSLog(@"str======%@",str);
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:str]]

UIView跳转到控制器的方法

//首先  定义一个全局变量    UIViewController *superVC;
//然后执行这个方法
  - (UIViewController *)viewController:(UIView *)view{

    UIResponder *responder = view;
    while ((responder = [responder nextResponder]))
        if ([responder isKindOfClass: [UIViewController class]])

            return (UIViewController *)responder;
       return nil;
}
//最后,在你跳转页面的方法中
superVC = [self viewController:self];
      [superVC.navigationController pushViewController:(UIViewController) animated:YES];

获取图片上某一点的颜色

- (UIColor *)colorAtPixel:(CGPoint)point {
// Cancel if point is outside image coordinates
if(!CGRectContainsPoint(CGRectMake(0.0f,0.0f, self.size.width, self.size.height), point)) {
  return nil;
}
NSInteger pointX = trunc(point.x);
NSInteger pointY = trunc(point.y);
CGImageRef cgImage = self.CGImage;
NSUInteger width = self.size.width;
NSUInteger height = self.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
int bytesPerPixel =4;
int bytesPerRow = bytesPerPixel *1;
NSUInteger bitsPerComponent = 8
    
unsigned char pixelData[4] = {0,0,0,0};

CGContextRef context = CGBitmapContextCreate(pixelData,1,1,bitsPerComponent,bytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy);

// Draw the pixel we are interested in onto the bitmap context

CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
CGContextDrawImage(context, CGRectMake(0.0f,0.0f, (CGFloat)width, (CGFloat)height), cgImage);
CGContextRelease(context);

// Convert color values [0..255] to floats [0.0..1.0]
CGFloat red   = (CGFloat)pixelData[0] /255.0f;
CGFloat green = (CGFloat)pixelData[1] /255.0f;
CGFloat blue  = (CGFloat)pixelData[2] /255.0f;
CGFloat alpha = (CGFloat)pixelData[3] /255.0f;

return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

UIView顺时针与逆时针旋转

//顺时针
[UIViewanimateWithDuration:0.2animations:^{
      _triangle1.transform = CGAffineTransformMakeRotation(M_PI);
 }];
//逆时针
[UIView animateWithDuration:0.2 animations:^{
    _triangle1.transform = CGAffineTransformMakeRotation( M_PI / 180.0);
}];

计算字符串的长度(一个汉字长度为3)

- (NSUInteger)textLength: (NSString *) text{
       NSUInteger asciiLength = 0;
       for (NSUInteger i = 0; i < text.length; i++) { 
                     unichar uc = [text characterAtIndex: i];
               asciiLength += isascii(uc) ? 1 : 3;
    }
       NSUInteger unicodeLength = asciiLength;
       return unicodeLength;   
}

你可能感兴趣的:(整理的一些iOS通用方法)