本文将演示标签控件的基础用法,
在项目导航区, 打开视图控制器的代码文件[ViewController.swift]
- import UIKit
- class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- // 创建一个位置在 (20,100), 尺寸为(280,80) 的显示区域
- let rect = CGRect(x: 20, y: 100, width: 280, height: 80)
- // 设置标签对象的显示区域
- let label = UILabel(frame: rect)
- // 设置标签对象在默认情况下, 显示的文字内容
- label.text = "Hello, Xcode and Swift!"
- // 创建一个指定大小的字体对象
- let font = UIFont(name: "Arial", size: 24)
- // 设置标签对象文字的字体和大小
- label.font = font
- // 设置标签文字的字体颜色为浅灰色
- label.shadowColor = UIColor.lightGray
- // 设置标签文字的阴影, 在横向和纵向上的偏移距离
- label.shadowOffset = CGSize(width: 2, height: 2)
- // 设置标签文字的对齐方式为右对齐
- label.textAlignment = NSTextAlignment.right
- // 设置标签文字的颜色为紫色
- label.textColor = UIColor.purple
- // 设置标签的背景颜色为黄色
- label.backgroundColor = UIColor.yellow
- // 将标签对象, 添加到当前视图控制器的根视图
- self.view.addSubview(label)
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- }
[Xcode10 实际操作]四, 常用控件 -(3)UILabel 文本标签的使用
来源: http://www.bubuko.com/infodetail-2860039.html