简体   繁体   中英

UILabel text gets cut off

I'm trying to dynamicly set label size. It works in a strange way, i get some of the text cut off. I first set my label text and then try to resize it like this way.

    _switch2Label.text = @"Call on alarm, there will be no call if other user of alarm system will recieve an alarm call and confirm (answer) it by pressing 0#";
    _switch2Label.numberOfLines = 0;
    [self newFrame:_switch2Label];

- (void) newFrame:(UILabel *) label
{

    CGSize maxSize = self.view.bounds.size;
    maxSize.width = maxSize.width - 30;
    CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maxSize lineBreakMode:label.lineBreakMode];

    CGRect newFrame = label.frame;
    newFrame.size.height = labelSize.height;
    label.frame = newFrame;

}

I only get three lines of text, while five is needed for this label. Maybe anyone could see my mistake here? If I add more text to label it gets shown, yet still about two lines of the label text gets cutt off.

 _switch2Label.text = @"Call on alarm, there will be no call if other user of alarm system will recieve an alarm call and confirm (answer) it by pressing 0#,";
 _switch2Label.numberOfLines = 0;
 [self newFrame:_switch2Label];



- (void) newFrame:(UILabel *) label
{

    CGSize maximumSize = CGSizeMake(label.frame.size.width, 10000);
    //maxSize.width = maxSize.width - 30;
    CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maximumSize lineBreakMode:UILineBreakModeWordWrap];

    CGRect newFrame = label.frame;
    newFrame.size.height = labelSize.height;
    label.frame = newFrame;

}

Use this code blocks , may help u.

I have changed your method...Please check it..it may help you..

- (void) newFrame:(UILabel *) label
{
     CGSize constraint = CGSizeMake(300, 1000.0f);
     CGSize size_txt_overview1 = [label.text sizeWithFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:15] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
     label.frame = CGRectMake(20,20, size_txt_overview1.width, size_txt_overview1.height+15);
}

Why programatically resizing the label? Is this something you cannot do in IB or using autorezizeMask ?

The label's constraint size isn't getting calculated as you intend, currently, your code is constraining the label height to the view's bound's height. Changing your maxSize instance to:

CGSize maxSize = CGSizeMake(self.view.bounds.size.width - 30, MAXFLOAT);
CGSize labelSize = ...

Doing so will ensure that the constraint is not bound by your view bounds. You may also want to consider setting the clipsToBounds property of your view if you want the label to be able to extend past your view's bounds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM