记在前面:以前做金融类的APP开发,有封装了一套浮点类数据的处理,比如添加分隔符:10000>>10,000。更多处理单独放一篇文章(我就是传送门)
1.字符串字节长度(包括中英文混合)
+(NSUInteger)stringByteLength:(NSString *)text {
int strlength = 0;
char *p = (char *)[text cStringUsingEncoding:NSUnicodeStringEncoding];
for (int i=0; i < [text lengthOfBytesUsingEncoding:NSUnicodeStringEncoding]; i++) {
if (*p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}
2.正则表达式(限制只能输入数字、字母、汉字)
+ (BOOL)isQualifiedString:(NSString *)string {
NSString *codeRegex = @"^[0-9a-zA-Z\u4e00-\u9fa5]*$";
NSPredicate *codeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",codeRegex];
return [codeTest evaluateWithObject:string];
}
3.正则表达式(身份证验证)
+ (BOOL)isIDCard:(NSString *)idCard{
NSString *codeRegex = @"^([0-9]{17}[0-9Xx]{1})|([0-9]{15})$";
NSPredicate *codeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",codeRegex];
return [codeTest evaluateWithObject:idCard];
}
4.时间戳转换成几分钟前的格式
+ (NSString *)transformMinutesAgoTimestamp:(NSInteger)timestamp{
NSDateFormatter *dateFormtter =[[NSDateFormatter alloc] init];
NSDate *d = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSTimeInterval late=[d timeIntervalSince1970]*1; //转记录的时间戳
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval now=[dat timeIntervalSince1970]*1; //获取当前时间戳
NSString *timeString=@"";
NSTimeInterval cha=now-late;
// 发表在一小时之内
if (cha/3600<1) {
if (cha/60<1) {
timeString = @"1";
}
else
{
timeString = [NSString stringWithFormat:@"%f", cha/60];
timeString = [timeString substringToIndex:timeString.length-7];
}
timeString=[NSString stringWithFormat:@"%@分钟前", timeString];
}
// 在一小时以上24小以内
else if (cha/3600>1&&cha/86400<1) {
timeString = [NSString stringWithFormat:@"%f", cha/3600];
timeString = [timeString substringToIndex:timeString.length-7];
timeString=[NSString stringWithFormat:@"%@小时前", timeString];
}
// 发表在24以上10天以内
else if (cha/86400>1&&cha/86400*3<1) //86400 = 60(分)*60(秒)*24(小时) 3天内
{
timeString = [NSString stringWithFormat:@"%f", cha/86400];
timeString = [timeString substringToIndex:timeString.length-7];
timeString=[NSString stringWithFormat:@"%@天前", timeString];
}
// 发表时间大于10天
else
{
[dateFormtter setDateFormat:@"yyyy/MM/dd"];
timeString = [dateFormtter stringFromDate:d];
}
return timeString;
}
5.生成二维码
+ (UIImage *)createQRUIImageFormUrl:(NSString *)qrString withSize:(CGFloat) size {
NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding];
// 创建filter
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 设置内容和纠错级别
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
CIImage *image = qrFilter.outputImage;
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 创建bitmap;
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 保存bitmap到图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
6.UIColor转UIImage
+ (UIImage *)imageWithColor:(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;
}
7.UIView转UIImage
+ (UIImage *)getImageFromView:(UIView *)view size:(CGSize)size {
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
8.创建渐变UIImage
/**
* 生成渐变UIImage
* @param startColor 开始颜色
* @param endColor 结束颜色
* @param size 生成Image的大小
* @param isHorizontal 水平/垂直方向
* @param cornerRadius 圆角量
*/
+ (UIImage *)getGradientImageStartColor:(UIColor *)startColor
endColor:(UIColor *)endColor
size:(CGSize)size
isHorizontal:(BOOL)isHorizontal
cornerRadius:(CGFloat)cornerRadius {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
gradientLayer.locations = @[@0.0, @1.0];
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = isHorizontal?CGPointMake(1, 0):CGPointMake(0, 1.0);
gradientLayer.frame = CGRectMake(0, 0, size.width, size.height);
gradientLayer.masksToBounds = YES;
gradientLayer.cornerRadius = cornerRadius;
[view.layer addSublayer:gradientLayer];
UIGraphicsBeginImageContextWithOptions(view.size, NO, 0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
9.绘制虚线
+ (UIView *)initDottedLineRect:(CGRect)rect lineSpace:(int)space lineWidth:(int)width{
UIView *line = [[UIView alloc] initWithFrame:rect];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:line.bounds];
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(line.frame) / 2, CGRectGetHeight(line.frame))];
[shapeLayer setFillColor:[UIColor clearColor].CGColor];
// 设置虚线颜色为blackColor
[shapeLayer setStrokeColor:NormColor_LineGray.CGColor];
// 设置虚线宽度
[shapeLayer setLineWidth:CGRectGetHeight(line.frame)];
[shapeLayer setLineJoin:kCALineJoinRound];
// 设置线宽,线间距
[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:width], [NSNumber numberWithInt:space], nil]];
// 设置路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL, CGRectGetWidth(line.frame), 0);
[shapeLayer setPath:path];
CGPathRelease(path);
// 把绘制好的虚线添加上来
[line.layer addSublayer:shapeLayer];
return line;
}
10.字符串是否为空
+ (BOOL)isEmptyString:(NSString *)string{
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if (![string isKindOfClass:[NSString class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
if ([string isEqualToString:@""]) {
return YES;
}
return NO;
}
11.获取APP当前版本号
+ (NSString *)appCurrentVersion {
NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
NSString *appVersion = [infoDict valueForKey:@"CFBundleShortVersionString"];
return appVersion;
}
12.手机号添加空格
+ (NSString *)addSpaceForPhone:(NSString *)phone {
if ([phone isKindOfClass:[NSString class]]) {
phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableString * str = [[NSMutableString alloc ] initWithString:phone];
if (phone.length >= 8) {
[str insertString:@" " atIndex:7];
}
if (phone.length >= 4) {
[str insertString:@" " atIndex:3];
}
if (str.length > 13 ) {
return [str substringToIndex:13];
}
return str;
}
return @"";
}
13.获取当前控制器
+ (UIViewController *)getCurrentController {
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
UIViewController* currentViewController = window.rootViewController;
BOOL runLoopFind = YES;
while (runLoopFind) {
if (currentViewController.presentedViewController) {
currentViewController = currentViewController.presentedViewController;
} else if ([currentViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController* )currentViewController;
currentViewController = [navigationController.childViewControllers lastObject];
} else if ([currentViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController* )currentViewController;
currentViewController = tabBarController.selectedViewController;
} else {
NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
if (childViewControllerCount > 0) {
currentViewController = currentViewController.childViewControllers.lastObject;
// 如果有addChildViewController的话,需要过滤子控制器,否则pop到子控制器会崩溃
if ([currentViewController isKindOfClass:[XXXXViewController class]]) {
currentViewController = currentViewController.parentViewController;
}
return currentViewController;
} else {
// 如果有addChildViewController的话,需要过滤子控制器,否则pop到子控制器会崩溃
if ([currentViewController isKindOfClass:[XXXXViewController class]]) {
currentViewController = currentViewController.parentViewController;
}
return currentViewController;
}
}
}
return currentViewController;
}