triggs
triggs

Reputation: 5900

Dynamic UILabel truncating the text

I'm using code provided in this answer to create a dynamic label and it works for the most part. But whenever the label text goes over 94 characters in length it gets truncated and ellipsis' are added.

There is one more odd thing about this is that if I add more characters to the string they are shown but the last 2 lines are still truncated.

Eg.

The string:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one two three.

shows up like this:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one tw...

But when I double the string by using the same sentence again in the label it show more of te text but still truncates it.

Eg.

The string:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one two 
three. this is a very very long 
string with lots of words to test 
the dynamic bubble sizing one 
two three.

shows like this:

this is a very very long string 
with lots of words to test the 
dynamic bubble sizing one two 
three. this is a very very long 
string with lots of words to tes...

Here's the code I'm using.

NSString *temp = [NSString stringWithFormat:@"this is a very very long string with lots of words to test the dynamic bubble sizing one two three"];

captionLabel.text = temp;
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);

CGSize expectedLabelSize = [temp sizeWithFont:captionLabel.font 
                                  constrainedToSize:maximumLabelSize 
                                      lineBreakMode:captionLabel.lineBreakMode]; 

//adjust the label the the new height.
CGRect newFrame = captionLabel.frame;
newFrame.size.height = expectedLabelSize.height;
captionLabel.frame = newFrame; 

Hope someone has an idea because this has me scratching my head.

EDIT

Using captionLabel.frame.size.width instead of hard-coded 296 fixed it, thanks to @troolee, if he/she chooses to create an answer I will mark it correct.

Upvotes: 4

Views: 7775

Answers (3)

macpandit
macpandit

Reputation: 833

Instead of captionLabel.lineBreakMode , just write UILineBreakModeWordWrap. It should work.

Upvotes: 1

triggs
triggs

Reputation: 5900

I was hoping @troolee would have made his comment an answer by now but since he hasn't I'm going to post the answer and mark it correct so I can close off this question.

Using captionLabel.frame.size.width instead of hard-coded 296 fixed it.

Upvotes: 1

lu yuan
lu yuan

Reputation: 7227

Try the following UILabel Category. Thanks for the creator.

UILabel+VAlign.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface UILabel (VAlign)
- (void) setVerticalAlignmentTopConstrainedToSize:(CGSize)size;
@end

UILabel+VAlign.h

#import "UILabel+VAlign.h"

@implementation UILabel (VAlign)

- (void) setVerticalAlignmentTopConstrainedToSize:(CGSize)size
{
    CGSize textSize = [self.text sizeWithFont:self.font
                            constrainedToSize:size
                                lineBreakMode:self.lineBreakMode];

    CGRect textRect = CGRectMake(self.frame.origin.x,
                                 self.frame.origin.y,
                                 self.frame.size.width,
                                 textSize.height);
    [self setFrame:textRect];
    [self setNeedsDisplay];
}

@end

Upvotes: 0

Related Questions