首页 > 代码库 > Swift_UILabel

Swift_UILabel

一.初始化

        // 初始化UIlabel,并设置frame
        //let labelOne = UILabel.init(frame: CGRect.init(x: 10, y: 20, width: 30, height: 40))
        
        // 初始化UIlabel
        let labelOne = UILabel()
        // 设置label的frame
        labelOne.frame = CGRect.init(x: 10, y: 20, width: (kScreenWidth - 20), height: 60)

二.设置UILabel的常用属性

        // 设置背景颜色和字体颜色
        labelOne.backgroundColor = UIColor.orange
        labelOne.textColor = UIColor.red
        
        labelOne.font = UIFont.systemFont(ofSize: 14)
        
        labelOne.text = "我是一个简单的Label"
        
        // 获取label的文字
        let labelString = labelOne.text!
        print(labelString)

        // 文本的对齐方式
        labelOne.textAlignment = NSTextAlignment.center
        
        labelOne.numberOfLines = 0

三.富文本

        // 富文本
        //let attrString = NSMutableAttributedString(string: "I am a attributed string")
        let attrString = NSMutableAttributedString(string: labelOne.text!)
        
        // 设置字体大小
        attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 18), range: NSMakeRange(0, 1))
        
        // 设置颜色
        attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellow, range: NSMakeRange(0, 2))
        
        // 设置下划线
        attrString.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0,4))

        labelOne.attributedText = attrString

四.所有代码

import UIKit


let kScreenWidth = UIScreen.main.bounds.size.width

class ViewController: UIViewController {

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.addLabelOne()
    }
    
    
    func addLabelOne() {
        
        self.view .addSubview(labelOne)
    }
    
    
    // 懒加载一个label
    lazy var labelOne: UILabel = {

        // 初始化UIlabel,并设置frame
        //let labelOne = UILabel.init(frame: CGRect.init(x: 10, y: 20, width: 30, height: 40))
        
        // 初始化UIlabel
        let labelOne = UILabel()
        // 设置label的frame
        labelOne.frame = CGRect.init(x: 10, y: 20, width: (kScreenWidth - 20), height: 60)
        
        // 设置背景颜色和字体颜色
        labelOne.backgroundColor = UIColor.orange
        labelOne.textColor = UIColor.red
        
        labelOne.font = UIFont.systemFont(ofSize: 14)
        
        labelOne.text = "我是一个简单的Label"
        
        // 获取label的文字
        let labelString = labelOne.text!
        print(labelString)

        // 文本的对齐方式
        labelOne.textAlignment = NSTextAlignment.center
        
        labelOne.numberOfLines = 0
        
        
        // 富文本
        //let attrString = NSMutableAttributedString(string: "I am a attributed string")
        let attrString = NSMutableAttributedString(string: labelOne.text!)
        
        // 设置字体大小
        attrString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 18), range: NSMakeRange(0, 1))
        
        // 设置颜色
        attrString.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellow, range: NSMakeRange(0, 2))
        
        // 设置下划线
        attrString.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0,4))

        labelOne.attributedText = attrString
        
        return labelOne
    } ()
}

 

Swift_UILabel