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;
相關文章
相關標籤/搜索