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

标签 ios iphone xcode swift uilabel

长期读者,第一次海报。我到处寻找解决方案,但尚未找到有效的答案。

好的问题:我有一个 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.
}
*/

最佳答案

好的。执行以下操作:

  1. 从标签向左拖动到 View ,松开并选择Leading Space to Container Margin
  2. 从标签向右拖动到 View ,松开并选择Trailing Space to Container Margin
  3. 从标签向上拖动到 View ,松开并选择Top Space to Top Layout Guide

之后转到标签的尺寸检查器并确保所有约束都具有标准常量。

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

如果您有任何问题,请随时提出:)

关于ios - 垂直对齐 UILabel 中的文本 (xcode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30050825/

相关文章:

html - Safari 中未应用最大高度

ios - UItableviewcell "cell-identifier"内存管理

objective-c - Swift - 使用未解析的标识符(Objective-C 类)

ios - 我正在尝试在 XCode 中修复我的引脚,但在 XCode8.2.1 中没有获取更新帧按钮

ios - Compute Kernel Metal - 如何检索结果和调试?

iphone - iOS 5.1 : -[UIColor colorWithPatternImage:] background color draws solid black

ios - clang : error: linker command failed with exit code 1 iOS9

ios - Xcode 4.1 fatal error : stdlib modified since the precompiled header was built

ios - Objective-C:强制一个类成为其他类的子类

iphone - 苹果新验证 Controller 的异步部分是必须的吗?