swift-标签(Label)

2019-04-14  本文已影响0人  寒丶酥

1.Label的创建

let label:UILabel = UILabel(frame(CGRect(X:100,y:100,width:100,height:100)))
self.view.addSubview(label)

2.Label的基础应用

//设置label的标题
label.text = "aimi"
//设置label的文本颜色
label.textColor = UIColor.red
//设置label的对齐方式
label.textAlignment = .center
//设置label的阴影颜色
label.shadowColor = UIColor.gray
//设置label的阴影大小
label.shadowOffset = CGSize(width:1.5,height:1.5)
//设置label中字体大小
label.font = UIFont(named:"Zapfino",size:20)
//设置label的省略方式
/*
    //省略尾部文字  .byTruncatingTail
    //省略头部文字  .byTruncatingHead
    //省略中间的文字  .byTruncatingMiddle
    //直接将多余的部分删除  .byClipping
    //自动换行(按词拆分) .byWordWrapping
    //自动换行(按字符拆分) .byCharWrapping
*/
label.lineBreakMode = .byTruncatingHead
//设置label的行数
label.numberOfLines = 2
///设置label的高亮和文字高亮颜色
label.isHighLighted = true
label.isHightedTextColor = UIColor.purpule

3.富文本设置

 //富文本设置
 let attributeString = NSMutableAttributedString(string:"仙女就是 不讲道理的")
 //从文本0开始6个字符字体HelveticaNeue-Bold,16号
 attributeString.addAttribute(NSAttributedString.Key.font, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!, range:   NSMakeRange(0,6))
 //设置字体颜色
 attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value:UIColor.blue,range:NSMakeRange(0, 3))
 //设置文字背景颜色
 attributeString.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.green,range: NSMakeRange(3,3))
 label.attributedText = attributeString
上一篇 下一篇

猜你喜欢

热点阅读