iOS - 工具函数

1.时间 :

取得时间差:

- (double)GetStringTimeDiff:(NSString*)timeS timeE:(NSString*)timeE
{
    double timeDiff =0.0;
    
    NSDateFormatter *formatters = [[NSDateFormatteralloc] init];
    [formatters setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateS = [formattersdateFromString:timeS];
    
    NSDateFormatter *formatterE = [[NSDateFormatteralloc] init];
    [formatterE setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateE = [formatterEdateFromString:timeE];
    
    timeDiff = [dateE timeIntervalSinceDate:dateS ];
    
    return timeDiff;
}


换算成几天几小时几分钟

- (NSString *)timeFormatted:(int)totalSeconds
{
    int seconds = totalSeconds %60;
    int minutes = (totalSeconds /60) % 60;
    int hours = totalSeconds /3600;
    
    if (hours <24)
    {
        return [NSStringstringWithFormat:@"%02d小时:%02d分:%02d秒",hours, minutes, seconds];
    }
    else
    {
        int days  = totalSeconds /86400;
        int hourss = (totalSeconds /60*60) %24;
        int minutess = (totalSeconds /60) % 60;
//        int secondss = totalSeconds % 60;
        return [NSStringstringWithFormat:@"%02d天:%02d小时:%02d分", days, hourss, minutess];
    }
}




2.颜色 :

合成颜色.

+ (UIColor*)colorWithHexString:(NSString *)color
{
    NSCharacterSet* set = [NSCharacterSetwhitespaceAndNewlineCharacterSet];
    NSString* cString = [[colorstringByTrimmingCharactersInSet:set]uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cStringlength] < 6)
    {
        return [UIColorclearColor];
    }
    
    // strip 0X if it appears
    if ([cStringhasPrefix:@"0X"])
    {
        cString = [cString substringFromIndex:2];
    }
    if ([cStringhasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }
    if ([cStringlength] != 6)
    {
        return [UIColorclearColor];
    }
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location =0;
    range.length =2;
    
    //r
    NSString *rString = [cStringsubstringWithRange:range];
    
    //g
    range.location =2;
    NSString *gString = [cStringsubstringWithRange:range];
    
    //b
    range.location =4;
    NSString *bString = [cStringsubstringWithRange:range];
    
    // Scan values
    unsignedint red, green, blue, one = 1;
    [[NSScannerscannerWithString:rString] scanHexInt:&red];
    [[NSScannerscannerWithString:gString] scanHexInt:&green];
    [[NSScannerscannerWithString:bString] scanHexInt:&blue];

    constfloat r = (((float)red)   /255.0f);
    constfloat g = (((float)green) /255.0f);
    constfloat b = (((float)blue)  /255.0f);
    constfloat i = (((float)one));

    return [UIColorcolorWithRed:r green:gblue:b alpha:i];
}



3.计算高度 :

//计算高度
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:13]};
CGSize contentSize = [chartMessage.content boundingRectWithSize:CGSizeMake(200, MAXFLOAT)
                                                        options: NSStringDrawingTruncatesLastVisibleLine |
                                                                 NSStringDrawingUsesLineFragmentOrigin |
                                                                 NSStringDrawingUsesFontLeading
                                                                 attributes:attributes
                                                                 context:nil].size;

只要把这一段代码封装好就可以变成一个计算文件Label 宽高的方法了




4.三方alertView库添加参数 :

把ZSBlockAlertView这个第三方的库添加了url这个重要参数:

- (void)sdkUpdateTypeWithURL:(NSString *)url cancelButtonTitle:(NSString *)cancelStr sureButtonTitle:(NSString *)sureStr
{
    ZSBlockAlertView *alert = [[ZSBlockAlertView alloc] initWithTitle:@"游戏更新" message:@"游戏有新版本" delegate:nil cancelButtonTitle:cancelStr otherButtonTitles:sureStr, nil];
    [alert setClickHandler:^(NSInteger index) {
        if (index == 0) {
            NSURL *appstoreURL = [NSURL URLWithString:url];
            if([[UIApplication sharedApplication] canOpenURL:appstoreURL])
            {
                [[UIApplication sharedApplication] openURL:appstoreURL];
            }
        }
    }];
    [alert show];
}

调用:

[[SYSystemInfo shareSystemInfo] sdkUpdateTypeWithURL:self.forceUpDateUrl cancelButtonTitle:@"立即更新" sureButtonTitle:nil];




5.压缩图片

压缩图片,常常用来上传到服务器!

- (NSData *)imageWithImage:(UIImage*)image
          scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return UIImageJPEGRepresentation(newImage, 0.8);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值