| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- //
- // NSString+Addition.m
- // PinChaoPhone
- //
- // Created by 克奎 岳 on 15/9/1.
- // Copyright (c) 2015年 LSY. All rights reserved.
- //
- #import "NSString+Addition.h"
- #define RGB_COLOR(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
- @implementation NSString (Addition)
- - (CGFloat)commonStringWidthForFont:(CGFloat)fontSize
- {
- CGFloat width = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size.width;
- return width;
- }
- - (CGFloat)commonStringHeighforLabelWidth:(CGFloat)width withFontSize:(CGFloat)fontSize
- {
- CGFloat heigh = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size.height;
- return heigh;
- }
- - (CGPoint)commonStringLastPointWithLabelFrame:(CGRect)frame withFontSize:(CGFloat)fontSize;
- {
- CGPoint lastPoint;
- CGSize sz = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
- CGSize lineSize = [self boundingRectWithSize:CGSizeMake(frame.size.width, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
- if(sz.width <= lineSize.width) //判断是否折行
- {
- lastPoint = CGPointMake(frame.origin.x + sz.width, frame.origin.y);
- }
- else
- {
- lastPoint = CGPointMake(frame.origin.x + (int)sz.width % (int)lineSize.width,lineSize.height + sz.height);
- }
- return lastPoint;
- }
- //判断字符串,正责表达式
- - (BOOL)isUserName
- {
- NSString * regex = @"(^[A-Za-z0-9]{3,20}$)";
- NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
-
- return [pred evaluateWithObject:self];
- }
- - (BOOL)isPassword
- {
- NSString * regex = @"(^[A-Za-z0-9]{6,20}$)";
- NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
-
- return [pred evaluateWithObject:self];
- }
- - (BOOL)isEmail
- {
- NSString * regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
- NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
-
- return [pred evaluateWithObject:self];
- }
- - (BOOL)isUrl
- {
- NSString * regex = @"http(s)?:\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- .\\/?%&=]*)?";
- NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
-
- return [pred evaluateWithObject:self];
- }
- - (BOOL)isTelephone
- {
- // NSString * MOBILE = @"^1(2[0-9]|3[0-9]|4[0-9]|5[0-9]|6[0-9]|7[0-9]|8[0-9])\\d{8}$";
- // NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
- // NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
- // NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
- // NSString * PHS = @"^0(10|2[0-5789]|\\d{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];
- // NSPredicate *regextestphs = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
- //
- // return [regextestmobile evaluateWithObject:self] ||
- // [regextestphs evaluateWithObject:self] ||
- // [regextestct evaluateWithObject:self] ||
- // [regextestcu evaluateWithObject:self] ||
- // [regextestcm evaluateWithObject:self];
- if(!StrValid(self))
- {
- return NO;
- }
- return YES;
- }
- - (BOOL) isidentityCard
- {
- NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
- NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
- return [identityCardPredicate evaluateWithObject:self];
- }
- /**
- * 判断字符串是否为空
- *
- * @return BOOL
- */
- - (BOOL)isEmpty
- {
- if (self == nil || self == NULL) {
- return YES;
- }
- if ([self isKindOfClass:[NSNull class]]) {
- return YES;
- }
- if ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
- return YES;
- }
- return NO;
- }
- // 在文本中间添加横划线
- - (NSMutableAttributedString *)addTextCenterLine
- {
- NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self];
-
- [attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, self.length)];
- [attrString addAttribute:NSStrikethroughColorAttributeName value:RGB_COLOR(168, 168, 170) range:NSMakeRange(0, self.length)];
-
- return attrString;
- }
- //纯数字
- - (BOOL)isNumber
- {
- NSString * regex = @"^[0-9]*$";
- NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
-
- return [pred evaluateWithObject:self];
- }
- +(NSString *)showTimeStrFormDate:(NSDate*)compareDate
- {
-
- if( compareDate == nil ) return @"";
-
- NSTimeInterval timeInterval = [compareDate timeIntervalSinceNow];
- timeInterval = -timeInterval;
- long temp = timeInterval;
- NSString *result;
-
- if (timeInterval < 60) {
- if( temp == 0 )
- result = ASLocalizedString(@"刚刚");
- else
- result = [NSString stringWithFormat:ASLocalizedString(@"%d秒前"),(int)temp];
- }
- else if(( timeInterval/60) <60){
- result = [NSString stringWithFormat:ASLocalizedString(@"%d分钟前"),(int)temp/60];
- }
-
- else if(( temp/86400) <30){
-
- NSDateFormatter *date = [[NSDateFormatter alloc] init];
- [date setDateFormat:@"dd"];
- NSString *str = [date stringFromDate:[NSDate date]];
- int nowday = [str intValue];
-
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"dd"];
- NSString *strDate = [dateFormatter stringFromDate:compareDate];
- int day = [strDate intValue];
- if (nowday-day==0) {
- [dateFormatter setDateFormat:ASLocalizedString(@"今天 HH:mm")];
- result = [dateFormatter stringFromDate:compareDate];
- }
- else if(nowday-day==1)
- {
-
- [dateFormatter setDateFormat:ASLocalizedString(@"昨天 HH:mm")];
- result = [dateFormatter stringFromDate:compareDate];
-
-
- }
-
-
- else if( temp < 8 )
- {
- if (temp==1) {
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:ASLocalizedString(@"昨天HH:mm")];
- NSString *strDate = [dateFormatter stringFromDate:compareDate];
- result = strDate;
- }
- else if(temp == 2)
- {
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:ASLocalizedString(@"前天HH:mm")];
- NSString *strDate = [dateFormatter stringFromDate:compareDate];
- result = strDate;
- }
-
- }
- else
- {
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"MM-dd HH:mm"];
- NSString *strDate = [dateFormatter stringFromDate:compareDate];
- result = strDate;
-
- }
- }
- else
- {//超过一个月的就直接显示时间了
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
- NSString *strDate = [dateFormatter stringFromDate:compareDate];
- result = strDate;
- }
-
- /*
- else if((temp = (temp/(3600*24))/30) <12){
- result = [NSString stringWithFormat:ASLocalizedString(@"%d个月前"),(int)temp];
- }
- else{
- temp = temp/12;
- result = [NSString stringWithFormat:ASLocalizedString(@"%d年前"),(int)temp];
- }
- */
-
- return result;
- }
- // 判断是不是小数2位
- +(BOOL)isDecimalNum:(NSString *)numStr
- {
- NSString *phoneRegex = @"^[0-9]+(\\.[0-9]{1,2})?$";
- NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
- return [phoneTest evaluateWithObject:numStr];
- }
- @end
|