4

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.

Community
  • 1
  • 1
triggs
  • 5,890
  • 3
  • 32
  • 31
  • Check this out! http://stackoverflow.com/questions/9059631/autoshrink-on-a-uilabel-with-multiple-lines/9060833#9060833 – The dude Jul 19 '12 at 10:35
  • 2
    What if you will use `captionLabel.frame.size.width` instead of hard-coded `296`. Maybe width of the label is defferent than `296`? – Pavel Reznikov Jul 19 '12 at 10:43
  • captionLabel.frame.size.width fixed it, stick it in as an answer and I'll mark it correct. Seems once again I've been caught by some copy pasta code... – triggs Jul 19 '12 at 10:46
  • I have edited my code it will surely help you :) Thanks. – SALMAN Jul 19 '12 at 10:46
  • where are you setting up the label? It looks like you are assigning it a different font after the size is calculated? – Jesse Gumpo Jul 19 '12 at 11:09

3 Answers3

1

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

Girish
  • 4,692
  • 4
  • 35
  • 55
macpandit
  • 835
  • 1
  • 7
  • 11
1

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.

triggs
  • 5,890
  • 3
  • 32
  • 31
0

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
lu yuan
  • 7,207
  • 9
  • 44
  • 78