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

时间:2017-06-29 06:13:38

标签: ios objective-c

我必须在uilabel的某个限制之后使用显示更多文本。当我点击uilabel显示更多文本时,标签应该展开,显示更多文本应该更改为显示更少。

2 个答案:

答案 0 :(得分:5)

使用以下代码进行回答。

 - (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];

        }
    }

答案 1 :(得分:1)

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;
    }