美文网首页
iOS oc-工具集合(1)

iOS oc-工具集合(1)

作者: 青菜白玉堂 | 来源:发表于2018-07-10 12:01 被阅读37次

处理数字

/**
 *  float ==> String  12.3333  ==>  12.33    12 ==> 12
 */
+(NSString *)tool_floatReturnString:(float)num
{
    NSString * str = @"";
    
    float i=roundf(num);//对num取整
    
    if (i==num) {
        
        str =[NSString stringWithFormat:@"%.0f",i];//%.0f表示小数点后面显示0位
        
    }else{
        
        str =[NSString stringWithFormat:@"%.2f",num];//注意这里是打印num对应的值
        
    }
    
    return str;
}

根据字体长度计算控件大小

/**
 *  根据字体长度计算控件所需要的尺寸
 *  str 字体
 *  size  控件(设置控件的宽度为最大宽度与最大高度)
 *  font 控件设置的字体
 */
+(CGSize)tool_stringSize:(NSString *)str size:(CGSize )size font:(CGFloat)font{
    
    CGRect tempRect = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}context:nil];
    
    return tempRect.size;
}

距离

/**
 *  根据距离返回m,km,>99Km
 *  string 距离
 */
+(NSString *)tool_distanceWithStringMTransformKm:(NSString *)string
{
    if (string.intValue<1000) {
        
        return [NSString stringWithFormat:@"%.0fm",string.floatValue];
    }else if (string.intValue<=100000) {
        float f=[string intValue]/1000.0;
        return [NSString stringWithFormat:@"%.1fKm",f];
    }else{
        return [NSString stringWithFormat:@">99Km"];
    }
    
}

截图

/**
 *   frame是你所需要的区域大小
 */

+(UIImage *)tool_screenshotsImageFram:(CGRect)frame{
    
    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    
    UIGraphicsBeginImageContext(screenWindow.frame.size);//全屏截图,包括window
    
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    CGRect rect1 =frame;
    
    UIImage * image = [UIImage imageWithCGImage:CGImageCreateWithImageInRect([viewImage CGImage], rect1)];
    
    return image;
    
}
/**
 *  对某个view截图
 *  view  目标View
 */
+ (UIImage *)tool_viewFormInterceptionImage:(UIView*)view {
    
    UIGraphicsBeginImageContext(view.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
/**
 *  根据给定得图片,从其指定区域截取一张新得图片
 *  image  需要截取的图片
 *  rect   想要截取的位置
 */
+ (UIImage *)tool_oldInemageFromInterceptionNewImage:(UIImage *)image inRect:(CGRect)rect {
    CGImageRef sourceImageRef = [image CGImage];
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
    return newImage;
}

相关文章

网友评论

      本文标题:iOS oc-工具集合(1)

      本文链接:https://www.haomeiwen.com/subject/jdxmhftx.html