Dynamic UILabel truncating the text

我只是一个虾纸丫 提交于 2019-12-07 07:41:37

问题


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.


回答1:


 NSString * test=@"this is test this is test inthis is test ininthis is test inthis is test inthis is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel ...this is test in uilabel END";

        testLabel.text = test;
        testLabel.numberOfLines = 0; //will wrap text in new line
        [testLabel sizeToFit];

        [self.view addSubview:testLabel];

This will surely help you.

Thanks




回答2:


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.




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/11558956/dynamic-uilabel-truncating-the-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!