Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

DTAttributedLabel truncation doesn't work #364

Closed
kRaw1er opened this Issue Mar 28, 2013 · 10 comments

Comments

Projects
None yet
4 participants

kRaw1er commented Mar 28, 2013

When numberOfLines set to non 0 value DTAttributeLabel just ignores it.

Collaborator

odrobnik commented Apr 2, 2013

@kRaw1er Please elaborate your bug report.

kRaw1er commented Apr 3, 2013

For example, for this code:

DTAttributedLabel * label = [[[DTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)] autorelease];
label.numberOfLines = 1;
label.lineBreakMode = NSLineBreakByTruncatingTail;
label.attributedString = [[[NSAttributedLabel alloc] initWithHtmlData:@"Very long lorem ipsum sit amet title bla bla bla foo bar" documentAttributes:nil] autorelease];

The result should be something like this (i.e. with "..." at the end):

Very long lorem ipsum...

But DTAttributedLabel draws whole text without truncation:

Very long lorem ipsum
sit amet title bla bla
bla foo bar

bkook commented Apr 13, 2013

I'm having same problem.
In DTCoreTextLayoutFrame.m, truncateLine is always NO regardless numberOfLines and lineBreakMode.

shc-vj commented Apr 15, 2013

Quick fix for this issue (not optimal with terms of performance)

Add setters methods to DTCoreTextLayoutFrame for numberOfLines , lineBreakMode and truncationString properties that reset lines cache:

- (void)setNumberOfLines:(NSInteger)numberOfLines
{
    if( _numberOfLines != numberOfLines ) {
        _numberOfLines = numberOfLines;
        // clear lines cache
        _lines = nil;
    }
}

- (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode
{
    if( _lineBreakMode != lineBreakMode ) {
        _lineBreakMode = lineBreakMode;
        // clear lines cache
        _lines = nil;
    }
}

- (void)setTruncationString:(NSAttributedString *)truncationString
{
    if( ![_truncationString isEqualToAttributedString:truncationString] ) {
        _truncationString = truncationString;

        if( self.numberOfLines > 0 ) {
            // clear lines cache
            _lines = nil;
        }
    }
}

Additionally _buildLinesWithTypesetter method need small fix with indexes (decreased by 1) like this:

**lines 295-302 original:

                if (truncationType == kCTLineTruncationEnd)
                {
                    index += oldLineRange.length;
                }
                else if (truncationType == kCTLineTruncationMiddle)
                {
                    index += oldLineRange.length/2.0;
                }

replace with:

                if (truncationType == kCTLineTruncationEnd)
                {
                    index += (oldLineRange.length > 0 ? oldLineRange.length - 1 : 0);
                }
                else if (truncationType == kCTLineTruncationMiddle)
                {
                    index += (oldLineRange.length > 1 ? (oldLineRange.length/2.0 - 1) : 0);
                }

bkook commented Apr 16, 2013

Thank you for your comment.
I applied your instructions but it crashes when I set numberOfLines to 1 and lineBreakMode to UILineBreakModeTailTruncation.

this is exception..
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds'

and application crashes here:
In DTCoreTextLayoutFrame.m...
[_attributedStringFragment enumerateAttribute:NSAttachmentAttributeName inRange:lineRange options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(DTTextAttachment *attachment, NSRange range, BOOL *stop) {...

shc-vj commented Apr 16, 2013

Did you apply changes to _buildLinesWithTypesetter ?

bkook commented Apr 16, 2013

Yes. I modified both.

나의 iPhone에서 보냄

      1. 오전 10:14 shc-vj notifications@github.com 작성:

Did you apply changes to _buildLinesWithTypesetter ?


Reply to this email directly or view it on
GitHubhttps://github.com/Cocoanetics/DTCoreText/issues/364#issuecomment-16421447
.

Collaborator

odrobnik commented Apr 16, 2013

@shc-vj why don't you make your changes available for pulling in a branch?

shc-vj commented Apr 16, 2013

OK, I have issued pull request for this quick fix.

Collaborator

odrobnik commented Apr 16, 2013

I hope you guys tested this. I merged it without further inspection.

@ghost ghost assigned odrobnik Apr 16, 2013

@odrobnik odrobnik closed this Apr 16, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment