Datenshi
Datenshi

Reputation: 1191

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.

Upvotes: 4

Views: 8606

Answers (4)

nmock
nmock

Reputation: 1917

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.

Upvotes: 0

Murali
Murali

Reputation: 1889

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);
}

Upvotes: 2

Nookaraju
Nookaraju

Reputation: 1668

 _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.

Upvotes: 2

Sagi Mann
Sagi Mann

Reputation: 3620

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

Upvotes: 0

Related Questions