iOS开发:多款直接COPY就用的源码,拿好不谢
2017-07-01 本文已影响174人
溪石iOS
产生随机订单号
#pragma mark - 产生随机订单号
- (NSString *)generateTradeNO {
static int kNumber = 16; // 订单号长度
NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 订单号组成字符集
NSMutableString *resultStr = [[NSMutableString alloc] init];
srand((unsigned)time(0));
for (int i = 0; i < kNumber; i++)
{
unsigned index = rand() % [sourceStr length];
NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
[resultStr appendString:oneStr];
}
return resultStr;
}
时间戳字符串
- (NSString *)stringDate:(NSDate *)date {
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *today = [dateFormatter stringFromDate:date];
return today;
}
随机颜色
+ (UIColor *)random {
CGFloat hue = (arc4random() % 256 / 256.0); // 0.0 to 1.0
CGFloat saturation = (arc4random() % 128 / 256.0) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = (arc4random() % 128 / 256.0) + 0.5; // 0.5 to 1.0, away from black
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
验证有效邮箱
- (BOOL)isValidateEmail:(NSString *)email {
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:email];
}
容量转换为易读的字符串(Size to GB)
function humanFileSize(bytes, si) {
var thresh = si ? 1000 : 1024;
if(bytes < thresh) return bytes + ' B';
var units = si ? ['kB','MB','GB','TB','PB','EB','ZB','YB'] : ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while(bytes >= thresh);
return bytes.toFixed(1)+' '+units[u];
}