Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

enter image description here

like image 351
Thanakrit Weekhamchai Avatar asked Aug 22 '13 16:08

Thanakrit Weekhamchai


1 Answers

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;
}
like image 180
vni Avatar answered Oct 19 '22 10:10

vni