import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//创建Label并设置位置
let label = UILabel(frame: CGRect(x: 150,y:250 , width: 100, height: 100))
//设置文本内容
label.text = "文本内容"
//添加到视图中
self.view.addSubview(label)
//设置文字颜色及背景颜色
label.textColor = UIColor.green
label.backgroundColor = UIColor.orange
//文字对其方式
label.textAlignment = .right //右对齐
//文字阴影设置
label.shadowColor = UIColor.gray//灰色阴影
label.shadowOffset = CGSize(width: 2,height: 2)
//字体及大小设置
label.font = UIFont.systemFont(ofSize: 18)//自定义字体
label.font = UIFont.systemFont(ofSize:17)//系统字体
//内容文字过长的省略方式
label.lineBreakMode = .byClipping//超出部分裁剪
label.lineBreakMode = .byCharWrapping//以字母结尾换行
label.lineBreakMode = .byTruncatingHead//将头部超出部分省略号代替
label.lineBreakMode = .byTruncatingTail//将尾部超出部分省略号代替
label.lineBreakMode = .byWordWrapping//以单词结尾换行
label.lineBreakMode = .byTruncatingMiddle//将中间部分超出部分省略号代替
//文字大小自适应标签高度
label.adjustsFontSizeToFitWidth = true//当文字超出标签宽度时,自动调整文字大小,使其不被截断
//设置显示行数
label.numberOfLines = 2//当numberOfLine为0时行数不受限制
//设置文本高亮
label.isHighlighted = true
//设置文本高亮颜色
label.highlightedTextColor = UIColor.red
//富文本设置
let attributedString = NSMutableAttributedString(string: "for Development")
attributedString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "Farah", size: 18)!,range: NSRange(location: 0, length: 5))
attributedString.addAttribute(NSAttributedString.Key.strokeWidth, value: 3, range: NSMakeRange(6, 9))
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range:NSRange(location: 16, length: 3))
label.attributedText = attributedString
}
}
iOS - UILabel