Swift - UILabel的用法

 let label = UILabel(frame: CGRect(x: 10, y: 20, width: 300, height: 100))
        label.text = "Hello WorldHello WorldHello WorldHello World"
        label.textColor = UIColor.red;
        label.backgroundColor = UIColor.blue
        label.textAlignment = .right//文字右对齐
        label.shadowColor = UIColor.yellow//黄色阴影
        label.shadowOffset = CGSize(width: 1.5, height: 1.5)//阴影的偏移量
        label.font = UIFont(name:"Zapfino",size:20)//字体设置
        label.lineBreakMode = .byTruncatingTail//隐藏尾部并显示省略号
        label.adjustsFontSizeToFitWidth = true//当文字超出标签宽度时,自动调整文字大小,使其不被截断
        label.numberOfLines = 0
        label.isHighlighted = true//设置文本高亮
        label.highlightedTextColor = UIColor.green//设置文本高亮颜色
        self.view.addSubview(label)
        
        //富文本设置
        let attributestring = NSMutableAttributedString(string:"welcome to china")
        //从文本0开始6个字符字体HelveticaNeue-Bold,16号
        attributestring.addAttribute(NSAttributedStringKey.font, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!,
                                     range: NSMakeRange(0,6))
        //设置字体颜色
        attributestring.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue, range: NSMakeRange(0, 3))
        
        //设置文字背景颜色
        attributestring.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.green, range: NSMakeRange(3, 3))
        
        label.attributedText = attributestring;

 

posted @ 2018-04-10 18:45  小白姐  阅读(266)  评论(0编辑  收藏  举报