ios 如何在 Swift 中以编程方式为 UILabel 提供动态高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31228831/
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
How to give dynamic height to UILabel programmatically in Swift?
提问by Sandesh Sardar
I have taken UIlabel which are generated dynamically using for loop, each type diff text is assign in label, I want to give UILabel size dynamically depending on text.
我采用了使用 for 循环动态生成的 UIlabel,在标签中分配了每种类型的差异文本,我想根据文本动态地赋予 UILabel 大小。
Is there any easy solution in to do that in Swift?
有什么简单的解决方案可以在 Swift 中做到这一点吗?
回答by user3182143
let label:UILabel = UILabel(frame: CGRectMake(x, y, width, height))
label.numberOfLines = 4
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
let font = UIFont(name: "Helvetica", size: 20.0)
label.font = font
label.text = "Whatever you want the text enter here"
label.sizeToFit()
If you want to set numberOfLines according to the content of text,give your maximum lines.That is very important here.
如果你想根据文本的内容设置numberOfLines,给出你的最大行数。这在这里很重要。
回答by bluenowhere
get a label height depending on it's text, font, and width you assign to it:
根据您分配给它的文本、字体和宽度获取标签高度:
func rectForText(text: String, font: UIFont, maxSize: CGSize) -> CGSize {
let attrString = NSAttributedString.init(string: text, attributes: [NSFontAttributeName:font])
let rect = attrString.boundingRectWithSize(maxSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)
let size = CGSizeMake(rect.size.width, rect.size.height)
return size
}
let labelSize = rectForText("your text here", font: UIFont.systemFontOfSize(your font), maxSize: CGSizeMake(your label width,999))
let labelHeight = labelSize.height //here it is!
回答by Piyush Sanepara
myLabel.text = "Your Label Text Here"
myLabel.textAlignment = .Natural
myLabel.numberOfLines = 0
myLabel.sizeToFit()
myLabel.frame = CGRectMake(myLabel.frame.origin.x, myLabel.frame.origin.y, 280, myLabel.frame.height)
回答by D.l.Prasad
let label:UILabel = UILabel()
label.textColor=UIColor.black
label.font = UIFont(name: "Halvetica", size: 17)
label.numberOfLines = 1
label.text = item.name
label.sizeToFit()
label.frame = CGRect(x: 5, y: imageView.frame.height+10, width: 50, height:label.frame.height)
回答by NSDeveloper
let label = UILabel()
label.backgroundColor = UIColor.greenColor()
label.text = "Hello,world.\n Just a test."
let font = UIFont.systemFontOfSize(17.0)
label.font = font
label.numberOfLines = 0;
let text = label.text! as NSString
let size = text.sizeWithAttributes([NSFontAttributeName:font])
label.frame = CGRectMake(0, 0, size.width, size.height)
You can use Auto Layout in code. See Auto Layout Guide
您可以在代码中使用自动布局。请参阅自动布局指南
回答by NSDeveloper
The Swift 4.1extension method to calculate label height:
在雨燕4.1的扩展方法来计算标签高度:
extension UILabel {
func heightForLabel(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
}
}
Refer: Adjust UILabel height to text
参考:将UILabel 高度调整为文本
回答by Tina Detroja
Create Extension to calculate the height of label following method return height of the label
Create Extension 计算标签高度,方法返回标签高度
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: 60)
回答by Muhammad Umer Farooq
NSString(string: "hello this is a string").boundingRect(
with: CGSize(width: width, height: .greatestFiniteMagnitude),
options: .usesLineFragmentOrigin,
attributes: [.font: self],
context: nil).size
Use this method to get determine the size for you string. Put maximum height and maximum width in CGSize(widhth,height)
and it will return CGSize
object containing height and width. Use it according with your scenario
使用此方法来确定字符串的大小。输入最大高度和最大宽度,CGSize(widhth,height)
它将返回CGSize
包含高度和宽度的对象。根据您的场景使用它