我尝试了很多解决方案但没有得到正确答案。
这是我的代码:
self.AhadeesView = UILabel()
self.AhadeesView.translatesAutoresizingMaskIntoConstraints = false
self.AhadeesView.backgroundColor = UIColor.orange
self.AhadeesView.numberOfLines = 0
self.AhadeesView.text = NSLocalizedString("TitleAhadees", comment: "Title Ahadees of the App")
self.AhadeesView.textAlignment = .center
self.AhadeesView.font = UIFont(name:"Jameel-Noori-Nastaleeq",size:25)
self.AhadeesView.lineBreakMode = NSLineBreakMode.byWordWrapping
// self.AhadeesView.sizeToFit()
containerView1.addSubview(AhadeesView)
答案 0 :(得分:-1)
好像你错过了框架来展示标签。
self.AhadeesView = UILabel(frame: [your frame value])
可以通过
创建框架let frame = CGRect(x: 0, y: 0, width: 320, height: [height_of_String])
[height_of_String]可以通过以下扩展名计算
您可以使用以下扩展方法
计算字符串的宽度和高度extension String {
//To calculate the height u need to pass the width of the label and the required font size.
func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return ceil(boundingBox.height)
}
func width(withConstraintedHeight height: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return ceil(boundingBox.width)
}
}