ios 使字体与 UILabel 一起增长(通过自动布局调整大小) - 如何在 Interface Builder 中做到这一点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22688851/
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
Make font grow together with UILabel (resized by Auto Layout) - how to do it in Interface Builder?
提问by Alexander Farber
In a simple iPhone appI display a letter tile (custom UIView
with an image and 2 labels) by the following code in viewDidLoad:
在一个简单的 iPhone 应用程序中,我UIView
通过以下代码在viewDidLoad 中显示一个字母磁贴(自定义带有一个图像和 2 个标签):
DraggedTile *tile = [[[NSBundle mainBundle] loadNibNamed:@"DraggedTile"
owner:self
options:nil] firstObject];
tile.frame = CGRectMake(10 + arc4random_uniform(100),
10 + arc4random_uniform(100),
kWidth,
kHeight);
[self.view addSubview:tile];
This works okay, but I would like to make the letter tile grow - when I supply bigger width and height parameters to the CGRectMake
.
这工作正常,但我想让字母瓷砖增长 - 当我为CGRectMake
.
So in Xcode 5.1 Interface Builder I open the DraggedTile.xib
and enable "Auto Layout".
因此,在 Xcode 5.1 Interface Builder 中,我打开DraggedTile.xib
并启用“自动布局”。
Then for the image
and letter
I add constraints to the left, top, right, bottom edges of the parent.
然后为image
and letter
I 添加约束到父级的左、上、右、下边缘。
For the letter
label I also set "Lines" to 0 and "Content Compression Resistance Priority" to 1000 (here fullscreen 1and fullscreen 2):
对于letter
标签,我还将“行”设置为 0,将“内容压缩阻力优先级”设置为 1000(此处为全屏 1和全屏 2):
Then I modify the code to use bigger width and height:
然后我修改代码以使用更大的宽度和高度:
tile.frame = CGRectMake(10 + arc4random_uniform(100),
10 + arc4random_uniform(100),
2 * kWidth,
2 * kHeight);
Here is the result:
结果如下:
Both the background image
and the letter
label seem to have grown as intended.
背景image
和letter
标签似乎都按预期增长。
However the font size of the label hasn't grown.
但是标签的字体大小没有增长。
I've searched around and I think that I need something like tile.letter.adjustsFontSizeToFitWidth = YES
. But at the same time I can not use it with "Auto Layout" being on...
我四处搜索,我认为我需要类似tile.letter.adjustsFontSizeToFitWidth = YES
. 但同时我不能在“自动布局”打开的情况下使用它......
So my question isif there is any option in the "Interface Builder" available to make the font size grow as well?
所以我的问题是“界面生成器”中是否有任何选项可以使字体大小也增加?
UPDATE:
更新:
I've tried ismailgulek's suggestion to set the font size to 200 in Interface Builder and in the codetile.letter.adjustsFontSizeToFitWidth = YES
and it looks promising (here fullscreen):
我已经尝试过 ismailgulek 的建议,在 Interface Builder 和代码中将字体大小设置为 200 tile.letter.adjustsFontSizeToFitWidth = YES
,它看起来很有希望(这里是全屏):
but I have now the problem that I have set 40px as the letter.bottom
constraint in Interface Builder. But what if I need a bigger tile frame? Is there a way to use percentage instead of absolute pixel value in that constraint?
但我现在遇到的问题是我letter.bottom
在 Interface Builder中将 40px 设置为约束。但是如果我需要一个更大的瓷砖框架怎么办?有没有办法在该约束中使用百分比而不是绝对像素值?
And another questionis if it's possible to set the adjustsFontSizeToFitWidth
somewhere in the Interface Builder or do I have to use source code for that? I've tried adding a key to the "User Defined Runtime Attributes" - but then the app crashes at the runtime:
另一个问题是,是否可以adjustsFontSizeToFitWidth
在 Interface Builder 中设置某个位置,或者我是否必须为此使用源代码?我尝试向“用户定义的运行时属性”添加一个键 - 但随后应用程序在运行时崩溃:
回答by ismailgulek
adjustsFontSizeToFitWidth
property of UILabel
will not grow up the font size, it is only for reducing according to the documentation: Normally, the label text is drawn with the font
you specify in the font property. If this property is set to YES
, however, and the text in the text property exceeds the label's bounding rectangle, the receiver starts reducing the font size until the string fits or the minimum font size is reached.
adjustsFontSizeToFitWidth
的属性UILabel
不会增大字体大小,只是根据文档缩小:通常情况下,标签文本是用font
您在字体属性中指定的绘制的。YES
但是,如果此属性设置为,并且 text 属性中的文本超出了标签的边界矩形,则接收器开始减小字体大小,直到字符串适合或达到最小字体大小。
Butif you set font size big enough (say 200) and set adjustsFontSizeToFitWidth
to YES
, i think you would get interestingly valuable results.
但是,如果您将字体大小设置得足够大(比如 200)并设置adjustsFontSizeToFitWidth
为YES
,我认为您会得到非常有价值的结果。
And do not forget to set Baseline
as Align Centers
, otherwise your text may not be seen properly.
并且不要忘记设置Baseline
为Align Centers
,否则您的文本可能无法正常显示。
Please inform us about your results.
请告知我们您的结果。
回答by ishahak
For those who come here by the title and need a simple solution(after hoursof investigations):
对于那些通过标题来到这里并需要简单解决方案的人(经过数小时的调查):
Problem example:If you use auto layout and set the UILabel height to be proportional to the screen height, you will get a frame which is low for iPhone (landscape) and high for iPad. How to adjust font height automatically?
问题示例:如果您使用自动布局并将 UILabel 高度设置为与屏幕高度成正比,您将得到一个框架,iPhone(横向)低,iPad 高。如何自动调整字体高度?
Solution:in Interface Builder,
解决方案:在界面生成器中,
- Set "Autoshrink" to "Minimum font size"and set something for the size (it affects too-wide texts, but required for our solution).
- Set "Lines" to be 0, and make font size higher than the frame you will get in iPad.
- 将“Autoshrink”设置为“Minimum font size”并设置大小(它会影响太宽的文本,但我们的解决方案需要)。
- 将"Lines" 设置为 0,并使字体大小高于您将在 iPad 中获得的框架。
This will cause the font to get increased automaticallyto the current height.
这将导致字体自动增加到当前高度。
回答by crow
You can accomplish what you want by setting Autoshrink in your storyboard. It will cause the label to shrink to a minimum scale/explicit size when the screen size gets smaller. Make it work on the biggest screen size and then ensure it shrinks to the size you want in the storyboard preview.
您可以通过在故事板中设置 Autoshrink 来完成您想要的操作。当屏幕尺寸变小时,它会导致标签缩小到最小比例/显式尺寸。让它在最大的屏幕尺寸上工作,然后确保它在故事板预览中缩小到你想要的尺寸。
回答by HymanPearse
You can replace the class in Interface Builder with your own derived class and do resizing there.
您可以用您自己的派生类替换 Interface Builder 中的类并在那里调整大小。
Example:
例子:
/**
MYLabel does exactly the same like UILabel, except that the font size will automatically shrink
if the code is executed on an iPhone.
*/
class MYLabel: UILabel {
var onlyOnceToken: dispatch_once_t = 0
override func drawRect(rect: CGRect) {
dispatch_once(&onlyOnceToken) {
if let usedFont:UIFont = self.font {
// define your reference size here
let iPadHeight:CGFloat = 768
let screenBounds = UIScreen.mainScreen().bounds
let scale:CGFloat = screenBounds.height / iPadHeight
self.font = UIFont(name: usedFont.fontName, size: CGFloat(usedFont.pointSize) * scale)
}
}
super.drawRect(rect)
}
}
From now on you just need to replace the UILabelclass with MYLabelin the identity inspectorof IB and all your fonts automatically resize proportionally to your devices height.
从现在开始,您只需要在IB的身份检查器中用MYLabel替换UILabel类,您的所有字体都会根据您的设备高度自动按比例调整大小。
If you want to just grow it together with the label size, replace
如果您只想将其与标签大小一起增长,请替换
let screenBounds = UIScreen.mainScreen().bounds
with
和
let screenBounds = self.bounds.height
or any other custom scale formula. UIFontoffers you additional measurement properties you can do a lot with this approach.
或任何其他自定义比例公式。UIFont为您提供了额外的测量属性,您可以使用这种方法做很多事情。
回答by Ethan Allen
Also make sure you set “Line Breaks” from “Word Wrap” to “Truncate Tail.”
还要确保将“换行符”从“自动换行”设置为“截尾”。
回答by Lior Pollak
The font will not grow automatically.
You should increase/decrease it by yourself, in IB or programmatically.
字体不会自动增长。
您应该自己增加/减少它,在 IB 中或以编程方式。