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

Shi*_*hra 2 objective-c ios

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

小智 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];

        }
    }
Run Code Online (Sandbox Code Playgroud)