GiGaMaskTime/GIGA/Common/GiGaHelper.m

178 lines
6.7 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// GiGaHelper.m
// GIGA
//
// Created by lianxiang on 2018/8/13.
// Copyright © 2018年 com.giga.ios. All rights reserved.
//
#import "GiGaHelper.h"
@implementation GiGaHelper
+(NSString *) getNowTimeString{
NSDate *now = [NSDate date];
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
formatDate.dateFormat= @"yyyy-MM-dd hh:mm:ss";
NSString *dayStr = [formatDate stringFromDate:now];
return dayStr;
}
+(long) timeStampWithDate:(NSDate *) timeDate{
long timeStamp = 0;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *timeString = [formatter stringFromDate:timeDate];
NSDate *date = [formatter dateFromString:timeString];
timeStamp = (long)[date timeIntervalSince1970];
return timeStamp;
}
+ (NSString *) dateWithTimeStamp:(long) longValue{
long value = longValue;
NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
formatDay.dateFormat = @"yyyy-MM-dd hh:mm:ss";
NSNumber *time = [NSNumber numberWithLong:value];
//转换成NSTimeInterval
NSTimeInterval nsTimeInterval = [time longValue];
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:nsTimeInterval];
NSString *dayStr = [formatDay stringFromDate:date];
return dayStr;
}
+(long)getFutureTimetstamp:(NSUInteger)hour minute:(NSUInteger)min second:(NSUInteger)second{
NSDate *datenow = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY年MM月dd日HH:mm:ss"];
NSTimeInterval timeInterval = hour * 3600 + min * 60 + second;
NSDate *futrueDate = [datenow dateByAddingTimeInterval:timeInterval];
NSString *futrueTimeStr = [formatter stringFromDate:futrueDate];
NSDate *furtureformatDate = [formatter dateFromString:futrueTimeStr];
long futrueTimestamp= [furtureformatDate timeIntervalSince1970];
return futrueTimestamp;
}
+(long)getNowDateTimestamp{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
[formatter setDateFormat:@"YYYY年MM月dd日HH:mm:ss"];
//现在时间,你可以输出来看下是什么格式
NSDate *datenow = [NSDate date];
//----------将nsdate按formatter格式转成NSString
NSString *currentTimeString_1 = [formatter stringFromDate:datenow];
NSDate *applyTimeString_1 = [formatter dateFromString:currentTimeString_1];
long nowTimeSp = (long)[applyTimeString_1 timeIntervalSince1970];
return nowTimeSp;
}
+(NSString *)stringWithNSTimerinterval:(NSTimeInterval)interval{
NSInteger hour = interval / (60*60);
NSInteger min = interval / 60;
NSInteger sec = (NSInteger) interval % 60;
return [NSString stringWithFormat:@"%02ld:%02ld:%02ld",hour,min,sec];
}
//计算文字size
+ (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
}
+(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font maxSize:(CGSize)maxSize {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = 6;
paraStyle.hyphenationFactor = 1.0;
paraStyle.firstLineHeadIndent = 0.0;
paraStyle.paragraphSpacingBefore = 0.0;
paraStyle.headIndent = 0;
paraStyle.tailIndent = 0;
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
};
CGSize size =[str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
return size.height;
}
+(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = 6; //设置行间距
paraStyle.hyphenationFactor = 1.0;
paraStyle.firstLineHeadIndent = 0.0;
paraStyle.paragraphSpacingBefore = 0.0;
paraStyle.headIndent = 0;
paraStyle.tailIndent = 0;
//设置字间距 NSKernAttributeName:@1.5f
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
};
NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
label.attributedText = attributeStr;
}
+(BOOL)isPhoneNumber:(NSString*)mobileNumber{
if (mobileNumber.length != 11)
{
return NO;
}
/** * 手机号码: * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9] * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705 * 联通号段: 130,131,132,155,156,185,186,145,176,1709 * 电信号段: 133,153,180,181,189,177,1700 */
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\\d{8}$";
/** * 中国移动China Mobile * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705 */
NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
/** * 中国联通China Unicom * 130,131,132,155,156,185,186,145,176,1709 */
NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)"; /** * 中国电信China Telecom * 133,153,180,181,189,177,1700 */
NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";
/** 25 * 大陆地区固话及小灵通 26 * 区号010,020,021,022,023,024,025,027,028,029 27 * 号码:七位或八位 28 */ //
// NSString * PHS = @"^(0[0-9]{2})\\d{8}$|^(0[0-9]{3}(\\d{7,8}))$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
if (([regextestmobile evaluateWithObject:mobileNumber] == YES) || ([regextestcm evaluateWithObject:mobileNumber] == YES) || ([regextestct evaluateWithObject:mobileNumber] == YES) || ([regextestcu evaluateWithObject:mobileNumber] == YES)) {
return YES;
} else {
return NO;
}
}
@end