首页 > 技术文章 > swift 实践- 03 -- UILabel

dingzhijie 2017-06-09 13:38 原文

class ViewController: UIViewController {

 

    override func viewDidLoad() {

        super.viewDidLoad()

    

        // 标签

        let label: UILabel = UILabel(frame: CGRect(x: 100, y: 250, width: 200, height: 100))

        label.text = "Hello world!"

        self.view.addSubview(label)

        label.textAlignment = .center

        

        // 设置文本高亮

        label.isHighlighted = true

        

        // 设置文本高亮颜色

        label.highlightedTextColor = UIColor.black

        

        // 富文本设置

        let attributeString = NSMutableAttributedString(string:"welcome to china")

        

        // 改变指定位置的字符串的文字 font

        attributeString.addAttribute(NSFontAttributeName,

                                      value: UIFont.systemFont(ofSize: 20),

                                      range: NSMakeRange(0, 4))

        

        // 设置指定位置的字体颜色

        attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue,

                                     range: NSMakeRange(0, 3))

        

        

        // 设置指定位置的文字背景颜色

        attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.green,

                                     range: NSMakeRange(3,3))

        

        

        label.attributedText = attributeString

        

    }

    

 

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

 

 

}

推荐阅读