简体   繁体   English

如何以编程方式快速创建动态标签尺寸?

[英]How to create dynamic label size programmatically in swift?

I'm trying to create a label programmatically in Swift but the issue I'm having is that based on the data model the amount of text can change thereby changing the size of the label. 我正在尝试在Swift中以编程方式创建标签,但是我遇到的问题是,根据数据模型,文本量可能会发生变化,从而改变标签的大小。 Typically I would create the label like this before knowing the text: 通常,在了解文本之前,我会像这样创建标签:

headerLabel.frame = CGRect(x: 0, y: 0, width: screenSize.width/2.5, height: screenSize.height/45)
headerLabel.center = CGPoint(x: screenSize.width/2, y: 245)

But in this case the text can be any amount ranging from a line to a paragraph so hard coding the height won't work. 但是在这种情况下,文本可以是任意大小,从一行到一个段落,因此很难对高度进行编码。 How to create the label so it would accommodate any amount of text? 如何创建标签以容纳任意数量的文本?

You can access label intrinsic property after you have given the text to label then you can give the frame. 在给文本加上标签之后,您就可以访问标签的固有属性,然后就可以给出框架了。

Swift 3: 斯威夫特3:

let label = UILabel()
label.text = "Your text here"
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 14)
label.frame = CGRect(x:0,y:0,width:label.intrinsicContentSize.width,height:label.intrinsicContentSize.height)

you can check some conditions based on the intrinsicContentSize. 您可以根据internalContentSize检查某些条件。

Hope it helps. 希望能帮助到你。

This label has a defined width but not a defined height. 该标签具有定义的宽度,但没有定义的高度。 The height is determined by the amount of text in the label. 高度由标签中的文本量决定。 If you removed the width, the label would not break into lines, which is not what you want. 如果删除宽度,标签将不会分成几行,这不是您想要的。 The sizeToFit() method called at the end sets the height of the label after it knows how many lines of text it needs to break into based on the label's text. 最后sizeToFit()方法在知道基于标签的文本需要分成多少行文本之后,设置标签的高度。 If this isn't what you wanted, let me know. 如果这不是您想要的,请告诉我。

let label = UILabel()
label.text = "scones"
label.numberOfLines = 0 // 0 = as many lines as the label needs
label.frame.origin.x = 32
label.frame.origin.y = 32
label.frame.size.width = view.bounds.width - 64
label.font = UIFont.displayHeavy(size: 17) // my UIFont extension
label.textColor = UIColor.black
label.sizeToFit()
view.addSubview(label)

You can use the following to calculate the string height and width and then set them: 您可以使用以下内容来计算字符串的高度和宽度,然后进行设置:

import Foundation
import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let width = "Hello World".stringWidth // 74.6
        let height = "Hello World".stringHeight // 16.7

        let headerLabel = UILabel()
        headerLabel.frame = CGRect(x: 0, y: 0, width: width, height: height)
        headerLabel.center = CGPoint(x: screenSize.width/2, y: 245)
    }
}

extension String {
    var stringWidth: CGFloat {
        let constraintRect = CGSize(width: UIScreen.main.bounds.width, height: .greatestFiniteMagnitude)
        let boundingBox = self.trimmingCharacters(in: .whitespacesAndNewlines).boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)], context: nil)
        return boundingBox.width
    }

    var stringHeight: CGFloat {
        let constraintRect = CGSize(width: UIScreen.main.bounds.width, height: .greatestFiniteMagnitude)
        let boundingBox = self.trimmingCharacters(in: .whitespacesAndNewlines).boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)], context: nil)
        return boundingBox.height
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Swift 2 中以编程方式在图像视图上显示动态标签 - How to show dynamic label on image view programmatically in Swift 2 如何在Swift中创建动态大小的UITableViewCell? - How to create a dynamic size UITableViewCell in Swift? Swift:如何根据iPhone(例如iPhone SE,iPhone 8)以编程方式更改标签/视图尺寸? - Swift: How to programmatically change a label/view size depending on iPhone (eg. iPhone SE, iPhone 8)? Swift:如何找出容器视图的大小并以编程方式在中心放置标签? - Swift: How to find out the size of a container view and position a label in the center programmatically? 如何在swift 4中以编程方式更改标签颜色 - How to change label colors programmatically in swift 4 标签NSMutableAttributedString以编程方式迅速4 - Label NSMutableAttributedString programmatically swift 4 Swift 3:无法以编程方式在Collectionview头上正确创建标签? - Swift 3: Cant programmatically create label on Collectionview header properly? 如何在 Swift 中以编程方式更改 UICollectionViewCell 大小? - How to change UICollectionViewCell size programmatically in Swift? 如何在Swift中在视频上叠加动态标签? - How to overlay a dynamic label on a video in Swift? 如何在 Swift 中以编程方式为 UILabel 提供动态高度? - How to give dynamic height to UILabel programmatically in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM