swift - 编辑-我怎样才能让 UIlabel 进入容器的中间?
问题描述
我怎样才能让 UIlabel 进入容器的中间?并且该文本和视图同样消失并进入下一个视图。
编辑:这段代码现在对我来说很好
class TestViewController: UIViewController {
let container = UIView()
let redSquare = UIView()
let blueSquare = UIView()
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
self.label.textColor = UIColor.orange
self.label.font = label.font.withSize(25)
self.label.numberOfLines = 0
self.label.center = self.blueSquare.center
self.label.textAlignment = NSTextAlignment.center
self.label.text =
"sosdihfiosdfhsdhfdisfhsdfhdsoifhsdofhsdifhdsofhdsofhsdohdsfdosdohdfh"
self.blueSquare.addSubview(label)
self.label.frame = CGRect(x: 45, y: 120, width: 300, height: 100)
self.container.frame = CGRect(x: 7, y: 200, width: 400, height: 500)
self.view.addSubview(container)
self.redSquare.frame = CGRect(x: 0, y: 0, width: 400, height: 500)
self.blueSquare.frame = redSquare.frame
self.redSquare.backgroundColor = UIColor.darkGray
self.blueSquare.backgroundColor = UIColor.darkGray
self.container.addSubview(self.redSquare)
}
解决方案
定义两个标签,如:
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
let label2 = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 30))
然后在将 redSquare 添加到容器之前在 viewDidLoad() 中修复一些标签的属性并将标签添加到蓝色和红色方块:
label.center = self.redSquare.center
label.textAlignment = NSTextAlignment.center
label.text = "same text"
label2.textAlignment = NSTextAlignment.center
label2.center = self.blueSquare.center
label2.text = "same text"
self.blueSquare.addSubview(label2)
self.redSquare.addSubview(label)
推荐阅读
- javascript - Video.js 控件显示奇怪,为什么?
- reactjs - stripe.redirectToCheckout 参数:价格不是可接受的参数
- java - 在 nlog(n) 时间内实现递归洗牌数组方法的问题
- postgresql - Postgresql 索引缓存
- javascript - 散景:补丁上的点击回调问题
- javascript - Word Javascript 插件中缺少打印功能的解决方法?
- python - CommandHandler 启动后 Telegram 机器人不应答
- graph - 如何使用 Neo4j“叠加”关系?
- python - python matplotlib中直方图的zoomed_inset_axes不起作用
- javascript - 如何更正:SyntaxError:未找到命名导出“mongo”