UILabel

let label=<span style="background-color: rgb(160, 255, 255);">UILabel</span>(frame: CGRectMake(30, 120, 300, 36))
        label.text="测试<span style="background-color: rgb(255, 102, 255);">Swift UILabel</span>"
        self.view .addSubview(label)
        
        //<a target=_blank name="baidusnap0" style="color: rgb(255, 153, 0);"></a><span style="background-color: rgb(255, 255, 102);">设置</span>背景色
        label.backgroundColor=UIColor.greenColor()
        
        //<span style="background-color: rgb(255, 255, 102);">设置</span>文字颜色
        label.textColor=UIColor.redColor()




文字居中

let label1=<span style="background-color: rgb(160, 255, 255);">UILabel</span>(frame: CGRectMake(30, 170, 300, 36))
        let label2=<span style="background-color: rgb(160, 255, 255);">UILabel</span>(frame: CGRectMake(30, 220, 300, 36))
        let label3=<span style="background-color: rgb(160, 255, 255);">UILabel</span>(frame: CGRectMake(30, 270, 300, 36))
        label1.text="测试<span style="background-color: rgb(255, 102, 255);">Swift UILabel</span>1"
        label2.text="测试<span style="background-color: rgb(255, 102, 255);">Swift UILabel</span>2"
        label3.text="测试<span style="background-color: rgb(255, 102, 255);">Swift UILabel</span>3"
        label1.backgroundColor=UIColor.grayColor()
        label2.backgroundColor=UIColor.grayColor()
        label3.backgroundColor=UIColor.grayColor()
        self.view.addSubview(label1)
        self.view.addSubview(label2)
        self.view.addSubview(label3)
        //NSTextAlignment 是一个枚举,<span style="background-color: rgb(160, 255, 255);">uilabel</span>默认文字是居左的
        label1.textAlignment=NSTextAlignment.Left
        label2.textAlignment=NSTextAlignment.Center
        label3.textAlignment=NSTextAlignment.Right



效果如下

Swift UILabel常用功能_背景色