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

二次信任 提交于 2019-12-18 13:59:31

问题


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 to contain the content.

However on iPad the screen is larger and I want the text to appear at the top left point of the label, not in the middle as it do when you use numberOfLines property. I know you can horizontally align the text, but it must be possible to align the text to the top left also? Or is it impossible.

Screenshot:


回答1:


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.




回答2:


I actually noticed that using

[Blue setSizeToFit]

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




回答3:


You can do it as follows

  1. Set your label's numberOfLines property 0 from IB

  2. Set your label's lineBreakMode as UILineBreakModeWordWrap (very very important)

now whatever you set on the label just append few @"\n" to it..... ex.-

[yourTextLabel setText:@"myLabel\n\n\n\n\n"];



回答4:


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



回答5:


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




回答6:


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.




回答7:


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




回答8:


You can Simply Use A UITextView ,and Set its UserInteraction Enabled to No,And All of your problems will be solved!



来源:https://stackoverflow.com/questions/6557178/is-it-possible-to-vertically-align-text-inside-labels-with-a-large-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!