ios 是否可以将标签内的文本与“大”框架垂直对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6557178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:36:39  来源:igfitidea点击:

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

iphoneobjective-ciosuitableviewuilabel

提问by LuckyLuke

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.

在我的应用程序中,我有多个带有自定义单元格的表格视图。单元格中的一些文本分布在 2-4 行之间,因此标签的高度足够大以包含内容。

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

但是在 iPad 上,屏幕更大,我希望文本出现在标签的左上角,而不是像使用numberOfLines属性时那样出现在中间。我知道您可以水平对齐文本,但也必须可以将文本与左上角对齐?或者是不可能的。

Screenshot:

截屏:

enter image description here

在此处输入图片说明

采纳答案by EmptyStack

Its impossibleto align the text in UILabelvertically. But, you can dynamically change the height of the label using sizeWithFont:method of NSString, and just set its xand yas you want.

不可能对齐文本中的UILabel垂直。但是,您可以使用NSString 的sizeWithFont:方法动态更改标签的高度,并根据需要设置其xy

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

作为替代方案,您可以使用UITextField。它支持contentVerticalAlignmentpeoperty,因为它是UIControl的子类。您必须将其userInteractionEnabled设置为NO以防止用户在其上键入文本。

回答by nembleton

I actually noticed that using

我实际上注意到使用

[Blue setSizeToFit]

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

确实与顶部垂直对齐。默认居中。

回答by Hons

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

sizeToFit在 中不起作用UITableCellView,因为在滚动期间会重复使用单元格。

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 postabout it

由于在网上很难找到解决方案,我写了一篇关于它的简短文章

回答by Fahim Parkar

You can do it as follows

你可以这样做

  1. Set your label's numberOfLinesproperty 0 from IB

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

  1. numberOfLines从 IB设置标签的属性 0

  2. 将您的标签设置lineBreakModeUILineBreakModeWordWrap非常非常重要

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

现在无论您在标签上设置什么,只需在其后面添加几个 @"\n" ..... 例如.-

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

回答by AppsolutEinfach

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

在 iOS 7sizeWithFont:中现已弃用!UILabel必须调整像子类化这样的几个解决方案。

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

我的顶部对齐标签文本的解决方案:在子类TopVerticalAlignmentLabel : UILabel覆盖中drawRect:,如下所示:

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

回答by carmen_munich

I added in viewDidLoad

我加了 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.

确保您的约束设置正确,以便允许标签缩小或增长。否则它不会工作。

回答by Kamar Shad

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.

这是 SomeOne 提出的相同问题。你会找到更合适的方法来解决这个问题。他们做了很好的解释。

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 textas below snippet of code described.

在该线程中,许多答案建议根据text以下描述的代码片段为UILabel 设置动态框架 。

    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 textFieldhere it is

你可以得到更精确的想法,而不是在这里使用textField

回答by Waleed Bashir Heraj

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

您可以简单地使用一个 UITextView ,并将其 UserInteraction Enabled 设置为 No,您的所有问题都将得到解决!