当前位置: 首页>编程语言>正文

iOS Tips

开发中 可能会用到的一些 方法

关于图片处理(UIImage)

将颜色转化为图片

 - (UIImage *)createImageWithColor:(UIColor *) color
   {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
   }

修改图片大小_ OC

   - (UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)itemSize
{
    UIImage *i;
    // 创建一个bitmap的context,并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(itemSize);
    CGRect imageRect=CGRectMake(0, 0, itemSize.width, itemSize.height);
    // 绘制改变大小的图片
    [img drawInRect:imageRect];
    // 从当前context中创建一个改变大小后的图片
    i=UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
     UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return i;
}

修改图片大小-Swift

func scaleImage(img:UIImage,itemSize:CGSize)->UIImage
{
    var  images=UIImage()
    // 创建一个bitmap的context,并把它设置成为当前正在使用的context
        UIGraphicsBeginImageContext(itemSize)
     // 绘制改变大小的图片
     images.drawInRect(CGRectMake(0, 0, itemSize.width, itemSize.height))
        // 从当前context中创建一个改变大小后的图片
        images = UIGraphicsGetImageFromCurrentImageContext()
        // 使当前的context出堆栈
        UIGraphicsEndImageContext()
        // 返回新的改变大小后的图片
    
    return images
}

裁剪图片圆角_Swift

UIImage的方法
func clipImgRectCorner(direction: UIRectCorner, cornerRadius: CGFloat)->UIImage{
    
    // 1.开启位图上下文,跟图片尺寸一样大
    UIGraphicsBeginImageContextWithOptions(self.size, false, 0);
    let cornerSize = CGSizeMake(cornerRadius, cornerRadius)
    // 2.1创建圆形的路径
    let path = UIBezierPath(roundedRect:CGRectMake(0, 0,size.width,size.height), byRoundingCorners: direction, cornerRadii: cornerSize)
    // 2.2把路径设置为裁剪区域
    path.addClip()
    // 3.绘制图片
    self.drawAtPoint(CGPointZero);
    // 4.从上下文中获取图片
    let clipImage = UIGraphicsGetImageFromCurrentImageContext()
    // 5.关闭上下文
    UIGraphicsEndImageContext();
    
return clipImage
}

裁剪图片圆角_OC

- (UIImage *)clipImgRectCorner:(UIRectCorner)direction cornerRadius:(CGFloat) cornerRadius{

// 1.开启位图上下文,跟图片尺寸一样大
UIGraphicsBeginImageContextWithOptions(self.size, false, 0);
CGSize cornerSize = CGSizeMake(cornerRadius, cornerRadius);
NSLog(@"%.2f",self.size.height);
// 2.1 创建圆形的路径
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0,self.size.width,self.size.height) byRoundingCorners:direction cornerRadii:cornerSize];
// 2.2把路径设置为裁剪区域
[path addClip];
// 3.绘制图片
[self drawAtPoint:CGPointZero];
// 4.从上下文中获取图片
UIImage * clipImage = UIGraphicsGetImageFromCurrentImageContext();
// 5.关闭上下文
UIGraphicsEndImageContext();
return clipImage;
}

关于字符串(NSString)

去掉空格或换行

  - (NSString *)removeSpaceAndNewline:(NSString *)str
  {
        NSString *temp = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
        temp = [temp stringByReplacingOccurrencesOfString:@"\r" withString:@""];
        temp = [temp stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        return temp;
  }

判断当前url是否合法

- (BOOL)isCurrentURLAvaliable:(NSString *)urlstring
{
    if (![self isCurrentStringAvailable:urlstring]) {
        return NO;
    }
    NSString * urlFormat = @"^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",urlFormat];
    if ([predicate evaluateWithObject:urlstring]){
        return YES;
    }
    return NO;
}

验证邮箱是否合法

-  (BOOL) isEmail: (NSString *)emaile
 {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:emaile];
}

判断一个字符串是否可用

- (BOOL)isCurrentStringAvailable:(NSString *)string
{
    BOOL available = NO;
    if (string && [string isKindOfClass:[NSString class]] && (![string isEqualToString:@""]) && (![string isEqual:[NSNull null]]) && ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] > 0)){
        available = YES;
    }
    return available;
}

判断一个字符串是否是整型数字

- (BOOL)isPureInt:(NSString *)string
{
    BOOL isPure;
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    isPure = [scan scanInt:&val] && [scan isAtEnd];
    return isPure;
}

判断一个字符串是否是浮点型数字

- (BOOL)isPureFloat:(NSString *)string
{
    BOOL isPure;
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    isPure = [scan scanFloat:&val] && [scan isAtEnd];
    return isPure;
}

判断当前手机号是否正确

- (BOOL)isCurrentMobileNumAvailable:(NSString *)mobileStr
{
    //字符串是否可用 在上面
    if (![self isCurrentStringAvailable:mobileStr]){
        return NO;
    }

    if ([mobileStr length]!= 11){
        return NO;
    }
     NSString *ALL = @"^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}|^19[8,9]\d{8}|^16[6]\d{8}$";

     NSPredicate *regextestall = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", ALL];

    return [regextestall evaluateWithObject:mobileStr];
}

判断一个字符串是否包含中文

-(BOOL)isCurrentIncludeChinese:(NSString *)str
{
    for(int i=0; i< [str length];i++)
    {
        int a = [str characterAtIndex:i];
        if( a >=0x4e00 && a <= 0x9fff)
        {
            return YES;
        }
    }
    return NO;
}

判断是否全部为中文

-(BOOL)isCurrentIsChinese:(NSString *)str
{
    for(int i=0; i< [str length];i++)
    {
        int a = [str characterAtIndex:i];
        if( a >= 0x4e00 && a <= 0x9fff)
        {
          
        }else{
          return  NO;
        }
    
    }
    return YES;
}

UILable 相关

根据字体大小 和 高度 计算宽度

/**
 @prame content 内容
 @prame fontSize 字体大小
 @prame height 高度
 @return 宽度
 */
+ (CGFloat)calculateLableWidth:(NSString *)content
                      fontSize:(NSUInteger)fontSize
                        height:(CGFloat)height{
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
    CGRect rect = [content boundingRectWithSize:CGSizeMake(0, height)
                                        options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                     attributes:dic context:nil];
    return rect.size.width;
}

根据字体大小 和 宽度 计算高度

/**
 @prame content 内容
 @prame fontSize 字体大小
 @prame height 宽度
 @return 高度
 */
+ (CGFloat)calculateLableHeight:(NSString *)content
                       fontSize:(NSInteger)fontSize
                          width:(CGFloat)width{
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
    CGRect rect = [content boundingRectWithSize:CGSizeMake(width, 0)
                                        options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                     attributes:dic context:nil];
    return rect.size.height;
}

https://www.xamrdz.com/lan/5a32016634.html

相关文章: