// // ViewController.m // phoneTest // // Created by Ray on 01/09/2017. // // #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] #import "ResultViewController.h" #import "FullyShowViewController.h" #import "RANetwork.h" #import "RAUtils.h" #import "JLRefreshHeader.h" #import "JLRefreshFooter.h" //#import "OrderDetailViewController.h" //#import "KVViewController.h" #import #import "config.h" #import "RAConvertor.h" #import "RAUtils.h" //#import "RTLabel.h" #import "ResultCell.h" #import const int delta = 25; @interface ResultViewController () @property (weak, nonatomic) IBOutlet NSLayoutConstraint *w_constraint; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *h_constraint; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *trail_constraint; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *lead_constraint; @end @implementation ResultViewController + (instancetype)buildInstanceWithParams:(NSMutableDictionary *)params { id resultVC = [[self alloc] initWithNibName:@"Result" bundle:nil]; ((ResultViewController *)resultVC).params = params; return resultVC; } - (void)labelClick { NSLog(@"underlineLabel被点击了"); } - (void)resetContraint { for (NSLayoutConstraint *constraint in self.view.constraints) { if ([constraint.identifier isEqualToString:@"title_top"] ) { [self.view removeConstraint:constraint]; } if ([constraint.identifier isEqualToString:@"scroll_bottom"]) { [self.view removeConstraint:constraint]; } } NSLayoutConstraint *scroll_top = [NSLayoutConstraint constraintWithItem:self.titleBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]; NSLayoutConstraint *scroll_bottom = [NSLayoutConstraint constraintWithItem:self.scrollview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]; [self.view addConstraint:scroll_top]; [self.view addConstraint:scroll_bottom]; CGFloat height = self.bottomLayoutGuide.length; self.bottom_mask_constraint.constant = height; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self resetContraint]; } - (void)viewDidLoad { [super viewDidLoad]; self.default_row_text_size=14; self.default_title_text_size=17; self.default_line_color = 0x000000; self.titleLabel.text = nil; // self.labelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapAction:)]; // // self.labelTap.numberOfTouchesRequired = 1; //手指数 // self.labelTap.numberOfTapsRequired = 2; //tap次数 // // // // // self.cellDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cellDoubleTapAction:)]; // // self.cellDoubleTap.numberOfTouchesRequired = 1; //手指数 // self.cellDoubleTap.numberOfTapsRequired = 2; //tap次数 // UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self // action:nil]; // singleFingerOne.numberOfTouchesRequired = 1; //手指数 // singleFingerOne.numberOfTapsRequired = 1; //tap次数 // // // singleFingerTwo.delegate = self; self.label = [MDHTMLLabel new]; self.label.font = [UIFont systemFontOfSize:self.default_row_text_size]; self.label.textInsets = UIEdgeInsetsMake(0.f, 8.f, 0.f, 8.f); // NSString* Path = [[NSBundle mainBundle] pathForResource:@"layout.json" ofType:nil]; // // NSData *data = [NSData dataWithContentsOfFile:Path]; // self.content_layout = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy]; // // // Path = [[NSBundle mainBundle] pathForResource:@"data.json" ofType:nil]; // // data = [NSData dataWithContentsOfFile:Path]; // self.content_data = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy]; // int width=[self.content_layout[@"header"][@"width"] intValue]; // if(widthlabel.frame.size.width||self.label.frame.size.height>label.frame.size.height) { FullyShowViewController *fullVC = [[UIStoryboard storyboardWithName:@"Result" bundle:nil] instantiateViewControllerWithIdentifier:@"fullyShowViewController"]; fullVC.htmlText = label.htmlText; fullVC.preferredContentSize = CGSizeMake(200, 240); fullVC.modalPresentationStyle=UIModalPresentationPopover; UIPopoverPresentationController* popPc = fullVC.popoverPresentationController; popPc.permittedArrowDirections = UIPopoverArrowDirectionAny; CGRect rectl = [RAUtils relativeFrameForScreenWithView:label]; CGRect rectv =[RAUtils relativeFrameForScreenWithView:self.view]; CGRect intersection_rect=CGRectIntersection(rectl, rectv); UIWindow * window=[[[UIApplication sharedApplication] delegate] window]; CGRect source_rect=[RAUtils relativeFrame:intersection_rect FromView:window toView:label]; // CGRect rect=[v convertRect: v.bounds toView:window]; popPc.sourceRect = source_rect; popPc.sourceView = label; // popPc.barButtonItem = label; popPc.delegate = self; [self presentViewController:fullVC animated:true completion:nil]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { } -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { // auto resizing // self.scrollview.contentSize=self.tableview.frame.size; // auto layout // int width=[self.content_layout[@"header"][@"width"] intValue]; // if(widthitem.count-1) text=@"row data missing"; else text=item[i]; // RTTextLineBreakModeWordWrapping = kCTLineBreakByWordWrapping, // RTTextLineBreakModeCharWrapping = kCTLineBreakByCharWrapping, // RTTextLineBreakModeClip = kCTLineBreakByClipping, // NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:text]; // vallabel.attributedText = attrStr; vallabel.htmlText = text; vallabel.textAlignment= [self get_TextHAlign:arr_col[i][@"h_align"] ]; vallabel.layer.borderWidth=0.3; vallabel.layer.borderColor = UIColorFromRGB(self.default_line_color).CGColor; // UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTapAction:)]; UITapGestureRecognizer* labelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapAction:)]; labelTap.numberOfTouchesRequired = 1; //手指数 labelTap.numberOfTapsRequired = 1; //tap次数 [vallabel addGestureRecognizer:labelTap]; [labelTap requireGestureRecognizerToFail:cellDoubleTap]; // [vallabel addGestureRecognizer:self.cellDoubleTap]; vallabel.userInteractionEnabled = YES; // [vallabel becomeFirstResponder]; // vallabel.delegate = self; // [statuslabel sizeToFit]; [cell addSubview:vallabel]; x+=width; } if(indexPath.row%2==0) cell.backgroundColor=UIColorFromRGB(color0);//[UIColor whiteColor]; else cell.backgroundColor=UIColorFromRGB(color1);//[UIColor lightGrayColor]; return cell; } } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = [UIColor whiteColor]; } //#pragma mark - TouchLabel Delegate //- (void)touchLabel:(TouchLabel *)touchLabel touchesWtihTag:(NSInteger)tag //{ // //} //-(void) empty //{ // //} #pragma mark - UIPopoverPresentationControllerDelegate Delegate //实现该代理方法,返回UIModalPresentationNone值,可以在iPhone设备实现popover效果 -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{ return UIModalPresentationNone;//不适配(不区分ipad或iPhone) } #pragma mark - TableView Delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"cell selected"); } - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } #pragma mark - Public - (void)setQueryParams:(NSMutableDictionary *)params { self.params = params; } #pragma mark - Private - (void)setupTableRefreshView { __weak typeof(self) weakSelf = self; self.tableview.jl_header = [[JLRefreshHeader alloc] init]; self.tableview.jl_header.refreshDelegate = self; self.tableview.jl_header.clear = YES; self.tableview.jl_header.offsetBlock = ^(CGFloat offset) { if (offset <= 0) { weakSelf.header_top_constraint.constant = -offset; } }; self.tableview.jl_header.stateChangeBlock = ^(JLRefreshState state) { NSString *title = [weakSelf.tableview.jl_header refreshTitleForState:state]; weakSelf.refreshHeaderTitleLb.text = title; }; self.tableview.jl_footer = [[JLRefreshFooter alloc] init]; self.tableview.jl_footer.refreshDelegate = self; self.tableview.jl_footer.clear = YES; self.tableview.jl_footer.offsetBlock = ^(CGFloat offset) { if (offset <= 0) { weakSelf.footer_top_constraint.constant = offset; } }; self.tableview.jl_footer.stateChangeBlock = ^(JLRefreshState state) { NSString *title = [weakSelf.tableview.jl_footer refreshTitleForState:state]; weakSelf.refreshFooterTitleLb.text = title; }; } - (void)configureTableView { [self.tableview registerNib:[UINib nibWithNibName:@"ResultCell" bundle:nil] forCellReuseIdentifier:@"tCell"]; } - (void)setupNavigationBar { if(self.content_menu==nil) { self.navigationItem.rightBarButtonItem=nil; return; } UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_menu"] style:UIBarButtonItemStylePlain target:self action:@selector(MenuClick:)]; self.navigationItem.rightBarButtonItem = menuItem; UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init]; backItem.title = @"Back"; self.navigationItem.backBarButtonItem = backItem; } - (NSInteger)resultItemCount { return [[self.content_data objectForKey:@"count"] integerValue]; } - (void)loadContent { [self loadContentWithOption:0 Complete:nil]; } - (void)updateTableFrame { // auto resizing // int width=[self.content_layout[@"header"][@"width"] intValue]; // if(width) previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index { if(self.documentPath==nil) return nil; return [NSURL fileURLWithPath:self.documentPath]; } #pragma mark - QuickLook Delegate - (void) previewControllerDidDismiss:(QLPreviewController *)controller { } /** * 文件内部链接点击不进行外部跳转 */ - (BOOL) previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id)item { return NO; } #pragma mark - customize editor - (UIView*)get_tableHeader { UILabel * headerview = [UILabel new]; headerview.frame = CGRectMake(0, 0, self.tableview.frame.size.width, 44); headerview.numberOfLines = 0; headerview.textAlignment = NSTextAlignmentCenter; headerview.text = @""; return headerview; } -(void) resize_tableHeader { //利用systemLayoutSizeFittingSize:计算出真实高度 CGFloat height = [self.tableview.tableHeaderView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; CGRect headerFrame = self.tableview.tableHeaderView.frame; headerFrame.size.height = height+20;//20是留的上下边距 //修改tableHeaderView的frame self.tableview.tableHeaderView.frame = headerFrame; } #pragma mark - 3D Touch Delegate (PreviewingDelegate) // peek (预览) - (UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location { // [previewingContext sourceView]即为所按压的视图 // UITableViewCell *view = (UITableViewCell *)[previewingContext sourceView]; // // // 设置不被虚化的范围 // CGRect rect = view.bounds; // [previewingContext setSourceRect:rect]; // // NSIndexPath *indexPath = [self.tableview indexPathForCell:view]; // // peek (预览界面) // UIViewController *tvc = [self viewControllerFor3DTouch:indexPath]; // // tvc.preferredContentSize = CGSizeMake(0, 480); // 预览图的大小 // // return tvc; // NSIndexPath *indexPath = [self.tableview indexPathForCell:view]; // [self presses3DTouchForIndexPath:indexPath]; return nil; } // pop (在预览基础上更用力按压) - (void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit { // [self showViewController:viewControllerToCommit sender:self]; } #pragma mark - Cell Touch Delegate - (void)touchedCell:(ResultCell *)cell withForce:(float)force { if (force > 4.0f) { cell.handlingTouch = YES; // 1519: 普通短震,3D Touch 中 Peek 震动反馈 // 1520: 普通短震,3D Touch 中 Pop 震动反馈 // 1521: 连续三次短震 // kSystemSoundID_Vibrate: 震动很暴力,不适合震动反馈 AudioServicesPlaySystemSound(1519); // 支持震动的设备上会震动,不支持就什么都不做 kSystemSoundID_Vibrate NSIndexPath *indexPath = [self.tableview indexPathForCell:cell]; [self presses3DTouchForIndexPath:indexPath]; } NSLog(@"force %f",force); } #pragma mark - Child View Controller Override /** * 0: init load * 1: refresh * 2: load more */ - (void)loadContentWithOption:(int)option Complete:(void (^)(int result,int count))finish { NSAssert(true, ([NSString stringWithFormat:@"%s loadContentWithOption not impl",object_getClassName([self class])])); // if (self.params) { // // NSMutableDictionary *dic = self.params.mutableCopy; // switch (option) { // case 0: // case 1: { // self.offset = 0; // self.content_layout =nil; // self.content_action = nil; // self.content_menu = nil; // self.content_data = nil; // } // break; // case 2: { // self.offset = [self resultItemCount]; // } // break; // } // //// dic[@"criteria"] = self.params; // // [dic setObject:@(self.offset) forKey:@"offset"]; // [dic setObject:@(delta) forKey:@"limit"]; //// dic[@"criteria"] = [NSArray arrayWithObjects:@"aaa",@"bbb", nil]; // // __weak typeof(self) weakSelf = self; // dispatch_async(dispatch_get_global_queue(0, 0), ^{ // NSDictionary *contentDic = [RANetwork query:dic]; // NSInteger result = [[contentDic objectForKey:@"result"] integerValue]; // dispatch_async(dispatch_get_main_queue(), ^{ // if (weakSelf) { // __strong typeof(weakSelf) strongSelf = weakSelf; // if (result == RESULT_TRUE) { // strongSelf.content_layout = [contentDic objectForKey:@"layout"]; // strongSelf.content_action = [contentDic objectForKey:@"row_action"]; // strongSelf.content_menu = [[contentDic objectForKey:@"menu"] mutableCopy]; // [strongSelf setupNavigationBar]; // // UILabel * headerview=(UILabel *)self.tableview.tableHeaderView; // headerview.text = contentDic[@"table_title"]; // [self resize_tableHeader]; // // if(strongSelf.content_data==nil) // strongSelf.content_data = [NSMutableDictionary new]; // NSMutableDictionary* newdata = [[contentDic objectForKey:@"data"] mutableCopy]; // for(int dc = 0;dc<[newdata[@"count"] intValue];dc++) // { // NSMutableDictionary * newdata_item = newdata[[NSString stringWithFormat:@"item_%d",dc]]; // // strongSelf.content_data[[NSString stringWithFormat:@"item_%d",[strongSelf.content_data[@"count"] intValue] ]] = newdata_item; // strongSelf.content_data[@"count"] = @([strongSelf.content_data[@"count"] intValue]+1); // } // //// strongSelf.content_data = [contentDic objectForKey:@"data"]; // [strongSelf updateTableFrame]; // [strongSelf.tableview reloadData]; // } else { // [RAUtils message_alert:[contentDic objectForKey:@"err_msg"] title:@"Warning" controller:strongSelf]; // } // // if (finish) { // finish((int)result,(int)[self resultItemCount]); // } // // } // // }); // }); // } } - (void)MenuClick:(UIBarButtonItem *)sender { // UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; // //block代码块取代了delegate // // __weak typeof(self) weakSelf = self; // for(int i=0;i