YYLabel content display (text content link (highlighted and clickable); content supports the height of the background)

   NSString *strTime = DSStringValue(self.model.CreateTime);
//    strTime = [strTime stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    
    if (strTime.length > 0) {
//        NSRange rangeDate = [strTime rangeOfString:@"T"];

        self.lblDate.text =[[self.model.CreateTime stringByReplacingOccurrencesOfString:@"T" withString:@" "]componentsSeparatedByString:@"."][0];
    }
    NSString *strContents = GetStringValue(self.model.NoticeContent);
    if (strContents.length > 0) {
    self.lblContents.text =strContents;
    CGSize sizeContents = [strContents sizeWithFontCustom:REGULAR_FONT(14) constrainedToSize:CGSizeMake(SCREEN_WIDTH - 15-97-17-19, MAXFLOAT)];
    UIImage *imageBg = [UIImage imageNamed:@"mine_message_bg"];
        imageBg = [imageBg resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0,0, 0) resizingMode:UIImageResizingModeStretch];
    self.imvBg.image =imageBg;
      CGRect lblContentFrame = self.lblContents.frame;
    lblContentFrame.size.height =sizeContents.height;
    self.lblContents.frame = lblContentFrame;
    CGRect imvBgFrame = self.imvBg.frame;
        imvBgFrame.size.height =sizeContents.height + 24;
    self.imvBg.frame = imvBgFrame;
    _scvMain.contentSize = CGSizeMake(SCREEN_WIDTH,CGRectGetMaxY(self.imvBg.frame)+ 10);
    //点击z跳转
    [self needHightText:strContents];
    //长按复制
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(copyAction:)];
    longPressGesture.minimumPressDuration = 1;//手势识别时间
    [_imvBg addGestureRecognizer:longPressGesture];
    }
- (void)copyAction:(UILongPressGestureRecognizer *)longPressGesture{
     if (longPressGesture.state == UIGestureRecognizerStateBegan) {
    UIPasteboard*pasteboard = [UIPasteboard generalPasteboard];

    pasteboard.string= self.lblContents.text;
     }else if(longPressGesture.state == UIGestureRecognizerStateEnded){
         [self showMessage:@"复制成功"];
     }
    
    
}

- (void)needHightText:(NSString *)wholeText {



    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:wholeText];
    
    text.yy_font = REGULAR_FONT(12);
    text.yy_color = Color_Title_AAA;
    NSError *error;
    NSDataDetector *dataDetector=[NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
    NSArray *arrayOfAllMatches=[dataDetector matchesInString:wholeText options:NSMatchingReportProgress range:NSMakeRange(0, wholeText.length)];
    //NSMatchingOptions匹配方式也有好多种,我选择NSMatchingReportProgress,一直匹配
    
    //我们得到一个数组,这个数组中NSTextCheckingResult元素中包含我们要找的URL的range,当然可能找到多个URL,找到相应的URL的位置,用YYlabel的高亮点击事件处理跳转网页
    for (NSTextCheckingResult *match in arrayOfAllMatches)
    {
        //        NSLog(@"%@",NSStringFromRange(match.range));
        
        DefinitionWeak(self) ;
        [text yy_setTextHighlightRange:match.range//设置点击的位置
                                 color:Color_Theme
                       backgroundColor:[UIColor whiteColor]
                             tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
                                 NSLog(@"这里是点击事件");
            
            
            
            CommonWebVC *vc = [[CommonWebVC alloc]init];
                    vc.commonWebHidenToken = CommonWebHidenTokenTwo;
                    vc.strAbsoluteUrl = [wholeText substringWithRange:match.range];//CM序列号填写引导
                    [weak_self dsPushViewController:vc animated:YES];
            
                                  //跳转用的WKWebView
//                                 WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
//                                 [self.view addSubview:webView];
//                                 [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[wholeText substringWithRange:match.range]]]];

                             }];
    }
    _lblContents.attributedText = text;
}


Guess you like

Origin blog.csdn.net/weixin_42050662/article/details/122734594