NIAttributedLabel verticalTextAlignment supports only top #322

Closed
jeroentrappers opened this Issue Dec 14, 2012 · 3 comments

Comments

Projects
None yet
4 participants

There is a problem with the vertical Text Alignment. If I put NIVerticalTextAlignmentMiddle or NIVerticalTextAlignmentBottom, it has no effect. It always shows the text at the top of the NIAttributedLabel.

example code:

        amount = [[NIAttributedLabel alloc]initWithFrame:...];

        amount.textAlignment = NSTextAlignmentCenter;
        amount.verticalTextAlignment = NIVerticalTextAlignmentMiddle;

        NSString *amountValue = modelobject.value;
        if( [transaction.amount isPositive] )
        {
            amountValue = [NSString stringWithFormat:@"+%@", amountValue];
        }
        CGFloat actualFontSize;
        [amountValue sizeWithFont:somefont minFontSize:9 actualFontSize:&actualFontSize forWidth:amount.frame.size.width lineBreakMode:NSLineBreakByTruncatingTail];

        amount.font = somefont;

        amount.text = amountValue;
        amount.backgroundColor = [UIColor clearColor];
        amount.textColor = [UIColor whiteColor];

        amount.shadowColor = [UIColor colorWithWhite:0.f alpha:0.4f];
        amount.shadowOffset = CGSizeMake(0.f, 1.f);

        NSString *description = someotherModelObject.description;

        descr.text = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        descr.backgroundColor = [UIColor blueColor];
        descr.font = [[Palette shared] fontHelveticaNeueBold:12];
        descr.textColor = [[Palette shared] colorIron];
        descr.numberOfLines = 2;
        descr.lineBreakMode = UILineBreakModeWordWrap;
        [descr setVerticalTextAlignment:NIVerticalTextAlignmentBottom];
Owner

jverkoey commented Jan 3, 2013

This is a duplicate of #269.

I'm fixed this issue.

My English is not very good. ^^:

i don't know github. This Comment is first time. Please check.

1389 line -> CGContextSetTextPosition(ctx, lineOrigin.x, lineOrigin.y-rect.origin.y);

///////////////////////////////////////////////////////////////////////////////////////////////////

  • (void)drawAttributedString:(NSAttributedString *)attributedString rect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // This logic adapted from @mattt's TTTAttributedLabel
    // https://github.com/mattt/TTTAttributedLabel

    CFArrayRef lines = CTFrameGetLines(self.textFrame);
    NSInteger numberOfLines = [self numberOfDisplayedLines];

    BOOL truncatesLastLine = (self.lineBreakMode == NSLineBreakByTruncatingTail);
    CGPoint lineOrigins[numberOfLines];
    CTFrameGetLineOrigins(self.textFrame, CFRangeMake(0, numberOfLines), lineOrigins);

    for (CFIndex lineIndex = 0; lineIndex < numberOfLines; lineIndex++) {
    CGPoint lineOrigin = lineOrigins[lineIndex];
    CGContextSetTextPosition(ctx, lineOrigin.x, lineOrigin.y-rect.origin.y);
    CTLineRef line = CFArrayGetValueAtIndex(lines, lineIndex);

    BOOL shouldDrawLine = YES;
    
    if (truncatesLastLine && lineIndex == numberOfLines - 1) {
        // Does the last line need truncation?
        CFRange lastLineRange = CTLineGetStringRange(line);
        if (lastLineRange.location + lastLineRange.length < (CFIndex)attributedString.length) {
            CTLineTruncationType truncationType = kCTLineTruncationEnd;
            NSUInteger truncationAttributePosition = lastLineRange.location + lastLineRange.length - 1;
    
            NSDictionary *tokenAttributes = [attributedString attributesAtIndex:truncationAttributePosition
                                                                 effectiveRange:NULL];
            NSAttributedString *tokenString = [[NSAttributedString alloc] initWithString:kEllipsesCharacter
                                                                              attributes:tokenAttributes];
            CTLineRef truncationToken = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)tokenString);
    
            NSMutableAttributedString *truncationString = [[attributedString attributedSubstringFromRange:NSMakeRange(lastLineRange.location, lastLineRange.length)] mutableCopy];
            if (lastLineRange.length > 0) {
                // Remove any whitespace at the end of the line.
                unichar lastCharacter = [[truncationString string] characterAtIndex:lastLineRange.length - 1];
                if ([[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:lastCharacter]) {
                    [truncationString deleteCharactersInRange:NSMakeRange(lastLineRange.length - 1, 1)];
                }
            }
            [truncationString appendAttributedString:tokenString];
    
            CTLineRef truncationLine = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)truncationString);
            CTLineRef truncatedLine = CTLineCreateTruncatedLine(truncationLine, rect.size.width, truncationType, truncationToken);
            if (!truncatedLine) {
                // If the line is not as wide as the truncationToken, truncatedLine is NULL
                truncatedLine = CFRetain(truncationToken);
            }
            CFRelease(truncationLine);
            CFRelease(truncationToken);
    
            CTLineDraw(truncatedLine, ctx);
            CFRelease(truncatedLine);
    
            shouldDrawLine = NO;
        }
    }
    
    if (shouldDrawLine) {
        CTLineDraw(line, ctx);
    }
    

    }
    }

jyaif commented Oct 1, 2013

This pull request solves this bug:
#374

jverkoey closed this Nov 19, 2015

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