ios - 文本在容器中无法正确显示(swift 3.0)

标签 ios swift xcode nslayoutconstraint

我有一个 UIlabel,当没有足够的文本填充 View 框架时,我想调整文本对齐方式。我提供了两个屏幕截图,以更好地说明我到底想要完成什么。

First Image

注意文本是如何位于中间的。

Second Image

请注意文本现在如何从左上角开始

有什么建议吗?

最佳答案

然后设置 UILabel 约束,将其锚定到容器的顶部和左侧..

 yourLabel.topAnchor.constraintsEqual(yourContainer.topanchor).isActive = true

           yourLabel.leftAnchor.constraintsEqual(yourContainer.leftAnchor).isActive = true

不要忘记设置

     yourLabel.translatesAutoresizingMasks = false

设置约束之前

关于ios - 文本在容器中无法正确显示(swift 3.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43430587/

相关文章:

c++ - 具有已定义宏的 iOS 预处理条件导致 "token is not valid"

ios - 无法使用 removeFromParent : Swift 删除点击的 spriteNode

ios - 如何使用 ScrollView 单击列表部分内的项目以导航到详细信息页面 SwiftUI

ios - customTableViewCell 不工作

SwiftUI - 按钮 - 如何将函数(带参数)请求从子传递给父

ios - "Use of unresolved identifier ' Realm '"

ios - 应用程序卡在 LaunchScreen 上且没有错误消息

iphone - 奇怪的情况给出错误 : "The application bundle does not contain a valid identifier."

ios - 使用 Firebase 匿名登录影响 Facebook 登录

swift - 位置管理器在 gmsMapView swift 4.2 中显示错误位置