Truncate text in TTStyledTextLabel

I use TTStyledTextLabel in my project to analyze hyperlinks, everything works fine. The only problem I encountered is to truncate long text - show ellipses if the text does not fit the borders of the TTStyledTextLabel .

In other words, I need the same behavior as UILabel, which adds ellipses to indicate that some text is cropped. I searched in the TTStyledTextLabel and TTStyledText , there are no conditions for this. The following is the code I used in my subclass of UITableViewCell to set the TTStyledTextLabel frame TTStyledTextLabel :

 -(void) layoutSubviews { [super layoutSubviews]; . . . CGSize maxSize = CGSizeMake(self.contentView.frame.size.width -TEXT_OFFSET_WIDTH, TT_TEXT_MAX_HEIGHT); [[[self textLabelTTStyled] text] setWidth:maxSize.width]; [[self textLabelTTStyled] sizeToFit]; double heigthForTTLabel = [[[self textLabelTTStyled] text] height]; if (heigthForTTLabel > maxSize.height) heigthForTTLabel = maxSize.height; // Do not exceed the maximum height for the TTStyledTextLabel. **// The Text was supposed to clip here when maximum height is set!** CGSize mTempSize = CGSizeMake([[[self textLabelTTStyled] text] width], heigthForTTLabel); CGRect frame = CGRectMake(TEXT_OFFSET_X,TEXT_OFFSET_Y,mTempSize.width, mTempSize.height); self.textLabelTTStyled.frame = frame; . . . } 

And in tableView:cellForRowAtIndexPath: I set the text like this to my TTStyledTextLabel :

 TTStyledText *styledStatusMessage = [TTStyledText textFromXHTML:@"This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on? This is a really long text, how long can this go on?" lineBreaks:YES URLs:YES]; if (nil == styledStatusMessage) { styledStatusMessage = [TTStyledText textWithURLs:[statusMessage title] lineBreaks:YES]; [[cell textLabelTTStyled] setText:styledStatusMessage]; } 

Excess text is simply discarded, ellipses are not added by default to indicate that the text is cropped. Any solutions to this problem?

Thanks Raj

+4
source share
1 answer

I believe that you are using the private Three20 api, which has a chance to abandon the appstore. Just check once before shipping. Genrally, you can install any set of any custom buttons with a title on it

[setContentHorizontalAlignment button: UIControlContentHorizontalAlignmentLeft]; [button setTitleEdgeInsets: UIEdgeInsetsMake (0.0, 10.0, 0.0, 0.0)]; - This line can be used to set the edges of the headers. Hope this helps.

-1
source

All Articles