1

我已经尝试了一切来在 Swift 中自动调整我的 UILabel 大小。当它是一行时,我可以自动调整文本的大小,但是当它是两行时,它不再起作用。UILabel 中的文本通常大约每 10 秒发生一次更改。我试过的代码是:

let direction = UILabel(frame: CGRect(x: 95, y: 10, width: screenWidth - 95, height:100))
direction.numberOfLines = 0
direction.adjustsFontSizeToFitWidth = true
direction.lineBreakMode = .byWordWrapping
direction.textAlignment = .center
direction.minimumScaleFactor = 0.2
direction.font = UIFont.boldSystemFont(ofSize: 40)
view.addSubview(direction)
direction.text = "Ffafdafafdfa fafda  dfafaf afa"

func updateDirection(update: String){
    direction.text = update
} 

原始文本“Ffafdafafdfa fafda dfafaf afa”将自动调整大小,但是当调用 updateDirection 时,字体大小不会从 40 更改。我还尝试将行数设置为 2 并删除 .byWordWrapping。如何让我的 UILabel 自动调整大小?

4

5 回答 5

2

下面的代码将保留frame sizeandadjust the font sizedirection label content

let backgroundView = UIView(frame: CGRect(x: 5, y: UINavigationController().navigationBar.frame.height + UIApplication.shared.statusBarFrame.height, width: UIScreen.main.bounds.width - 10, height: UIScreen.main.bounds.width - 100))
let direction = UILabel()

 override func viewDidLoad() {
    super.viewDidLoad()

 direction.backgroundColor = UIColor.green
 direction.numberOfLines = 0
 direction.textAlignment = .center
 direction.font = UIFont.boldSystemFont(ofSize: 40)
 direction.adjustsFontForContentSizeCategory = true
 direction.adjustsFontSizeToFitWidth = true
 direction.text = "This is some multiline label with a background colour" // Set or Initiate random function for your array here.

 backgroundView.backgroundColor = UIColor.red
 view.addSubview(backgroundView)
 backgroundView.addSubview(direction)

 Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(random), userInfo: nil, repeats: true)  

 direction.translatesAutoresizingMaskIntoConstraints = false

 NSLayoutConstraint(item: direction,
                       attribute: .leading,
                       relatedBy: .equal,
                       toItem: backgroundView,
                       attribute: .leadingMargin,
                       multiplier: 1.0,
                       constant: 0.0).isActive = true

NSLayoutConstraint(item: direction,
                       attribute: .trailing,
                       relatedBy: .equal,
                       toItem: backgroundView,
                       attribute: .trailingMargin,
                       multiplier: 1.0,
                       constant: 0.0).isActive = true


NSLayoutConstraint(item: direction,
                       attribute: .top,
                       relatedBy: .equal,
                       toItem: backgroundView,
                       attribute: .topMargin,
                       multiplier: 1.0,
                       constant: 0.0).isActive = true

NSLayoutConstraint(item: direction,
                       attribute: .bottom,
                       relatedBy: .equal,
                       toItem: backgroundView,
                       attribute: .bottomMargin,
                       multiplier: 1.0,
                       constant: 0.0).isActive = true


}

func random(sender: Timer) {

    //Place your random func code here.     
}

输出:

在此处输入图像描述

于 2017-05-18T13:52:09.193 回答
0

我会试试这个代码它是使用完整的

首先创建一个函数

func calchight(strin:String) -> CGFloat
   {
       let label =  UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CGFloat.greatestFiniteMagnitude))
       label.numberOfLines = 0
        label.text = strin
       label.sizeToFit()
        return label.frame.height + 2
   }

然后在您想要调整标签大小时在正确的位置调用此函数

/---------------

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    { 
        let heit = self.calchight(strin: StringObject)

        return (heit)
    }

/--------------------------

此代码很有用,不需要自动布局容器

于 2017-05-18T04:28:20.177 回答
0

试试这个没有约束:

    let direction = UILabel(frame: CGRect(x: 95, y: 10, width: 375 - 95, height:100))
    direction.numberOfLines = 0
    direction.adjustsFontSizeToFitWidth = true
    direction.textAlignment = .center
    direction.minimumScaleFactor = 0.2
    direction.font = UIFont.boldSystemFont(ofSize: 40)
    view.addSubview(direction)

    direction.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard"
于 2017-05-18T03:59:00.913 回答
0

UILabel将认为其内容大小为一行文本,即使numberOfLines设置为非零值,除非preferredMaxLayoutWidth也设置。

换句话说,它无法预测没有 的文本如何换行preferredMaxLayoutWidth,因此它不打算这样做。似乎在自动布局过程中,aUILabel不知道如何平衡其外部布局约束设置的宽度与文本换行之间的优先级。

所以它不会在垂直维度上施加任何额外的压力(超过一条线的高度),即使它的 Content Compression Resistance 高于其他视图,因为它会假设通过在水平轴上施加压力,它的内容将显示。

因此解决方案是设置,以便在包装文本后preferredMaxLayoutWidth能够确定内容的高度,然后使用该内容大小与其他视图协商布局。

您可能希望避免硬编码UILabel. 假设它的容器的宽度不会根据自动布局而改变,那么使用它应该是安全的。在这种情况下,我在一个UITableViewCell或中UICollectionViewCell,并且“标题标签”跨越了单元格的整个宽度。

override func layoutSubviews() {
    // Tell the label it should wrap text at the same width as this cell's contentView.
    titleLabel.preferredMaxLayoutWidth = contentView.frame.width

    super.layoutSubviews()
}
于 2021-04-26T14:41:12.150 回答
-1

在将文本分配给标签后尝试使用函数 sizetofit()。喜欢:

direction.sizeToFit()
于 2017-05-18T04:34:04.723 回答