垂直对齐UILabel [英] Vertically align UILabel

查看:135
本文介绍了垂直对齐UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用的UILabel视图中垂直对齐文本。问题是我希望文本垂直对齐顶部,标签大小为280 x 150.我只能实现这两件事之一。如果我删除该行

I am trying to vertically align the text in the UILabel view of my app. The problem is that I want the text to be vertically aligned to the top and the size of the label to be 280 x 150. I am only able to achieve one of these 2 things. If I remove the line

[myLabel sizeToFit];

然后文本的对齐方式正常但是大小搞砸了。但是如果我添加上面的行,那么对齐就搞砸了但是大小没问题。我如何解决这个问题。

then the alignment of the text is alright but the size is messed up. But if I add the above line, then the alignment is messed up but the size is alright. How do I fix this problem.

我已添加以下代码 -

I've added the code below -

CGRect labelFrame = CGRectMake(22, 50, 280, 150);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setText:finalRecipe];
[myLabel setBackgroundColor: [UIColor lightGrayColor]];
[myLabel setNumberOfLines:0];
[myLabel sizeToFit];
[self.view addSubview:myLabel];


推荐答案

我是FXLabel的作者,而我不知道Manish,我相信他正在努力帮助(如果他是在为我做广告,我当然没有要求他,而且我也没有付钱给他 - 对不起Manish!)。

I'm the author of FXLabel, and whilst I don't know Manish, I believe he was trying to help (if he was advertising for me, I certainly didn't ask him to, and I'm not paying him - sorry Manish!).

FXLabel的一个特性是它尊重标签的UIContentMode属性,如接口构建器中所设置的那样。这意味着你可以设置label.contentMode = UIViewContentModeTop;将文本对齐到标签视图的顶部(这对常规UILabel不起作用)。相关的例子在这里:

One of FXLabel's features is that it respects the UIContentMode property of the label, as set in Interface builder. This means you can set label.contentMode = UIViewContentModeTop; to align the text to the top of the label view (which doesn't work for a regular UILabel). The relevant example is here:


https://github.com/nicklockwood/FXLabel/tree/master/Examples/Text%20Alignment

FXLabel是UILabel的一个子类,所以我认为对于提出的问题来说这是一个非常好的解决方案。但是,如果海​​报宁愿在不使用第三方库的情况下解决问题(这是可以理解的),那么这里是代码:

FXLabel is a drop-in subclass of UILabel, so I think it's a pretty good solution for the question being posed. However if the poster would rather solve the problem without using a 3rd party library (which is understandable) then here is the code to do it:

CGRect labelFrame = CGRectMake(22, 50, 280, 150);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setText:finalRecipe];
[myLabel setBackgroundColor: [UIColor lightGrayColor]];
[myLabel setNumberOfLines:0];

CGFloat fontSize = 0.0f;
labelFrame.size = [myLabel.text sizeWithFont:myLabel.font
                                minFontSize:myLabel.minimumFontSize
                             actualFontSize:&fontSize
                                   forWidth:labelFrame.width
                            lineBreakMode:myLabel.lineBreakMode];

myLabel.frame = labelFrame;
[self.view addSubview:myLabel];

注意:(这是未经测试的,如果有任何拼写错误,请道歉)

Note:(This is untested, so apologies if there are any typos)

这篇关于垂直对齐UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆