如何在 uilabel 中显示“显示更多”文本?

Posted

技术标签:

【中文标题】如何在 uilabel 中显示“显示更多”文本?【英文标题】:How to show "Show more" text in uilabel? 【发布时间】:2017-06-29 06:13:37 【问题描述】:

在一定的uilabel限制后,我必须使用显示更多文本。当我单击该 uilabel 显示更多文本时,标签应展开并且显示更多文本应更改为显示更少。

【问题讨论】:

你需要使用tableview。 @BrijeshShiroya 除了 Tableview 还有其他选择吗? 您需要在标签@ShibaneeMishra 中使用NSAttributed 文本 Add "...Read More" to the end of UILabel的可能重复 【参考方案1】:

使用下面的代码来回答。

 - (void)addReadMoreStringToUILabel:(UILabel*)label
    
        NSString *readMoreText = @" ...Read More";
        NSInteger lengthForString = label.text.length;
        if (lengthForString >= 100)
        
            NSInteger lengthForVisibleString = 100;
            NSMutableString *mutableString = [[NSMutableString alloc] initWithString:label.text];
            NSString *trimmedString = [mutableString stringByReplacingCharactersInRange:NSMakeRange(lengthForVisibleString, (label.text.length - lengthForVisibleString)) withString:@""];
            NSInteger readMoreLength = readMoreText.length;
            NSString *trimmedForReadMore = [trimmedString stringByReplacingCharactersInRange:NSMakeRange((trimmedString.length - readMoreLength), readMoreLength) withString:@""];
            NSMutableAttributedString *answerAttributed = [[NSMutableAttributedString alloc] initWithString:trimmedForReadMore attributes:@
                                                                                                                                            NSFontAttributeName : label.font


                                                                                                                                            ];


            UIColor *color = [UIColor colorWithRed:21.0/255.0 green:40.0/255.0 blue:86.0/255.0 alpha:1]; // select needed color
            NSDictionary *attrs = @ NSForegroundColorAttributeName : color, NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone);
            NSMutableAttributedString *readMoreAttributed = [[NSMutableAttributedString alloc] initWithString:readMoreText attributes:attrs];






            [answerAttributed appendAttributedString:readMoreAttributed];
            label.attributedText = answerAttributed;

            UITapGestureRecognizer *readMoreGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(readMoreDidClickedGesture:)];

            readMoreGesture.numberOfTapsRequired = 1;
            [label addGestureRecognizer:readMoreGesture];

            label.userInteractionEnabled = YES;
        
        else
        

            NSLog(@"No need for 'Read More'...");

        
    

    -(void)readMoreDidClickedGesture:(id)sender
    
        if(ReadMoretag==1)
        

            ReadMoretag=0;

            NSString *readMoreText = @" Read Less";
            UIColor *color = [UIColor colorWithRed:21.0/255.0 green:40.0/255.0 blue:86.0/255.0 alpha:1]; // select needed color
            NSDictionary *attrs = @ NSForegroundColorAttributeName : color ;
            NSMutableAttributedString *readMoreAttributed = [[NSMutableAttributedString alloc] initWithString:readMoreText attributes:attrs];

            NSMutableAttributedString *answerAttributed = [[NSMutableAttributedString alloc] initWithString:StrProfileSummary attributes:@
                                                                                                                                         NSFontAttributeName : LblProfilEsummary.font


                                                                                                                                         ];






            [answerAttributed appendAttributedString:readMoreAttributed];
            LblProfilEsummary.attributedText = answerAttributed;





        

        else
        
            ReadMoretag=1;
            [self addReadMoreStringToUILabel:LblProfilEsummary];

        
    

【讨论】:

好的..试试看,让我知道你的反馈 它可以,但在新的更大尺寸的设备上完全不工作,例如 iPhone Xs Max、iPhones XS、iPhones XR,【参考方案2】:
CGFloat width = ([UIScreen mainScreen].bounds.size.width - 40);
         NSMutableAttributedString *stringText;
         NSInteger oneLineHeight = [CommonMethods findHeightForText:@"A" havingWidth:width andFont:kMJFontNameANDSize(kMJHelveticaNeueRegular, 11.0)].height;
         NSInteger totalHeight = [CommonMethods findHeightForText:_strCaption havingWidth:width andFont:kMJFontNameANDSize(kMJHelveticaNeueRegular, 11.0)].height;
         NSInteger noOfLines = totalHeight/oneLineHeight;

         if(noOfLines > kNumberOfLines) 
             CGSize size = CGSizeMake(width, 42);

             NSInteger maxCharLength = [self findPageSplits:_strCaption size:size font:kMJFontNameANDSize(kMJHelveticaNeueRegular, 11.0)];
             NSLog(@"%ld",(long)maxCharLength);

             NSString *strText = [_strCaption substringWithRange:NSMakeRange(0, maxCharLength-10)];
             strText = [NSString stringWithFormat:@"%@... More",strText];
             NSLog(@"%@",strText);

             stringText = [[NSMutableAttributedString alloc] initWithString:strText];
             [stringText addAttribute: NSFontAttributeName value:kMJFontNameANDSize(kMJHelveticaNeueRegular, 11.0) range:[strText rangeOfString:ellipsis]];
             [stringText addAttribute: NSForegroundColorAttributeName value:RGB120200230 range:[strText rangeOfString:ellipsis]];
         
         else
         
             stringText = [[NSMutableAttributedString alloc] initWithString:_strCaption];
         

使用此方法进行分页

- (NSInteger) findPageSplits:(NSString*)string size:(CGSize)size font:(UIFont*)font;
    
        //Session Content Split
        CTFontRef fnt;
        CFAttributedStringRef str;
        CTFramesetterRef fs;

        fnt = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize,NULL);
        str = CFAttributedStringCreate(kCFAllocatorDefault,
                                       (CFStringRef)string,
                                       (CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)fnt,kCTFontAttributeName,nil]);
        fs = CTFramesetterCreateWithAttributedString(str);
        CFRange r = 0,0;
        CFRange res = 0,0;
        CTFramesetterSuggestFrameSizeWithConstraints(fs,r, NULL, size, &res);
        return res.length;
    

【讨论】:

以上是关于如何在 uilabel 中显示“显示更多”文本?的主要内容,如果未能解决你的问题,请参考以下文章

动画改变 UILabel 的行数

显示 UILabels 和 UIButtons 相互内联的方法

如何在 UILabel 上仅突出显示触摸文本的背景

我们可以在 UILabel 上显示 HTML 表格吗(属性文本)

如何让 UILabel 显示轮廓文本?

如何让 UILabel 显示轮廓文本?