How to add button to the end of text like Facebook's “Continue reading”?

巧了我就是萌 提交于 2019-12-04 19:23:21

问题


When a status post is too long, Facebook app cuts the text and adds "Continue Reading" at the end. How does it know where to cut the text and add "... Continue Reading"?

Not just adding a button to textView or label but how do I cut the string. In picture below, for example, I limited number of lines to 7. I could just place a button over lower right corner of the textView or label but it might be overlap some characters.


回答1:


This should help you:)

NSString *str=self.strQuestionTitle;

CGRect rect=CGRectMake(51, 16, 257, 0);

CGSize size=[str sizeWithFont:self.lblQuestion.font constrainedToSize:CGSizeMake(257, 3000) lineBreakMode:self.lblQuestion.lineBreakMode];
int lines=(size.height/self.lblQuestion.font.pointSize);
self.lblQuestion.numberOfLines=lines;
rect.size=size;
if(lines>2)
{
    if(lines==3 &&[str length]>66)
    {
        str=[str substringToIndex:66];
    str=[str stringByAppendingString:@"...Read More"];
    size=[str sizeWithFont:self.lblQuestion.font constrainedToSize:CGSizeMake(257, 67) lineBreakMode:self.lblQuestion.lineBreakMode];

    int lines=(size.height/self.lblQuestion.font.pointSize);
    self.lblQuestion.numberOfLines=lines;

    rect.size=CGSizeMake(257, 67);
    }
    else if(lines>3)
    {
        str=[str stringByAppendingString:@"...Read More"];
        size=[str sizeWithFont:self.lblQuestion.font constrainedToSize:CGSizeMake(257, 67) lineBreakMode:self.lblQuestion.lineBreakMode


              ];

        int lines=(size.height/self.lblQuestion.font.pointSize);
        self.lblQuestion.numberOfLines=lines;

        rect.size=CGSizeMake(257, 67);
    }

    //self.lblQuestion.lineBreakMode=NSLineBreakByTruncatingHead;
}


来源:https://stackoverflow.com/questions/18386498/how-to-add-button-to-the-end-of-text-like-facebooks-continue-reading

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!