长期读者,第一次海报。我到处寻找解决方案,但尚未找到有效的答案。
好的问题:我有一个 UILabel,它可以动态分配给它的文本。如果我调整标签的大小以覆盖整个 View Controller ,文本就会卡在中间。我找到了一堆解决方案,说要添加以下代码:
detailsLabel.numberOfLines = 0;
detailsLabel.sizeToFit()
然而这并没有做任何事情。
有人可以看看我的代码并提供建议吗?
import UIKit
class DetailsViewController: UIViewController {
@IBOutlet weak var detailsLabel: UILabel!
@IBOutlet weak var lbldetails: UILabel!
var selectedBeach : Beach?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
detailsLabel.text = selectedBeach?.greaterDetails
lbldetails.text = (selectedBeach?.beachName ?? "") + " Details"
detailsLabel.numberOfLines = 0;
[detailsLabel .sizeToFit()]
}
@IBAction func dismiss(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
最佳答案
好的。执行以下操作:
- 从标签向左拖动到 View ,松开并选择Leading Space to Container Margin
- 从标签向右拖动到 View ,松开并选择Trailing Space to Container Margin
- 从标签向上拖动到 View ,松开并选择Top Space to Top Layout Guide
之后转到标签的尺寸检查器并确保所有约束都具有标准常量。
最后但同样重要的是转到标签的属性检查器并确保 Lines 属性设置为 0。
如果您有任何问题,请随时提出:)
关于ios - 垂直对齐 UILabel 中的文本 (xcode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30050825/