How to center UILabel in Swift?(如何在 Swift 中将 UILabel 居中?)
本文介绍了如何在 Swift 中将 UILabel 居中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使某些文本居中,但它似乎不起作用.
I'm trying to center some text but I it doesn't seem to be working.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let title = UILabel()
title.text = "Some Sentence"
title.numberOfLines = 0
title.frame = CGRectMake(self.view.bounds.size.width/2,50,self.view.bounds.size.width, self.view.bounds.size.height) // x , y, width , height
title.textAlignment = .Center
title.sizeToFit()
title.backgroundColor = UIColor.redColor()
self.view.addSubview(title)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我正在使用的代码,但这是我得到的:
That is the code that I'm using but this is what I get:
它不在屏幕的中心.谁能告诉我我做错了什么?
It's not center to the screen. Can someone tell me what am I doing wrong?
推荐答案
要使 UILabel 居中,只需添加这一行
To center a UILabel just add this row
x 和 y:
title.center = self.view.center
x:
title.center.x = self.view.center.x
y:
title.center.y = self.view.center.y
这篇关于如何在 Swift 中将 UILabel 居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在 Swift 中将 UILabel 居中?


猜你喜欢
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01