ios 将 UILabel 高度调整为文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25180443/
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
Adjust UILabel height to text
提问by TheBurgerShot
I have some labels which I want to adjust their height to the text, this is the code I wrote for this now
我有一些标签,我想将它们的高度调整为文本,这是我现在为此编写的代码
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
EDIT:
编辑:
The issue was not in this piece of code, so my fix is in the question itself. It might still be useful for others!
问题不在这段代码中,所以我的解决方法是问题本身。它可能对其他人仍然有用!
回答by Anorak
I've just put this in a playground and it works for me.
我刚刚把它放在操场上,它对我有用。
Updated for Swift 4.0
为 Swift 4.0 更新
import UIKit
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
let font = UIFont(name: "Helvetica", size: 20.0)
var height = heightForView("This is just a load of text", font: font, width: 100.0)
Swift 3:
斯威夫特 3:
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
回答by Phan Van Linh
If you are using AutoLayout, you can adjust UILabel
height by config UI only.
如果您使用 AutoLayout,则UILabel
只能通过配置 UI调整高度。
For iOS8 or above
iOS8 或以上
- Set constraint leading/trailing for your
UILabel
- And change the lines of
UILabel
from 1 to 0
- 为您设置约束前导/尾随
UILabel
- 并将行
UILabel
从 1更改为 0
For iOS7
对于iOS7
- First, you need to add contains height for
UILabel
- Then, modify the Relation from
Equal
toGreater than or Equal
- 首先,您需要添加包含高度
UILabel
- 然后,将 Relation 从 修改
Equal
为Greater than or Equal
- Finally, change the lines of
UILabel
from 1 to 0
- 最后,将行
UILabel
从 1更改为 0
Your UILabel
will automatically increase height depending on the text
您UILabel
将根据文本自动增加高度
回答by YannSteph
I create this extension if you want
如果你愿意,我会创建这个扩展
extension UILabel {
func setSizeFont (sizeFont: CGFloat) {
self.font = UIFont(name: self.font.fontName, size: sizeFont)!
self.sizeToFit()
}
}
回答by dimpiax
I have strong working solution.
我有强大的工作解决方案。
in layoutSubviews:
在布局子视图中:
_title.frame = CGRect(x: 0, y: 0, width: bounds.width, height: 0)
_title.sizeToFit()
_title.frame.size = _title.bounds.size
in text setter:
在文本设置器中:
_title.text = newValue
setNeedsLayout()
UPD.of course with this UILabel settings:
更新。当然有了这个 UILabel 设置:
_title.lineBreakMode = .ByWordWrapping
_title.numberOfLines = 0
回答by Jordan Amman
Just by setting:
只需通过设置:
label.numberOfLines = 0
The label automatically adjusts its height based upon the amount of text entered.
标签根据输入的文本量自动调整其高度。
回答by LeftyT
based on Anorak's answer, I also agree with Zorayr's concern, so I added a couple of lines to remove the UILabel and return only the CGFloat, I don't know if it helps since the original code doesn't add the UIabel, but it doesn't throw error, so I'm using the code below:
基于 Anorak 的回答,我也同意 Zorayr 的担忧,所以我添加了几行来删除 UILabel 并仅返回 CGFloat,我不知道它是否有帮助,因为原始代码没有添加 UIabel,但是它不会抛出错误,所以我使用下面的代码:
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat{
var currHeight:CGFloat!
let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.font = font
label.text = text
label.sizeToFit()
currHeight = label.frame.height
label.removeFromSuperview()
return currHeight
}
回答by Philipp Otto
The solution suggested by Anorak as a computed property in an extension for UILabel:
Anorak 建议作为 UILabel 扩展中的计算属性的解决方案:
extension UILabel
{
var optimalHeight : CGFloat
{
get
{
let label = UILabel(frame: CGRectMake(0, 0, self.frame.width, CGFloat.max))
label.numberOfLines = 0
label.lineBreakMode = self.lineBreakMode
label.font = self.font
label.text = self.text
label.sizeToFit()
return label.frame.height
}
}
}
Usage:
用法:
self.brandModelLabel.frame.size.height = self.brandModelLabel.optimalHeight
回答by iOS
In swift 4.1 and Xcode 9.4.1
在 swift 4.1 和 Xcode 9.4.1 中
Only 3 steps
只需3步
Step 1)
第1步)
//To calculate height for label based on text size and width
func heightForView(text:String, font:UIFont, width:CGFloat) -> CGFloat {
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
Step 2)
第2步)
//Call this function
let height = heightForView(text: "This is your text", font: UIFont.systemFont(ofSize: 17), width: 300)
print(height)//Output : 41.0
Step 3)
步骤 3)
//This is your label
let proNameLbl = UILabel(frame: CGRect(x: 0, y: 20, width: 300, height: height))
proNameLbl.text = "This is your text"
proNameLbl.font = UIFont.systemFont(ofSize: 17)
proNameLbl.numberOfLines = 0
proNameLbl.lineBreakMode = .byWordWrapping
infoView.addSubview(proNameLbl)
回答by Lirik
Following on @Anorak answer, i added this extension to String and sent an inset as a parameter, because a lot of times you will need a padding to your text. Anyway, maybe some you will find this usefull.
在@Anorak 回答之后,我将此扩展名添加到 String 并发送了一个插图作为参数,因为很多时候您需要对文本进行填充。无论如何,也许有些你会发现这很有用。
extension String {
func heightForWithFont(font: UIFont, width: CGFloat, insets: UIEdgeInsets) -> CGFloat {
let label:UILabel = UILabel(frame: CGRectMake(0, 0, width + insets.left + insets.right, CGFloat.max))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.font = font
label.text = self
label.sizeToFit()
return label.frame.height + insets.top + insets.bottom
}
}
回答by Shubham bairagi
just call this method where you need dynamic Height for label
只需在需要动态标签高度的地方调用此方法
func getHeightforController(view: AnyObject) -> CGFloat {
let tempView: UILabel = view as! UILabel
var context: NSStringDrawingContext = NSStringDrawingContext()
context.minimumScaleFactor = 0.8
var width: CGFloat = tempView.frame.size.width
width = ((UIScreen.mainScreen().bounds.width)/320)*width
let size: CGSize = tempView.text!.boundingRectWithSize(CGSizeMake(width, 2000), options:NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: tempView.font], context: context).size as CGSize
return size.height
}