Is it possible to vertically align text inside labels with a “large” frame

前端 未结 8 1565
梦谈多话
梦谈多话 2021-01-01 11:58

In my application I have multiple tableviews with custom cells. Some of the text in the cells are spread out on between 2-4 lines so the height of the label is large enough

相关标签:
8条回答
  • 2021-01-01 12:29

    sizeToFit does not work in UITableCellView, because the Cells are reused during scrolling.

    The solution is to calculate the table cell height according to the font and font size and adjust the label height to it.

    As it was quite difficult to find the solution on the web I wrote a short post about it

    0 讨论(0)
  • 2021-01-01 12:33

    I actually noticed that using

    [Blue setSizeToFit]
    

    does align vertically to the top. The default being centered.

    0 讨论(0)
  • 2021-01-01 12:34

    Its impossible to align the text in UILabel vertically. But, you can dynamically change the height of the label using sizeWithFont: method of NSString, and just set its x and y as you want.

    As an alternative you can use UITextField. It supports the contentVerticalAlignment peoperty as it is a subclass of UIControl. You have to set its userInteractionEnabled to NO to prevent user from typing text on it.

    0 讨论(0)
  • 2021-01-01 12:35

    I added in viewDidLoad

    self.textLabel.numberOfLines = 0;
    [self.textLabel sizeToFit];
    

    make sure your constraints are set correct, so that the label is allowed to shrink or grow. Otherwise it won't work.

    0 讨论(0)
  • 2021-01-01 12:42

    Here is the Same Question asked by SomeOne. you'll find more appropriate way to solve this problem.they guys did great explanation over it.

    I think so, you should take a look of this.

    here in that thread many answers suggest set the dynamic frame for the UILabel on the basis of text as below snippet of code described.

        CGSize theStringSize = [textToBeUsed sizeWithFont:lblTitle.font  constrainedToSize:labelSize lineBreakMode:lblTitle.lineBreakMode];
        lblTitle.frame = CGRectMake(lblTitle.frame.origin.x, lblTitle.frame.origin.y, theStringSize.width, theStringSize.height);
        lblTitle.text = theText;
    
       // lblTitle is the Label used for showing the Text.
    

    you can get more precise idea rather than using textField here it is

    0 讨论(0)
  • 2021-01-01 12:43

    In iOS 7 sizeWithFont: is now deprecated! Several solutions like subclassing UILabel have to be adapted.

    My solution for top aligned label text: In a subclass TopVerticalAlignmentLabel : UILabel override drawRect: as follows:

    - (void)drawRect:(CGRect)rect
    {
        CGRect labelStringRect = [self.text boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX)
                                                         options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                                      attributes:@{ NSFontAttributeName: self.font, /* further attributes */}
                                                         context:nil];
    
        [super drawTextInRect:CGRectMake(0, 0, self.frame.size.width, labelStringRect.size.height)];
    }
    
    0 讨论(0)
提交回复
热议问题