【发布时间】:2020-09-11 11:50:43
【问题描述】:
所以我在 UIView 中有一个普通的 UILabel,里面有一些单词。它是一个 3 行标签,具有 .adjustsFontSizeToFitWidth = true
大多数情况下它可以正常工作,但在某些情况下会出现连字符,如下所示:
但在其他字母组合中,就像我想要/需要的那样,它不会连字符,只是调整大小:
我需要的是强制 UILabel 不要中断/连字符,而是更喜欢调整大小。换行模式在这里没有多大作用。 有没有我想念的方法来关闭连字符或断字? 我还试图找出如何检查单词是否被破坏但也无法找到它,因此我可以调整行数。
编辑:这里是操场代码看看:
import Foundation
import UIKit
import PlaygroundSupport
public class Card: UIView {
var label2: UILabel!
public init(frame: CGRect, withText: String) {
super.init(frame: frame)
self.backgroundColor = UIColor(red: 0.313, green: 0.89, blue: 0.76, alpha: 1.000)
label2 = UILabel(frame: CGRect(x: bounds.width / 2 - 115, y: (bounds.height/2 - 90), width: 230, height: 180))
label2.textAlignment = .center
label2.text = "stepper; rotory; revolutionizing"
label2.font = UIFont.boldSystemFont(ofSize: 60)
label2.textColor = UIColor.white
label2.adjustsFontSizeToFitWidth = true
label2.translatesAutoresizingMaskIntoConstraints = false
label2.numberOfLines = 0
self.addSubview(label2)
// label2.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
// label2.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -50).isActive = true
// label2.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 50).isActive = true
// label2.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let card = Card(frame: CGRect(x: 10, y: 30, width: 280, height: 200), withText: "????")
let page = PlaygroundPage.current
let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 500))
view.backgroundColor = UIColor.white
view.addSubview(card)
page.liveView = view
【问题讨论】: