澎湃iPad上线版本

toolKit.m 70KB

    // // toolKit.m // ThePaperDemo // // Created by Scar on 14-9-4. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "toolKit.h" #import <objc/runtime.h> #import "RegexKitLite.h" #import "AppDelegate.h" #import "UIImage+ImageEffects.h" #import <sys/xattr.h> #import "YRSideViewController.h" #import "KGStatusBar.h" #import "detailContentHomeController.h" #import "ImageContentController.h" #import "TPLiveHomeController.h" #import "topicHomeController.h" #import "sys/utsname.h" #import "TPcontentWebController.h" #import "KGModal.h" #import "MLNavigationController.h" #import "SDImageCache.h" //#import "hotAskCommentController.h" #import "topicContentController.h" //#import "myTopicCenterController.h" #import "creatTopicController.h" #import "loginPopUpController.h" #import "gambitCenterHomeController.h" //用于UIImage的imageNamed函数载入resource.bundle下的图片 NSString* Bundle(NSString* fileName) { return [NSString stringWithFormat:@"imageResource.bundle/%@", fileName]; } //Font: HiraMinProN-W6 //Font: HiraMinProN-W3 UIFont* appFont(CGFloat size,BOOL isBold) { NSString *isUseSystem = [TPUserDefault instance].isUseSystemFont; if ([isUseSystem intValue] == 0) { if (isBold) { return [UIFont boldSystemFontOfSize:size]; }else { return [UIFont systemFontOfSize:size]; } }else { if (isBold) { return [UIFont fontWithName:@"FZCYSK--GBK1-0" size:size]; }else { return [UIFont fontWithName:@"FZBIAOYSK--GBK1-0" size:size]; } } } //根据图片路径加载图片 extern UIImage* Image(NSString* imageName) { return [UIImage imageNamed:Bundle(imageName)]; } id setJsonDicToDataModel(NSDictionary *dic, Class dataClass){ int i; unsigned int propertyCount = 0; objc_property_t *propertyList = class_copyPropertyList(dataClass, &propertyCount); NSMutableArray *propertyNameList = [NSMutableArray array]; for ( i=0; i < propertyCount; i++ ) { objc_property_t *thisProperty = propertyList + i; const char* propertyName = property_getName(*thisProperty); NSString *string = [NSString stringWithFormat:@"%s",propertyName]; [propertyNameList addObject:string]; } if (propertyNameList.count > 0) { id dataModal = nil; dataModal = [[dataClass alloc]init]; [propertyNameList enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) { if (dic[key]) { [dataModal setValue:dic[key] forKey:key]; } }]; return dataModal; }else return nil; } id setDataModelToDic(id dataModel, Class dataClass){ int i; unsigned int propertyCount = 0; objc_property_t *propertyList = class_copyPropertyList(dataClass, &propertyCount); NSMutableArray *propertyNameList = [NSMutableArray array]; for ( i=0; i < propertyCount; i++ ) { objc_property_t *thisProperty = propertyList + i; const char* propertyName = property_getName(*thisProperty); NSString *string = [NSString stringWithFormat:@"%s",propertyName]; [propertyNameList addObject:string]; } if (propertyNameList.count > 0) { NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [propertyNameList enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) { if ([dataModel valueForKey:key] && ![[dataModel valueForKey:key] isKindOfClass:[NSNull class]]) { if ([[dataModel valueForKey:key] isKindOfClass:[NSString class]]) { NSString *str = [dataModel valueForKey:key]; if (!isBlankString(str)) { [dic setValue:str forKey:key]; } }else { [dic setValue:[dataModel valueForKey:key] forKey:key]; } } }]; return dic; }else return nil; } id makeRemoteModelToCoreDataModel(id remoteModel,Class remoteDataClass,NSManagedObject *coreDataModel) { int i; unsigned int propertyCount = 0; objc_property_t *propertyList = class_copyPropertyList(remoteDataClass, &propertyCount); NSMutableArray *propertyNameList = [NSMutableArray array]; for ( i=0; i < propertyCount; i++ ) { objc_property_t *thisProperty = propertyList + i; const char* propertyName = property_getName(*thisProperty); NSString *string = [NSString stringWithFormat:@"%s",propertyName]; [propertyNameList addObject:string]; } if (propertyNameList.count > 0) { [propertyNameList enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) { id obj = [remoteModel valueForKey:key]; if ([obj isKindOfClass:[NSString class]]) { [coreDataModel setValue:obj forKey:key]; }else { NSData *objData = [NSKeyedArchiver archivedDataWithRootObject:obj]; [coreDataModel setValue:objData forKey:key]; } }]; return coreDataModel; }else return nil; } id makeCoreDataModelToRemoteModel(id CoreDataModel,Class remoteDataClass) { int i; unsigned int propertyCount = 0; objc_property_t *propertyList = class_copyPropertyList(remoteDataClass, &propertyCount); NSMutableArray *propertyNameList = [NSMutableArray array]; for ( i=0; i < propertyCount; i++ ) { objc_property_t *thisProperty = propertyList + i; const char* propertyName = property_getName(*thisProperty); NSString *string = [NSString stringWithFormat:@"%s",propertyName]; [propertyNameList addObject:string]; } if (propertyNameList.count > 0) { id dataModal = [[remoteDataClass alloc]init]; [propertyNameList enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) { id obj = [CoreDataModel valueForKey:key]; if ([obj isKindOfClass:[NSString class]]) { [dataModal setValue:obj forKey:key]; }else { id objFromData = [NSKeyedUnarchiver unarchiveObjectWithData:obj]; [dataModal setValue:objFromData forKey:key]; } }]; return dataModal; }else return nil; } NSDictionary* makeCoreDataModelToDic(id CoreDataModel,Class coreDataClass) { NSMutableDictionary *tempDic = [NSMutableDictionary dictionary]; int i; unsigned int propertyCount = 0; objc_property_t *propertyList = class_copyPropertyList(coreDataClass, &propertyCount); NSMutableArray *propertyNameList = [NSMutableArray array]; for ( i=0; i < propertyCount; i++ ) { objc_property_t *thisProperty = propertyList + i; const char* propertyName = property_getName(*thisProperty); NSString *string = [NSString stringWithFormat:@"%s",propertyName]; [propertyNameList addObject:string]; } if (propertyNameList.count > 0) { [propertyNameList enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) { id obj = [CoreDataModel valueForKey:key]; if (obj) { if (![key isEqualToString:@"date"]) { if ([obj isKindOfClass:[NSString class]]) { [tempDic setValue:obj forKey:key]; }else { id objFromData = [NSKeyedUnarchiver unarchiveObjectWithData:obj]; [tempDic setValue:objFromData forKey:key]; } } } }]; return tempDic; }else return nil; } float rectScale (){ return rect_screen.size.width/320; } id makeDicToCoreDataModel(NSDictionary*dic, NSManagedObject* coreDataModel) { if (dic.allKeys.count > 0) { [dic.allKeys enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) { id obj = [dic valueForKey:key]; if ([obj isKindOfClass:[NSString class]]) { [coreDataModel setValue:obj forKey:key]; }else { id objToData = [NSKeyedArchiver archivedDataWithRootObject:obj]; [coreDataModel setValue:objToData forKey:key]; } }]; return coreDataModel; }else return nil; } float heightForString(NSString *str, UIFont* font, float width,NSLineBreakMode mode) { if (isNotIOS8) { CGSize sizeToFit = [str sizeWithFont: font constrainedToSize: CGSizeMake(width, CGFLOAT_MAX) lineBreakMode: mode]; // 可根据具体需求设置lineBreakMode return sizeToFit.height; }else { if (isBlankString(str)) { return 0; } if (font.pointSize > 11) { NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:font}]; NSRange range = NSMakeRange(0, atrString.length); NSDictionary* dic = [atrString attributesAtIndex:0 effectiveRange:&range]; CGRect rect = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil]; return rect.size.height; }else { NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:str]; NSRange range = NSMakeRange(0, atrString.length); NSDictionary* dic = [atrString attributesAtIndex:0 effectiveRange:&range]; CGRect rect = [str boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options: NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil]; return rect.size.height; } } } float heightForAttributeString(NSAttributedString *str, CGFloat width,UIFont *font) { UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, width, CGFLOAT_MAX)]; textView.attributedText = str; CGFloat height = [textView sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)].height; textView = nil; return height; } float heightForAttributeStringWithLabel(NSAttributedString *str, CGFloat width,UIFont *font) { UILabel *textView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, CGFLOAT_MAX)]; textView.textAlignment = NSTextAlignmentLeft; textView.numberOfLines = 0; textView.lineBreakMode = NSLineBreakByWordWrapping; textView.attributedText = str; CGFloat height = [textView sizeThatFits:CGSizeMake(width, CGFLOAT_MAX)].height; textView = nil; return height; } NSAttributedString* attributedStringWithHtml(NSString* html) { UIFont *font = appFont([TPUserDefault instance].contFontSize, NO); // if(isPad) { // font = appFont([TPUserDefault instance].contFontSize, NO); // } // [[GONMarkupParserManager sharedParser] registerFont:font forKey:@"appFont"]; NSAttributedString *attrString = [[GONMarkupParserManager sharedParser]attributedStringFromString:html error:nil]; return attrString; } float widthForString(NSString *str, UIFont* font, float height,NSLineBreakMode mode) { if (isNotIOS8) { CGSize sizeToFit = [str sizeWithFont: font constrainedToSize: CGSizeMake(CGFLOAT_MAX, height) lineBreakMode: mode]; // 可根据具体需求设置lineBreakMode return sizeToFit.width; }else { if (isBlankString(str)) { return 0; } NSAttributedString* atrString = [[NSAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:font}]; NSRange range = NSMakeRange(0, atrString.length); NSDictionary* dic = [atrString attributesAtIndex:0 effectiveRange:&range]; CGRect rect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil]; return rect.size.width; } } NSDate* getLocalDate(){ NSDate *date = [NSDate date]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interval = [zone secondsFromGMTForDate: date]; NSDate *localeDate = [date dateByAddingTimeInterval: interval]; return localeDate; } NSString* StringFromDate(NSDate* aDate, NSString *aFormat) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone defaultTimeZone]]; [formatter setDateFormat:aFormat]; NSString *dateString = [formatter stringFromDate:aDate]; return dateString; } NSDate* DateFromString(NSString* string, NSString* aFormat) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone:[NSTimeZone defaultTimeZone]]; [formatter setDateFormat:aFormat]; NSDate *date = [formatter dateFromString:string]; return date; } NSString* getUpdateStringFromDate(NSDate* date) { if (!date) { return @"刚刚"; } NSString *updateString = @""; long offsetTime = 0-[date timeIntervalSinceNow]; if (offsetTime <= 120) { updateString = @"刚刚"; }else if (offsetTime < 3600) { updateString = [NSString stringWithFormat:@"%ld分钟前",offsetTime/60]; }else if (offsetTime < 60*60*24) { updateString = [NSString stringWithFormat:@"%ld小时前",offsetTime/60/60]; }else if (offsetTime < 60*60*24*7) { updateString = [NSString stringWithFormat:@"%ld天前",offsetTime/60/60/24]; }else updateString = StringFromDate(date, @"yyyy-MM-dd"); return updateString; } UIImage* getSnapshotImgFromView(UIView*inputView) { UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, inputView.opaque, 0.0); [inputView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds]; // // [imageView setImage:img]; // return img; } UIImageView* customBlurSnapshotFromView(UIView *inputView) { UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, YES, 1); [inputView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageView *imageView = [[UIImageView alloc]init]; [imageView setImage:[viewImage applyBlurWithRadius:10 tintColor:[UIColor colorWithWhite:0.8 alpha:0.2] saturationDeltaFactor:1.8 maskImage:nil]]; return imageView; } UIImageView* customDarkBlurSnapshotFromView(UIView *inputView) { UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, YES, 1); [inputView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageView *imageView = [[UIImageView alloc]init]; [imageView setImage:[viewImage applyDarkEffect]]; return imageView; } UIImageView* customLightDarkBlurSnapshotFromView(UIView *inputView) { UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, YES, 1); [inputView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageView *imageView = [[UIImageView alloc]init]; [imageView setImage:[viewImage applyLightDarkEffect]]; return imageView; } NSString* getImageNameFromURL(NSString*url) { // NSArray *tempArray = [url componentsSeparatedByString:@"/"]; // __block NSString *imgName = @""; // [tempArray enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) { // if ([obj isMatchedByRegex:@"jpg"] || [obj isMatchedByRegex:@"png"] || [obj isMatchedByRegex:@"JPG"]) { // imgName = obj; // } // }]; // imgName = [imgName stringByReplacingOccurrencesOfString:@".jpg" withString:@""]; // imgName = [imgName stringByReplacingOccurrencesOfString:@".png" withString:@""]; // imgName = [imgName stringByReplacingOccurrencesOfString:@".JPG" withString:@""]; NSString *imgName = url; imgName = [imgName stringByReplacingOccurrencesOfString:@"http://" withString:@""]; imgName = [imgName stringByReplacingOccurrencesOfString:@"/" withString:@"_"]; return imgName; } NSString* fileNameFromUrl(NSString* url) { NSArray *tempArray = [url componentsSeparatedByString:@"/"]; __block NSString *fileName = @""; [tempArray enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) { if ([obj isMatchedByRegex:@"zip"]) { fileName = obj; } }]; return fileName; } BOOL ExistAtPath(NSString* fileFullPath) { return [[fileFullPath pathExtension] length] > 0 && [[NSFileManager defaultManager] fileExistsAtPath:fileFullPath]; } NSString* getImageNumFromURL(NSString*url) { NSArray *tempArray = [url componentsSeparatedByString:@"/"]; __block NSString *imgName = @""; [tempArray enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) { if ([obj isMatchedByRegex:@"jpg"] || [obj isMatchedByRegex:@"png"] || [obj isMatchedByRegex:@"JPG"]) { imgName = obj; } }]; imgName = [imgName stringByReplacingOccurrencesOfString:@".jpg" withString:@""]; imgName = [imgName stringByReplacingOccurrencesOfString:@".png" withString:@""]; imgName = [imgName stringByReplacingOccurrencesOfString:@".JPG" withString:@""]; return imgName; } CGFloat returnTextHeightWithRTLabel(NSString*text, CGFloat width, UIFont* font,CGFloat lineSpace){ RTLabel* textRTLabel = [[RTLabel alloc]initWithFrame:CGRectMake(0, 0, width, 0)]; textRTLabel.font = font; textRTLabel.textColor = [UIColor colorWithHexString:TextBlack]; textRTLabel.textAlignment = RTTextAlignmentLeft; textRTLabel.lineBreakMode = NSLineBreakByWordWrapping; textRTLabel.lineSpacing = lineSpace; textRTLabel.text = text; CGSize size = textRTLabel.optimumSize; textRTLabel = nil; return size.height; // if (isIOS9 && [[TPUserDefault instance].isUseSystemFont intValue] == 0) { // return size.height+(CGFloat)8; // /** // * bug:5043(【适配性】iOS9.0选择系统字体时,有部分列表标题消失) // */ // /** // * bug:5174(首页,头条一行文字,有空白) // */ // }else { // return size.height; // } } CGFloat returnTextWidthWithRTLabel(NSString*text, CGFloat height, UIFont* font,CGFloat lineSpace){ RTLabel* textRTLabel = [[RTLabel alloc]initWithFrame:CGRectMake(0, 0, 0, height)]; textRTLabel.textColor = [UIColor colorWithHexString:TextBlack]; textRTLabel.textAlignment = RTTextAlignmentLeft; textRTLabel.lineBreakMode = NSLineBreakByWordWrapping; textRTLabel.lineSpacing = lineSpace; textRTLabel.font = font; textRTLabel.text = text; CGSize size = textRTLabel.optimumSize; textRTLabel = nil; return size.width; } NSString* CachePath(NSString* fileName) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString* path = [paths objectAtIndex:0]; // TPLOG(@"*********%@",path); NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:path]){ if (![fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]) { // TPLOG(@"create filepath fail:%@", path); return nil; } } if (nil != [fileName lastPathComponent]) { return [path stringByAppendingFormat:@"/%@", fileName]; } else { return [path stringByAppendingString:@"/"]; } } NSString* ConfigPath(NSString* fileName) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"dataCaches"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:path]){ if (![fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]) { // TPLOG(@"create filepath fail:%@", path); return nil; } } if (AddSkipBackupAttributeToItemAtURLFile([NSURL fileURLWithPath:path])) { if (nil != [fileName lastPathComponent]) { return [path stringByAppendingFormat:@"/%@", [fileName lastPathComponent]]; } else { return [path stringByAppendingString:@"/"]; } } else { return nil; } } NSString* tempPath(NSString* fileName) { NSString* path = NSTemporaryDirectory(); // TPLOG(@"*********%@",path); path = [path stringByAppendingPathComponent:@"date"]; NSFileManager *fileManager = [NSFileManager defaultManager]; if (![fileManager fileExistsAtPath:path]){ if (![fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]) { // TPLOG(@"create filepath fail:%@", path); return nil; } } if (nil != [fileName lastPathComponent]) { return [path stringByAppendingFormat:@"/%@", fileName]; } else { return [path stringByAppendingString:@"/"]; } } //获取资源全路径 NSString* ResourcePath(NSString* fileName) { return [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"/%@", fileName]; } BOOL AddSkipBackupAttributeToItemAtURLFile(NSURL* URL) { const char* filePath = [[URL path] fileSystemRepresentation]; const char* attrName = "com.apple.MobileBackup"; u_int8_t attrValue = 1; int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0); return result == 0; } //弹出消息框来显示消息 void ShowMessage(NSString* message,BOOL isSuccess){ if (![message isKindOfClass:[NSString class]]||message.length<=0) { return; } // AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES]; if (isSuccess){ hud.customView = [[UIImageView alloc] initWithImage:Image(@"tabBarButton/rightArrow.png")]; }else { hud.customView = [[UIImageView alloc] initWithImage:Image(@"tabBarButton/wrongArrow.png")]; } // Configure for text only and offset down hud.mode = MBProgressHUDModeCustomView; UIColor *hudColor = [UIColor colorWithRed:159.f/255.f green:159.f/255.f blue:159.f/255.f alpha:0.9f]; hud.color = hudColor; hud.cornerRadius = 4; hud.userInteractionEnabled = NO; hud.labelFont = appFont(TEXT_FOUR_LEVELSIZE, NO); hud.labelText = message; // hud.opacity = 0.8; // hud.margin = 25.f; hud.removeFromSuperViewOnHide = YES; hud.square = YES; // hud.alpha = 0.8; // hud.yOffset = [delegate window].frame.size.height/2-25; [hud hide:YES afterDelay:1]; } //弹出消息框来显示消息 void ShowMessageInPop(NSString* message,BOOL isSuccess,UIView *popView){ if (![message isKindOfClass:[NSString class]]||message.length<=0) { return; } MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:popView animated:YES]; if (isSuccess){ hud.customView = [[UIImageView alloc] initWithImage:Image(@"tabBarButton/rightArrow.png")]; }else { hud.customView = [[UIImageView alloc] initWithImage:Image(@"tabBarButton/wrongArrow.png")]; } // Configure for text only and offset down hud.mode = MBProgressHUDModeCustomView; UIColor *hudColor = [UIColor colorWithRed:159.f/255.f green:159.f/255.f blue:159.f/255.f alpha:0.9f]; hud.color = hudColor; hud.cornerRadius = 4; hud.userInteractionEnabled = NO; hud.detailsLabelFont = appFont(TEXT_FOUR_LEVELSIZE, NO); hud.detailsLabelText = message; // hud.opacity = 0.8; // hud.margin = 25.f; hud.removeFromSuperViewOnHide = YES; hud.square = YES; // hud.alpha = 0.8; // hud.yOffset = [delegate window].frame.size.height/2-25; [hud hide:YES afterDelay:1]; } //弹出消息框来显示消息 void ShowTextMessageInPop(NSString* message,UIView* popView){ if (![message isKindOfClass:[NSString class]]||message.length<=0) { return; } MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:popView animated:YES]; // Configure for text only and offset down hud.mode = MBProgressHUDModeText; hud.userInteractionEnabled = NO; UIColor *hudColor = [UIColor colorWithRed:159.f/255.f green:159.f/255.f blue:159.f/255.f alpha:0.9f]; hud.color = hudColor; hud.cornerRadius = 4; hud.detailsLabelFont = appFont(TEXT_FOUR_LEVELSIZE, NO); hud.detailsLabelText = message; hud.margin = 10.f; hud.removeFromSuperViewOnHide = YES; hud.yOffset = popView.frame.size.height/2-25; [hud hide:YES afterDelay:1]; } void ShowTextMessage(NSString* message){ if (![message isKindOfClass:[NSString class]]||message.length<=0) { return; } MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES]; // Configure for text only and offset down hud.mode = MBProgressHUDModeText; hud.userInteractionEnabled = NO; UIColor *hudColor = [UIColor colorWithRed:159.f/255.f green:159.f/255.f blue:159.f/255.f alpha:0.9f]; hud.color = hudColor; hud.cornerRadius = 4; hud.detailsLabelFont = appFont(TEXT_FOUR_LEVELSIZE, NO); hud.detailsLabelText = message; hud.margin = 10.f; hud.removeFromSuperViewOnHide = YES; hud.yOffset = KEY_WINDOW.frame.size.height/2-rect_screen.size.height/3; [hud hide:YES afterDelay:1]; } void ShowTextMessageWithDelay(NSString* message,NSInteger delayTime){ if (![message isKindOfClass:[NSString class]]||message.length<=0) { return; } MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES]; // Configure for text only and offset down hud.mode = MBProgressHUDModeText; hud.userInteractionEnabled = NO; UIColor *hudColor = [UIColor colorWithRed:159.f/255.f green:159.f/255.f blue:159.f/255.f alpha:0.9f]; hud.color = hudColor; hud.cornerRadius = 4; /** * bug:5032(tips很长被截断了,显示不全) */ hud.detailsLabelFont = appFont(TEXT_FOUR_LEVELSIZE, NO); hud.detailsLabelText = message; hud.margin = 10.f; hud.removeFromSuperViewOnHide = YES; hud.yOffset = KEY_WINDOW.frame.size.height/2-rect_screen.size.height/3; [hud hide:YES afterDelay:delayTime]; } void showStatusText(NSString *text) { // [KGStatusBar showWithStatus:text]; // //设置1s延迟 // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // [KGStatusBar dismiss]; // }); } void showStatusTextNotDismiss(NSString *text) { // [KGStatusBar showWithStatus:text]; } void needGestureOfSideController(BOOL isNeed) { AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; YRSideViewController *sideController=[delegate sideViewController]; sideController.needSwipeShowMenu = isNeed; } BOOL isBlankString(NSString *string) { if (string == nil || string == NULL) { return YES; } if ([string isKindOfClass:[NSNull class]]) { return YES; } if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) { return YES; } return NO; } void addHaveReadFlag(NSString* contId) { NSError *error; NSFetchRequest *request = [[NSFetchRequest alloc]init]; NSFetchRequest *requestAll = [[NSFetchRequest alloc]init]; //为已创建好的实体利用检索到的上下文创建一个实体描述 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"HaveReadContentList" inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext]; [request setEntity:entityDescription]; [requestAll setEntity:entityDescription]; //确定持久库中是否存在与此字段相对应的托管对象,所以穿件一个谓词来确定字段的正确对象: NSPredicate *pred = [NSPredicate predicateWithFormat:@"contId == %@",contId]; [request setPredicate:pred]; HaveReadContentList *haveReadContentList = nil; NSArray *objs = [[CoreDataManager shareInstance].managedObjectContext executeFetchRequest:request error:&error]; NSArray *allObjs = [[CoreDataManager shareInstance].managedObjectContext executeFetchRequest:requestAll error:&error]; if (objs == nil) { TPLOG(@"there was an error!!"); } if (objs.count == 0) { if (allObjs.count == 100) { HaveReadContentList *earliestObj = nil; earliestObj = [allObjs objectAtIndex:0]; [[CoreDataManager shareInstance].managedObjectContext deleteObject:earliestObj]; } haveReadContentList = [NSEntityDescription insertNewObjectForEntityForName:@"HaveReadContentList" inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext]; haveReadContentList.contId = contId; haveReadContentList.date = getLocalDate(); [[CoreDataManager shareInstance] saveContext]; } } UIImage* imageWithUIColor(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; } void SaveFile(NSString* fileName, NSData* data) { NSFileManager *manager = [NSFileManager defaultManager]; [manager createFileAtPath:fileName contents:data attributes:nil]; } void pushContentWithListContentObject(UINavigationController *naviController,listContObjectVO *listItem) { if ([listItem.forwordType intValue] == commonNewsForwardType) {//普通新闻详情 detailContentHomeController *contentHomeVC = detailContentHomeController.new; [naviController pushViewController:contentHomeVC animated:YES]; contentHomeVC.nodeID = listItem.forwordNodeId; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == imageNewsForwardType) {//图集 ImageContentController *imageVC = ImageContentController.new; imageVC.type = imageScanType; imageVC.nodeID = listItem.forwordNodeId; [naviController pushViewController:imageVC animated:YES]; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == liveNewsForwardType) {//直播 TPLiveHomeController *liveVC = TPLiveHomeController.new; liveVC.nodeId = listItem.forwordNodeId; [naviController pushViewController:liveVC animated:YES]; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == specialTopicNewsForwardType) { //专题 topicHomeController *topicHomeVC = topicHomeController.new; topicHomeVC.nodeID = listItem.forwordNodeId; [naviController pushViewController:topicHomeVC animated:YES]; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == linkNewsForwardType) { //外链 if (listItem.forwordNodeId && !isBlankString(listItem.forwordNodeId)) { TPcontentWebController *webVC = [[TPcontentWebController alloc]init]; webVC.contentId = listItem.forwordNodeId; [naviController pushViewController:webVC animated:YES]; webVC.url = listItem.link; addHaveReadFlag(listItem.forwordNodeId); }else { TPWebViewContoller *webVC = [[TPWebViewContoller alloc]init]; [naviController pushViewController:webVC animated:YES]; webVC.url = listItem.link; } }else if ([listItem.forwordType intValue] == commonUserTopicForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = listItem.forwordNodeId; topicContentController *vc = topicContentController.new; vc.preTopicInfo = topic; vc.isCreatPage = NO; [naviController pushViewController:vc animated:YES]; }else if ([listItem.forwordType intValue] == creatUserTopicForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = listItem.forwordNodeId; topicContentController *vc = topicContentController.new; vc.preTopicInfo = topic; vc.isCreatPage = YES; [naviController pushViewController:vc animated:YES]; }else if ([listItem.forwordType intValue] == topicEditForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = listItem.forwordNodeId; creatTopicController *vc = creatTopicController.new; vc.topicBO = topic; [naviController pushViewController:vc animated:YES]; }else if ([listItem.forwordType intValue] == hotAskPageForwardType) { // hotAskCommentController *hotAskController = [hotAskCommentController new]; // [naviController pushViewController:hotAskController animated:YES]; } } void pushTopicPageWithTopicInfo(UINavigationController *naviController,TopicInfoBO *topicInfo) { if ([topicInfo.forwordType intValue] == commonUserTopicForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = topicInfo.topicId; topicContentController *vc = topicContentController.new; vc.preTopicInfo = topic; vc.isCreatPage = NO; [naviController pushViewController:vc animated:YES]; }else if ([topicInfo.forwordType intValue] == creatUserTopicForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = topicInfo.topicId; topicContentController *vc = topicContentController.new; vc.preTopicInfo = topic; vc.isCreatPage = YES; [naviController pushViewController:vc animated:YES]; }else if ([topicInfo.forwordType intValue] == topicEditForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = topicInfo.topicId; creatTopicController *vc = creatTopicController.new; vc.topicBO = topic; [naviController pushViewController:vc animated:YES]; } } void pushContentWithCommentBO(UINavigationController *naviController,commentObjectVO *commentBO) { if ([commentBO.forwordType intValue] == commonNewsForwardType) {//普通新闻详情 detailContentHomeController *contentHomeVC = detailContentHomeController.new; [naviController pushViewController:contentHomeVC animated:YES]; contentHomeVC.nodeID = commentBO.contId; addHaveReadFlag(commentBO.contId); }else if ([commentBO.forwordType intValue] == imageNewsForwardType) {//图集 ImageContentController *imageVC = ImageContentController.new; imageVC.type = imageScanType; imageVC.nodeID = commentBO.contId; [naviController pushViewController:imageVC animated:YES]; addHaveReadFlag(commentBO.contId); }else if ([commentBO.forwordType intValue] == liveNewsForwardType) {//直播 TPLiveHomeController *liveVC = TPLiveHomeController.new; liveVC.nodeId = commentBO.contId; [naviController pushViewController:liveVC animated:YES]; addHaveReadFlag(commentBO.contId); }else if ([commentBO.forwordType intValue] == specialTopicNewsForwardType) { //专题 topicHomeController *topicHomeVC = topicHomeController.new; topicHomeVC.nodeID = commentBO.contId; [naviController pushViewController:topicHomeVC animated:YES]; addHaveReadFlag(commentBO.contId); }else if ([commentBO.forwordType intValue] == linkNewsForwardType) { //外链 if (commentBO.contId && !isBlankString(commentBO.contId)) { TPcontentWebController *webVC = [[TPcontentWebController alloc]init]; webVC.contentId = commentBO.contId; [naviController pushViewController:webVC animated:YES]; webVC.url = commentBO.shareUrl; addHaveReadFlag(commentBO.contId); }else { TPWebViewContoller *webVC = [[TPWebViewContoller alloc]init]; [naviController pushViewController:webVC animated:YES]; webVC.url = commentBO.shareUrl; } }else if ([commentBO.forwordType intValue] == commonUserTopicForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = commentBO.contId; topicContentController *vc = topicContentController.new; vc.preTopicInfo = topic; vc.isCreatPage = NO; [naviController pushViewController:vc animated:YES]; }else if ([commentBO.forwordType intValue] == creatUserTopicForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = commentBO.contId; topicContentController *vc = topicContentController.new; vc.preTopicInfo = topic; vc.isCreatPage = YES; [naviController pushViewController:vc animated:YES]; }else if ([commentBO.forwordType intValue] == topicEditForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = commentBO.contId; creatTopicController *vc = creatTopicController.new; vc.topicBO = topic; [naviController pushViewController:vc animated:YES]; } } void pushContentWithObjInfo(UINavigationController *naviController,objInfoBO *commentBO) { if ([commentBO.forwordType intValue] == commonNewsForwardType) {//普通新闻详情 detailContentHomeController *contentHomeVC = detailContentHomeController.new; [naviController pushViewController:contentHomeVC animated:YES]; contentHomeVC.nodeID = commentBO.forwordNodeId; addHaveReadFlag(commentBO.forwordNodeId); }else if ([commentBO.forwordType intValue] == imageNewsForwardType) {//图集 ImageContentController *imageVC = ImageContentController.new; imageVC.type = imageScanType; imageVC.nodeID = commentBO.forwordNodeId; [naviController pushViewController:imageVC animated:YES]; addHaveReadFlag(commentBO.forwordNodeId); }else if ([commentBO.forwordType intValue] == liveNewsForwardType) {//直播 TPLiveHomeController *liveVC = TPLiveHomeController.new; liveVC.nodeId = commentBO.forwordNodeId; [naviController pushViewController:liveVC animated:YES]; addHaveReadFlag(commentBO.forwordNodeId); }else if ([commentBO.forwordType intValue] == specialTopicNewsForwardType) { //专题 topicHomeController *topicHomeVC = topicHomeController.new; topicHomeVC.nodeID = commentBO.forwordNodeId; [naviController pushViewController:topicHomeVC animated:YES]; addHaveReadFlag(commentBO.forwordNodeId); }else if ([commentBO.forwordType intValue] == linkNewsForwardType) { //外链 TPcontentWebController *webVC = [[TPcontentWebController alloc]init]; webVC.contentId = commentBO.forwordNodeId; [naviController pushViewController:webVC animated:YES]; addHaveReadFlag(commentBO.forwordNodeId); }else if ([commentBO.forwordType intValue] == commonUserTopicForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = commentBO.forwordNodeId; topicContentController *vc = topicContentController.new; vc.preTopicInfo = topic; vc.isCreatPage = NO; [naviController pushViewController:vc animated:YES]; }else if ([commentBO.forwordType intValue] == creatUserTopicForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = commentBO.forwordNodeId; topicContentController *vc = topicContentController.new; vc.preTopicInfo = topic; vc.isCreatPage = YES; [naviController pushViewController:vc animated:YES]; }else if ([commentBO.forwordType intValue] == topicEditForwardType) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = commentBO.forwordNodeId; creatTopicController *vc = creatTopicController.new; vc.topicBO = topic; [naviController pushViewController:vc animated:YES]; } } void pushLetterPageWithLetterBO(UINavigationController *naviController,letterBO *letterBo){ // 跳转类型:0-站内信 1-首页、2-新闻详情页、3-专题详情页、4-直播详情页、5-图集详情页、6-外链 ,7-话题 if ([letterBo.linkType intValue] ==1){ popToFrontPage(); }else if ([letterBo.linkType intValue] == 2) {//普通新闻详情 detailContentHomeController *contentHomeVC = detailContentHomeController.new; [naviController pushViewController:contentHomeVC animated:YES]; contentHomeVC.nodeID = letterBo.link; addHaveReadFlag(letterBo.link); }else if ([letterBo.linkType intValue] == 3) { //专题 topicHomeController *topicHomeVC = topicHomeController.new; topicHomeVC.nodeID = letterBo.link; [naviController pushViewController:topicHomeVC animated:YES]; addHaveReadFlag(letterBo.link); }else if ([letterBo.linkType intValue] == 4) {//直播 TPLiveHomeController *liveVC = TPLiveHomeController.new; liveVC.nodeId = letterBo.link; [naviController pushViewController:liveVC animated:YES]; addHaveReadFlag(letterBo.link); }else if ([letterBo.linkType intValue] == 5) {//图集 ImageContentController *imageVC = ImageContentController.new; imageVC.type = imageScanType; imageVC.nodeID = letterBo.link; [naviController pushViewController:imageVC animated:YES]; addHaveReadFlag(letterBo.link); }else if ([letterBo.linkType intValue] == 6) { //外链 //私信,纯外链新闻,显示此文章已下线(bug:4907) if (letterBo.cid && !isBlankString(letterBo.cid)) { TPcontentWebController *webVC = [[TPcontentWebController alloc]init]; webVC.contentId = letterBo.cid; [naviController pushViewController:webVC animated:YES]; webVC.url = letterBo.link; addHaveReadFlag(letterBo.cid); }else { TPWebViewContoller *webVC = [[TPWebViewContoller alloc]init]; [naviController pushViewController:webVC animated:YES]; webVC.url = letterBo.link; } }else if ([letterBo.linkType intValue] == 7) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = letterBo.link; topicContentController *vc = topicContentController.new; vc.preTopicInfo = topic; vc.isCreatPage = NO; [naviController pushViewController:vc animated:YES]; } } void presentAfterPushOrClickAd() { remotePushBO *pushBO = [TPUserDefault instance].pushBO; NSString *type = getForwardTypeWithTypeInPush(pushBO); if ([pushBO.linkType intValue] == 0 || isBlankString(type)) { popToFrontPage(); }else { type = getForwardTypeWithTypeInPush(pushBO); NSString *contontID = pushBO.contId; TPHttpController *vc = nil; if ([type intValue] == 4 &&([contontID hasPrefix:@"http"] || [contontID hasPrefix:@"https"])) { TPWebViewContoller *webVC = [[TPWebViewContoller alloc]init]; webVC.isPresent = YES; vc = webVC; MLNavigationController *naviVC = [[MLNavigationController alloc]initWithRootViewController:vc]; [KEY_WINDOW.rootViewController presentViewController:naviVC animated:YES completion:^{ webVC.url = contontID; [TPUserDefault instance].pushBO = nil; }]; }else { if (![contontID isMatchedByRegex:@"^[0-9]+$"]) { popToFrontPage(); return; } listContObjectVO *listItem = listContObjectVO.new; listItem.forwordType = type; listItem.forwordNodeId = contontID; if ([listItem.forwordType intValue] == 0) {//普通新闻详情 if (![contontID isMatchedByRegex:@"^[0-9]{7}$"]) { popToFrontPage(); return; } detailContentHomeController *contentHomeVC = detailContentHomeController.new; contentHomeVC.isPresent = YES; contentHomeVC.nodeID = listItem.forwordNodeId; addHaveReadFlag(listItem.forwordNodeId); vc = contentHomeVC; }else if ([listItem.forwordType intValue] == 1) {//图集 if (![contontID isMatchedByRegex:@"^[0-9]{7}$"]) { popToFrontPage(); return; } ImageContentController *imageVC = ImageContentController.new; imageVC.type = imageScanType; imageVC.nodeID = listItem.forwordNodeId; imageVC.isPresent = YES; vc = imageVC; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == 2) {//直播 if (![contontID isMatchedByRegex:@"^[0-9]{5}$"]) { popToFrontPage(); return; } TPLiveHomeController *liveVC = TPLiveHomeController.new; liveVC.nodeId = listItem.forwordNodeId; liveVC.isPresent = YES; vc = liveVC; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == 3) { //专题 if (![contontID isMatchedByRegex:@"^[0-9]{5}$"]) { popToFrontPage(); return; } topicHomeController *topicHomeVC = topicHomeController.new; topicHomeVC.nodeID = listItem.forwordNodeId; topicHomeVC.isPresent = YES; vc = topicHomeVC; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == 4) { //外链 if (![contontID isMatchedByRegex:@"^[0-9]{7}$"]) { popToFrontPage(); return; } TPcontentWebController *webVC = [[TPcontentWebController alloc]init]; webVC.contentId = listItem.forwordNodeId; webVC.isPresent = YES; vc = webVC; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == 5) { TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = listItem.forwordNodeId; topicContentController *topicVC = topicContentController.new; topicVC.preTopicInfo = topic; topicVC.isCreatPage = NO; vc = topicVC; }else if ([listItem.forwordType intValue] == 6) { TPHttpController *presentVC = nil; if ([KEY_WINDOW.rootViewController isKindOfClass:[MLNavigationController class]]) { UIViewController *topVC = ((MLNavigationController*)KEY_WINDOW.rootViewController).topViewController; if ([topVC isKindOfClass:[YRSideViewController class]]) { YRSideViewController *yrVC = (YRSideViewController*)topVC; presentVC = (TPHttpController*)yrVC.rootViewController; }else { presentVC = (TPHttpController*)topVC; } }else { presentVC = (TPHttpController*)KEY_WINDOW.rootViewController; } if (![TPUserDefault instance].userBO) { loginPopUpController *loginVC = loginPopUpController.new; [presentVC presentController:loginVC animated:YES presentSize:loginPopUpSize completion:^{ } dismiss:^{ if ([TPUserDefault instance].userBO) { gambitCenterHomeController* gambitVC = gambitCenterHomeController.new; // gambitVC.delegate = self; [presentVC presentController:gambitVC animated:YES presentSize:gambitCenterPopSize completion:^{ [gambitVC setCurrentPageIndex:1]; } dismiss:^{ }]; } }]; }else { gambitCenterHomeController* gambitVC = gambitCenterHomeController.new; [presentVC presentController:gambitVC animated:YES presentSize:gambitCenterPopSize completion:^{ [gambitVC setCurrentPageIndex:1]; } dismiss:^{ }]; } return; } MLNavigationController *naviVC = [[MLNavigationController alloc]initWithRootViewController:vc]; [KEY_WINDOW.rootViewController presentViewController:naviVC animated:YES completion:^{ [TPUserDefault instance].pushBO = nil; }]; } } } void pushToContentAfterPushOrClickAd(UINavigationController *naviController) { remotePushBO *pushBO = [TPUserDefault instance].pushBO; NSString *type = getForwardTypeWithTypeInPush(pushBO); if ([pushBO.linkType intValue] == 0 || isBlankString(type)) { popToFrontPage(); }else { type = getForwardTypeWithTypeInPush(pushBO); NSString *contontID = pushBO.contId; TPHttpController *vc = nil; if ([type intValue] == 4 &&([contontID hasPrefix:@"http"] || [contontID hasPrefix:@"https"])) { TPWebViewContoller *webVC = [[TPWebViewContoller alloc]init]; webVC.isPresent = YES; vc = webVC; [naviController pushViewController:vc animated:YES]; webVC.url = contontID; [TPUserDefault instance].pushBO = nil; }else { if (![contontID isMatchedByRegex:@"^[0-9]+$"]) { popToFrontPage(); return; } listContObjectVO *listItem = listContObjectVO.new; listItem.forwordType = type; listItem.forwordNodeId = contontID; if ([listItem.forwordType intValue] == commonNewsForwardType) {//普通新闻详情 if (![contontID isMatchedByRegex:@"^[0-9]{7}$"]) { popToFrontPage(); return; } detailContentHomeController *contentHomeVC = detailContentHomeController.new; contentHomeVC.isPresent = YES; contentHomeVC.nodeID = listItem.forwordNodeId; addHaveReadFlag(listItem.forwordNodeId); vc = contentHomeVC; }else if ([listItem.forwordType intValue] == imageNewsForwardType) {//图集 if (![contontID isMatchedByRegex:@"^[0-9]{7}$"]) { popToFrontPage(); return; } ImageContentController *imageVC = ImageContentController.new; imageVC.type = imageScanType; imageVC.nodeID = listItem.forwordNodeId; imageVC.isPresent = YES; vc = imageVC; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == liveNewsForwardType) {//直播 if (![contontID isMatchedByRegex:@"^[0-9]{5}$"]) { popToFrontPage(); return; } TPLiveHomeController *liveVC = TPLiveHomeController.new; liveVC.nodeId = listItem.forwordNodeId; liveVC.isPresent = YES; vc = liveVC; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == specialTopicNewsForwardType) { //专题 if (![contontID isMatchedByRegex:@"^[0-9]{5}$"]) { popToFrontPage(); return; } topicHomeController *topicHomeVC = topicHomeController.new; topicHomeVC.nodeID = listItem.forwordNodeId; topicHomeVC.isPresent = YES; vc = topicHomeVC; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == liveNewsForwardType) { //外链 if (![contontID isMatchedByRegex:@"^[0-9]{7}$"]) { popToFrontPage(); return; } TPcontentWebController *webVC = [[TPcontentWebController alloc]init]; webVC.contentId = listItem.forwordNodeId; webVC.isPresent = YES; vc = webVC; addHaveReadFlag(listItem.forwordNodeId); }else if ([listItem.forwordType intValue] == commonUserTopicForwardType) { // TopicInfoBO *topic = [[TopicInfoBO alloc]init]; // topic.topicId = listItem.forwordNodeId; // topicContentController *vc = topicContentController.new; // vc.preTopicInfo = topic; // vc.isCreatPage = NO; // [naviController pushViewController:vc animated:YES]; }else if ([listItem.forwordType intValue] == creatUserTopicForwardType) { // if (![TPUserDefault instance].userBO) { // vc = loginHomeController.new; // }else { // myTopicCenterController *topicCenter = [myTopicCenterController new]; // vc = topicCenter; // } }else if ([listItem.forwordType intValue] == topicEditForwardType) { // TopicInfoBO *topic = [[TopicInfoBO alloc]init]; // topic.topicId = listItem.forwordNodeId; // creatTopicController *vc = creatTopicController.new; // vc.topicBO = topic; // [naviController pushViewController:vc animated:YES]; } [naviController pushViewController:vc animated:YES]; [TPUserDefault instance].pushBO = nil; // if ([vc isKindOfClass:[myTopicCenterController class]]) { // myTopicCenterController *topicCenter = (myTopicCenterController*)vc; // // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // [topicCenter setCurrentIndex:1]; // }); // } } } } void popToFrontPage() { AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; YRSideViewController *sideController=[delegate sideViewController]; [[KGModal sharedInstance] hideAnimated:NO]; [sideController.navigationController popToRootViewControllerAnimated:YES]; [TPUserDefault instance].pushBO = nil; } NSString *getForwardTypeWithTypeInPush(remotePushBO* push) { if ([push.linkType intValue] == 2) { return @"0"; }else if ([push.linkType intValue] == 3) { return @"3"; }else if ([push.linkType intValue] == 4) { return @"2"; }else if ([push.linkType intValue] == 5) { return @"1"; }else if ([push.linkType intValue] == 6) { return @"4"; }else if ([push.linkType intValue] == 7) { return @"5"; }else if ([push.linkType intValue] == 8) { return @"6"; }else return @""; } NSString *returnNumOfVersion(NSString *versionStr) { versionStr = [versionStr stringByReplacingOccurrencesOfString:@"." withString:@""]; return versionStr; } BOOL isNeedUpdate(NSString *latestVerstion,NSString *currentVersion,NSString* ignoreVersion) { NSInteger latest = [returnNumOfVersion(latestVerstion) intValue]; NSInteger current = [returnNumOfVersion(currentVersion) intValue]; NSInteger ignore = [returnNumOfVersion(ignoreVersion) intValue]; if (latest > current && latest > ignore) { return YES; }else return NO; } void clearImageMemCache () { SDImageCache *sd_cache = [SDImageCache sharedImageCache]; [sd_cache clearMemory]; } NSString* deviceString() { // 需要#import "sys/utsname.h" struct utsname systemInfo; uname(&systemInfo); NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding]; if ([deviceString isEqualToString:@"iPhone1,1"]) return @"iPhone 1G"; if ([deviceString isEqualToString:@"iPhone1,2"]) return @"iPhone 3G"; if ([deviceString isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS"; if ([deviceString isEqualToString:@"iPhone3,1"]) return @"iPhone 4"; if ([deviceString isEqualToString:@"iPhone4,1"]) return @"iPhone 4S"; if ([deviceString isEqualToString:@"iPhone5,2"]) return @"iPhone 5"; if ([deviceString isEqualToString:@"iPhone3,2"]) return @"Verizon iPhone 4"; if ([deviceString isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G"; if ([deviceString isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G"; if ([deviceString isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G"; if ([deviceString isEqualToString:@"iPod4,1"]) return @"iPod Touch 4G"; if ([deviceString isEqualToString:@"iPad1,1"]) return @"iPad"; if ([deviceString isEqualToString:@"iPad2,1"]) return @"iPad 2 (WiFi)"; if ([deviceString isEqualToString:@"iPad2,2"]) return @"iPad 2 (GSM)"; if ([deviceString isEqualToString:@"iPad2,3"]) return @"iPad 2 (CDMA)"; if ([deviceString isEqualToString:@"i386"]) return @"Simulator"; if ([deviceString isEqualToString:@"x86_64"]) return @"Simulator"; NSLog(@"NOTE: Unknown device type: %@", deviceString); return deviceString; } #pragma mark - is emoji BOOL isContainsEmoji(NSString *string) { __block BOOL isEomji = NO; [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { const unichar hs = [substring characterAtIndex:0]; // surrogate pair if (0xd800 <= hs && hs <= 0xdbff) { if (substring.length > 1) { const unichar ls = [substring characterAtIndex:1]; const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000; if (0x1d000 <= uc && uc <= 0x1f77f) { isEomji = YES; } } } else { // non surrogate if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) { isEomji = YES; } else if (0x2B05 <= hs && hs <= 0x2b07) { isEomji = YES; } else if (0x2934 <= hs && hs <= 0x2935) { isEomji = YES; } else if (0x3297 <= hs && hs <= 0x3299) { isEomji = YES; } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50|| hs == 0x231a ) { isEomji = YES; } if (!isEomji && substring.length > 1) { const unichar ls = [substring characterAtIndex:1]; if (ls == 0x20e3) { isEomji = YES; } } } }]; return isEomji; } NSString * disable_emoji(NSString *text) { NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil]; NSString *modifiedString = [regex stringByReplacingMatchesInString:text options:0 range:NSMakeRange(0, [text length]) withTemplate:@""]; return modifiedString; } NSString *setStrSpaceForParagraphSpacingWithString(NSString *str) { if (![str isMatchedByRegex:@"\n"]) { return str; } str = [str stringByReplacingOccurrencesOfRegex:@"[\\n]+" withString:@"\n"]; str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\n"]; NSRange range = NSMakeRange(str.length-2, 2); NSString *tempStr = [str substringWithRange:range]; if ([tempStr isEqualToString:@"\n\n"]) { str = [str substringToIndex:str.length-2]; } return str; } commentObjectVO * setStrSpaceForParagraphSpacingWithCommentBO(commentObjectVO *commentBO) { return commentBO;//暂时不处理互动区段间距 NSString *askContent = commentBO.content; NSMutableArray *answerList = [NSMutableArray arrayWithArray:commentBO.answerList]; NSMutableDictionary *quoInfo = [NSMutableDictionary dictionaryWithDictionary:commentBO.quoteInfo]; if (quoInfo[@"content"] && !isBlankString(quoInfo[@"content"])) { NSString *quoInfoStr = setStrSpaceForParagraphSpacingWithString(quoInfo[@"content"]); [quoInfo setObject:quoInfoStr forKey:@"content"]; commentBO.quoteInfo = quoInfo; } askContent = setStrSpaceForParagraphSpacingWithString(askContent); commentBO.content = askContent; NSMutableArray *tempArray = [NSMutableArray arrayWithArray:answerList]; [answerList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:obj]; if (obj[@"content"] && !isBlankString(obj[@"content"])) { NSString *answerContent = setStrSpaceForParagraphSpacingWithString(obj[@"content"]); [tempDic setObject:answerContent forKey:@"content"]; NSMutableDictionary *quotoInfoDic = [NSMutableDictionary dictionaryWithDictionary:obj[@"quoteInfo"]]; if (quotoInfoDic[@"content"] && !isBlankString(quotoInfoDic[@"content"])) { quotoInfoDic[@"content"] = setStrSpaceForParagraphSpacingWithString(quotoInfoDic[@"content"]); [tempDic setObject:quotoInfoDic forKey:@"quoteInfo"]; } [tempArray replaceObjectAtIndex:idx withObject:tempDic]; } }]; // [answerList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { // NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:obj]; // if (obj[@"content"] && !isBlankString(obj[@"content"])) { // NSString *answerContent = setStrSpaceForParagraphSpacingWithString(obj[@"content"]); // [tempDic setObject:answerContent forKey:@"content"]; // NSMutableDictionary *quotoInfoDic = [NSMutableDictionary dictionaryWithDictionary:obj[@"quoteInfo"]]; // if (quotoInfoDic[@"content"] && !isBlankString(quotoInfoDic[@"content"])) { // quotoInfoDic[@"content"] = setStrSpaceForParagraphSpacingWithString(quotoInfoDic[@"content"]); // [tempDic setObject:quotoInfoDic forKey:@"quoteInfo"]; // } // [tempArray replaceObjectAtIndex:idx withObject:tempDic]; // } // }]; commentBO.answerList = tempArray; return commentBO; } NSAttributedString* getLineSpaceAttributedString(NSString* string, CGFloat lineSpace, UIFont* font) { NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = lineSpace;// 字体的行间距 NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: font, NSFontAttributeName, paragraphStyle,NSParagraphStyleAttributeName, nil]; NSAttributedString *attributeStr = [[NSAttributedString alloc]initWithString:string attributes:attributes]; return attributeStr; } //解析文章详情内容 NSMutableArray * analysisContent(NSArray* list) { NSMutableArray *analysisedList = [NSMutableArray array]; __block BOOL isHaveBFlag = NO; __block BOOL isHaveUFlag = NO; __block BOOL isHaveCFlag = NO; __block NSString *colorStr = @""; [list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { sectionContentBO *sectionBO = setJsonDicToDataModel(obj, [sectionContentBO class]); if (sectionBO) { if (sectionBO.content && ![sectionBO.content isEqualToString:@""]) { NSString *contentStr = sectionBO.content; // if ([contentStr isMatchedByRegex:@"\n</U>"]) {//bug:3901 // contentStr = [contentStr stringByReplacingOccurrencesOfString:@"\n</U>" withString:@"</U>\n"]; // } contentStr = [contentStr stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"]; contentStr = [contentStr stringByReplacingOccurrencesOfString:@"\n\n" withString:@"\n"]; NSArray *deperateArray = [contentStr componentsSeparatedByRegex:@"\n"]; [deperateArray enumerateObjectsUsingBlock:^(id item, NSUInteger index, BOOL *stoped) { if (isHaveBFlag) { item = [NSString stringWithFormat:@"<B>%@",item]; } if (isHaveUFlag) { item = [NSString stringWithFormat:@"<U>%@",item]; } if (isHaveCFlag) { item = [NSString stringWithFormat:@"%@%@",colorStr,item]; } NSRange range; NSRange lastRange; NSString *curStr; NSString *lastStr; for(int i=0; i<[item length]; i+=range.length){ range = [item rangeOfComposedCharacterSequenceAtIndex:i]; curStr = [item substringWithRange:range]; if (i != 0) { lastRange = [item rangeOfComposedCharacterSequenceAtIndex:i-1]; lastStr = [item substringWithRange:lastRange]; if ([curStr isEqualToString:@"B"] && [lastStr isEqualToString:@"<"]) { isHaveBFlag = YES; } if ([curStr isEqualToString:@"B"] && [lastStr isEqualToString:@"/"]) { isHaveBFlag = NO; } if ([curStr isEqualToString:@"U"] && [lastStr isEqualToString:@"<"]) { isHaveUFlag = YES; } if ([curStr isEqualToString:@"U"] && [lastStr isEqualToString:@"/"]) { isHaveUFlag = NO; } if ([curStr isEqualToString:@"C"] && [lastStr isEqualToString:@"<"]) { isHaveCFlag = YES; NSRange colorRange = NSMakeRange(i-1, 10); colorStr = [item substringWithRange:colorRange]; } if ([curStr isEqualToString:@"N"] && [lastStr isEqualToString:@"<"]) { isHaveCFlag = NO; } } } if (isHaveBFlag) { item = [item stringByAppendingString:@"</B>"]; } if (isHaveUFlag) { item = [item stringByAppendingString:@"</U>"]; } if (isHaveCFlag) { item = [item stringByAppendingString:@"<N>"]; } /** * bug:(正文渲染问题(5362,5171,5248,5216)) */ /** * bug:5646(【倒退】新闻详情页:有几篇新闻显示异常) */ [analysisedList addObject:item]; }]; } if (sectionBO.imageInfoList.count > 0) { [sectionBO.imageInfoList enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger index, BOOL *stop) { imageObjectBO *imageBO = setJsonDicToDataModel(dic, [imageObjectBO class]); if (imageBO) { [analysisedList addObject:imageBO]; } }]; } } }]; return analysisedList; } CGFloat getLineHeightWithCountAndFontWidth(int count,CGFloat lineSpace,CGFloat fontSize) { return (fontSize+lineSpace)*count; }