9

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

  • 1
    I imagine it just has a character limit in the preview text. If the text exceeds that limit, it cuts it off and adds "... Continue Reading". If you want to avoid cutting off a word, just search for the last instance of whitespace (space) before the character limit. – Paris Nelson Aug 22 '13 at 16:54
  • @ParisNelson So I use this to get string from text length limited rect. Then do what you said. Does this make sense? – Thanakrit Weekhamchai Aug 22 '13 at 17:17
  • @ParisNelson Try describing an actual solution, and not just a high level description of what you see. There is no 'character limit' on UILabel, nor does it automagically add clickable text with nice styling that says "Continue Reading". – Patrick Goley Apr 23 '15 at 17:15

1 Answers1

2

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;
}
taocp
  • 23,276
  • 10
  • 49
  • 62
vni
  • 56
  • 5