UIlabel gets shrink on scrolling UiTableView

后端 未结 4 1479
天涯浪人
天涯浪人 2020-12-11 08:52

I have two UILabel in side of UICell, which contains dynamic text so i need to resize its frame according to content, for which im using [str

相关标签:
4条回答
  • 2020-12-11 09:16

    Try This code.

        label.numberOfLines = 0; // allows label to have as many lines as needed
        label.text = @"some long text";
        [label sizeToFit];
        NSLog(@"Label's frame is: %@", NSStringFromCGRect(label.frame));
    

    For reference : click here

    0 讨论(0)
  • 2020-12-11 09:33

    Instead of doing your own customization. Try with dynamic uitableviewcell height. You will find really usefull & comparatively easier than what you have done.

    You can check it here : http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ with full of explanation. Enjoy programming.

    0 讨论(0)
  • 2020-12-11 09:35
    NSString *reuseIdentifier = @"EventTitleCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    cell=nil;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] ;
    
    }
    
    0 讨论(0)
  • 2020-12-11 09:42

    In your case you need to set the height of cell based on the label.

    Check the link :

    http://dcraziee.wordpress.com/2013/05/22/calculate-size-of-uillabel-base-on-text-in/

    There is a function named

    -(CGFloat)getHeightForLabel:(NSString *)_str font:(UIFont *)fontOfObject
    

    Use that function to calculate height as :

    -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CGFloat _height  = [self getHeightForLabel:self.label.text font:[self.label font]];
        return _height;
    }
    

    and do the same while creating and adding label in the cell. I hope this will help.

    0 讨论(0)
提交回复
热议问题