Tuesday, April 28, 2026
HomeiOS Developmentios - Label resize with textual content utilizing loop, spacing shouldn't be...

ios – Label resize with textual content utilizing loop, spacing shouldn’t be variable with uilabel dimension

[ad_1]

Uilabel peak is meant to alter with size of textual content, however within the loop it isn’t doing that. NSlog exhibits that the uilabel peak is totally different for every label. However one thing goes incorrect when the label is being created.

 CGSize screensize = [[UIScreen mainScreen]bounds].dimension;
 CGFloat screensizewidth = screensize.width;
 
 NSLog(@"display dimension   ----------  %f",screensizewidth);
 int ycounter = 0;
 _chatListRect.backgroundColor= [UIColor whiteColor];
 NSArray *commaChat01 = @[@"test 01 g01 01 01 01 01 01 01 01 01 01 01 01 01 01 011 011 011 011 011 022 033 044 055 066 066 077 088 088 099",@"test 02----02----02",@"test 03 03 03 test",@"test 04"];
    for (NSString* str in commaChat01){
        UILabel * label = [[UILabel alloc] init];
        label.textual content = str;
        label.numberOfLines = 0;
        CGSize maximumLabelSize = CGSizeMake(screensizewidth, 9999); //280:max width of label and 9999-max peak of label.
    
// use font info from the UILabel to calculate the scale
        CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
        NSLog(@"one    ----- %@@",NSStringFromCGSize(expectedLabelSize));
     
// create a body that's stuffed with the UILabel body information
        CGRect newFrame = label.body;
        NSLog(@"two    ----- %@@",NSStringFromCGRect(label.body));
    
// resizing the body to calculated dimension
        newFrame.dimension.peak = expectedLabelSize.peak;
        newFrame.dimension.width = expectedLabelSize.width;
      //newFrame.origin.y =y;
      //newFrame.origin.x =y;
        NSLog(@"three   ----- %f@",expectedLabelSize.peak);
    
// put calculated body into UILabel body
        label.body = newFrame;
        NSLog(@"4   ----- %@@",NSStringFromCGRect(label.body));
        label.backgroundColor = [UIColor redColor];
    
//UIView *testview =[[UIView alloc]initWithFrame:CGRectMake(0,(ycounter*(y+h))+y, w,h)];
        UIView *testview =[[UIView alloc]initWithFrame:CGRectMake(0,(ycounter*((expectedLabelSize.peak+20)+(expectedLabelSize.peak+20)))+10,expectedLabelSize.width,expectedLabelSize.peak)];
        NSLog(@"5   ----- %f@",(expectedLabelSize.peak));
        NSLog(@"six   ----- %f@",(ycounter*((expectedLabelSize.peak+20)+(expectedLabelSize.peak+20))));
        [self.view layoutIfNeeded];
        [testview addSubview:label];
        [_chatListRect addSubview:testview];
         ycounter = ycounter+1;
                                    }

I feel the issue lies with ycounter, however I am undecided tips on how to go about fixing it.
I’ve been making an attempt for a number of weeks now.
Sorry, I am not that skilled with objective-c.
A pointer in the proper course is all I ask.
Thanks on your time.

screenshot, all spacing is similar and never altering with peak of uilabel

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments