垂直对齐UILabel(xcode)中的文本

时间:2015-05-05 10:44:58

标签: ios iphone xcode swift uilabel

长期读者,第一次海报。我已经搜遍了所有的解决方案,但我还没有找到一个有效的答案。

确定问题:我有一个UILabel可以动态分配给它的文本。如果我调整标签大小以覆盖整个视图控制器,则文本卡在中间。我找到了一堆解决方案,说要添加以下代码:

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.
}
*/

2 个答案:

答案 0 :(得分:2)

确定。执行以下操作:

  1. 将LEFT从标签拖至视图,发布并选择领先空间到容器边距
  2. 将RIGHT从标签拖到视图,发布并选择尾随空格到容器边距
  3. 将UPWARDS从标签拖到视图,然后发布并选择顶部空间到顶部布局指南
  4. 之后转到标签的尺寸检查器并确保约束都具有标准的常量。

    最后但并非最不重要的是转到标签的属性检查器,并确保 Lines 属性设置为0。

    随时问您是否有任何问题:)

答案 1 :(得分:0)

您可以执行以下操作来启用支持多行的UILabel

 lbldetails.numberOfLines = 0
 lbldetails.preferredMaxLayoutWidth = lbldetails.frame.size.width

在这里,我认为您的UILable支持动态高度,因为它的文字只是关于不显示多行的问题。

请向UILabel提供以下约束。 enter image description here

希望这对你有所帮助。

相关问题