/* 1. 创建UILabel */
let label = UILabel()
label.text = "hello world"
label.center = view.center
label.bounds = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 200, height: 40))
view.addSubview(label)
/* 2. 背景颜色和文字颜色设置, 字体大小设置 */
label.textColor = UIColor.red
label.backgroundColor = UIColor.yellow
label.font = UIFont.systemFont(ofSize: 20)
/* 3. 对齐方式设置 */
label.textAlignment = .center
/* 4. 文字阴影设置 */
label.shadowColor = UIColor.gray
label.shadowOffset = CGSize.init(width: 1.5, height: 1.5)
/* 5. 设置文本过长时的省略方式 truncating 截断*/
label.lineBreakMode = .byTruncatingTail /* 隐藏尾部并显示省略号 */
label.lineBreakMode = .byTruncatingMiddle/* 截断中间部分并显示省略号 */
label.lineBreakMode = .byTruncatingHead /* 隐藏头部并显示省略号 */
label.lineBreakMode = .byClipping /* 截取多余部分也不显示省略号 */
/* 6. 文字大小自适应标签宽度*/
label.adjustsFontSizeToFitWidth = true /* 当文本超出标签宽度时,自动调整文字大小,使其不会被截断 */
/* 7. label显示多行 */
label.numberOfLines = 2 /* 设置0表示没有行数限制 */
label.preferredMaxLayoutWidth = UIScreen.main.bounds.size.width /*自动布局情形下,需要设置preferredMaxLayoutWidth才能多行显示 */
/* 8. 设置文本高亮 */
label.isHighlighted = true
label.highlightedTextColor = UIColor.green
/* 9. 富文本 */
/* 1.富文本字符串 */
let attributeString = NSMutableAttributedString.init(string: "welcome hello world")
/* 2.富文本的字体颜色以及范围设置 */
attributeString.addAttributes([NSForegroundColorAttributeName:UIColor.purple], range: NSRange.init(location: 0, length: attributeString.length/2))
/* 3.赋值给label的attributedText */
label.attributedText = attributeString