美文网首页
提高开发效率用到的工具类(持续更新...)

提高开发效率用到的工具类(持续更新...)

作者: Peter_Zhang | 来源:发表于2018-06-12 15:07 被阅读16次

     这段时间开发任务比较少(也许产品快死,或许我们很牛X)有些许时间来分享一些开发中的事情和大家一起学习进步。之前我也是一名拿来主义,现在我也要懂得回馈了,闲话少叙,还是直奔主题。

     今天跟大家分享的是提高开发效率常用到的工具类(直接拿去用,不用谢):
    //计算当前月份的第一天

    +(NSString *)getFirstDayOfThisMonth
    {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *satrtDate;
    [calendar rangeOfUnit:NSCalendarUnitMonth
    startDate:&satrtDate
    interval:nil
    forDate:[NSDate date]];
    NSDateFormatter *firstDayFormatter = [[NSDateFormatter alloc] init];
    [firstDayFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *firstDay = [firstDayFormatter stringFromDate:satrtDate];
    return firstDay;
    }
    

    // 正则判断,判断输入的必须是中文

    + (BOOL)matchStringFormat:(NSString *)matchedStr withRegex:(NSString *)regex
    {
    //SELF MATCHES一定是大写
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    return [predicate evaluateWithObject:matchedStr];
    }
    

    //数组转json格式

    + (NSString *)arrayToJSONString:(NSArray *)array
    {
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
    options:NSJSONWritingPrettyPrinted
    error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData
    encoding:NSUTF8StringEncoding];
    return jsonString;
    }
    

    //字典转json格式

    + (NSString *)dictionaryToJSONString:(NSDictionary *)dictionary
    {
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
    options:NSJSONWritingPrettyPrinted
    error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData
    encoding:NSUTF8StringEncoding];
    return jsonString;
    }
    

    /**
    计算两个时间差
    @param localTime 获取本地时间
    @param endTime 结束时间
    @return 时间差
    */

    + (NSDictionary *)customTimeWithLocalTime:(NSDate *)localTime
    endTime:(NSDate *)endTime
    {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *dateTime = [formatter stringFromDate:localTime];
    NSLog(@"当前时间%@============年-月-日  时:分:秒=====================",dateTime);
    //    NSDate *endTime = [NSDate dateWithTimeInterval:timeInterval
    
    //                                        sinceDate:localTime];
    
    NSString *endTimeStr = [formatter stringFromDate:endTime];
    NSLog(@"最终时间%@============年-月-日  时:分:秒=====================",endTimeStr);
    
    //计算时间间隔(单位是秒)
    NSTimeInterval time = [endTime timeIntervalSinceDate:localTime];
    //计算天数、时、分、秒
    NSInteger days = ((NSInteger)time)/(3600*24);
    NSInteger hours = ((NSInteger)time)%(3600*24)/3600;
    NSInteger minutes = ((NSInteger)time)%(3600*24)%3600/60;
    NSInteger seconds = ((NSInteger)time)%(3600*24)%3600%60;
    NSNumber *day = [NSNumber numberWithInteger:days];
    NSNumber *hour = [NSNumber numberWithInteger:hours];
    NSNumber *minute = [NSNumber numberWithInteger:minutes];
    NSNumber *second = [NSNumber numberWithInteger:seconds];
    NSString *dateContent = [[NSString alloc] initWithFormat:@"相差时间%li天%li小时%li分%li秒",(long)days,(long)hours,(long)minutes,(long)seconds];
    NSLog(@"%@",dateContent);
    NSMutableDictionary *dateInfo = [NSMutableDictionary dictionary];
    [dateInfo setValue:day forKey:@"day"];
    [dateInfo setValue:hour forKey:@"hour"];
    [dateInfo setValue:minute forKey:@"minute"];
    [dateInfo setValue:second forKey:@"second"];
    return dateInfo;
    }
    

    //时间转换

    + (NSString *)getTimeStrWithNum:(NSNumber *)time
    {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[time longValue]/1000];
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *string = [formatter stringFromDate:date];
    return string;
    }
    

    //日期格式转换

    + (NSString *)timeSpToTimeWithDate:(NSDate *)timesp
    {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *confromTimespStr = [formatter stringFromDate:timesp];
    return confromTimespStr;
    }
    

    //图片压缩处理

    +(NSData *)imageData:(UIImage *)image
    {
    NSData *data=UIImageJPEGRepresentation(image, 1.0);
    if (data.length>100*1024)
    {
    if (data.length>5*1024*1024)
    {//5M以及以上
    data=UIImageJPEGRepresentation(image, 0.1);
    }
    else if (data.length>512*1024)
    {//0.5M-1M
    data=UIImageJPEGRepresentation(image, 0.5);
    }
    else if (data.length>200*1024)
    {//0.25M-0.5M
    data=UIImageJPEGRepresentation(image, 0.9);
    }
    data = UIImageJPEGRepresentation(image, 1.0);
    }
    return data;
    }
    

    //图片大小设置

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

    /**

    • 改变文本的字体颜色
    • @param font 字体
    • @param color 颜色
    • @param totalString 总字符串
    • @param subArray 需改变的字符串的数组
    • @return
      */
    + (NSMutableAttributedString *)changeFontAndColor:(UIFont *)font
    Color:(UIColor *)color
    TotalString:(NSString *)totalString
    SubStringArray:(NSArray *)subArray
    {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]
    initWithString:totalString];
    for (NSString *rangeStr in subArray)
    {
    NSRange range = [totalString rangeOfString:rangeStr
    options:NSBackwardsSearch];
    [attributedStr addAttribute:NSForegroundColorAttributeName
    value:color
    range:range];
    [attributedStr addAttribute:NSFontAttributeName
    value:font
    range:range];
    }
    return attributedStr;
    }
    

    /**
    文本中插入图片
    @param string 文字
    @param imageNamed 图片名称
    @param imageBounds 图片大小位置
    @param atIndex 插入的位置
    @return 返回带有图片富文本
    */

    +(NSMutableAttributedString *)lableInsertImageByString:(NSString *)string
    needInsertImageNamed:(NSString *)imageNamed
    imageBounds:(CGRect)imageBounds
    insertAtIndex:(NSInteger)atIndex
    {
    //文本中放置图片
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
    // 添加图片
    NSTextAttachment *attch = [[NSTextAttachment alloc] init];
    // 已支付图片
    attch.image = [UIImage imageNamed:imageNamed];
    // 设置图片大小
    attch.bounds = imageBounds;//CGRectMake(0, -2, 22, 18);
    // 创建带有图片的富文本
    NSAttributedString *attStr = [NSAttributedString attributedStringWithAttachment:attch];
    [attri insertAttributedString:attStr atIndex:atIndex];
    return attri;
    }
    

    以上,就是今天所分享出来的东西,希望可以帮助到大家,谢谢!

    相关文章

      网友评论

          本文标题:提高开发效率用到的工具类(持续更新...)

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