swift中UILabel的使用

2016-12-18  本文已影响0人  不安分心

UILabel的相关属性

import UIKit

class MainViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.red
        
        self.setupUILabel()
    }
    
    func setupUILabel() {
    
        // 创建一个label
        let label = UILabel(frame: CGRect(x: 10, y: 50, width: 100, height: 100))
        label.backgroundColor = UIColor.orange
        // 设置字体的颜色
        label.textColor = UIColor.black
        // 设置文字
        label.text = "HJ_66HJ_66HJ_66HJ_66HJ_66HJ_66HJ_66HJ_66HJ_66HJ_66"
        // 设置文字居中显示
        /**
         public enum NSTextAlignment : Int {
         
         case left // Visually left aligned
         case center // Visually centered
         case right // Visually right aligned
         case justified // Fully-justified. The last line in a paragraph is natural-aligned.
         case natural // Indicates the default alignment for script
         }
         */
        label.textAlignment = .center
        // 自适应文字大小
        label.adjustsFontSizeToFitWidth = true
        // 设置字体大小
        label.font = UIFont.boldSystemFont(ofSize: 12)
        // 设置文本行数,0为自动换行
        label.numberOfLines = 0
        // 截取
        /**
         public enum NSLineBreakMode : Int {
         
         case byWordWrapping // Wrap at word boundaries, default
         case byCharWrapping // Wrap at character boundaries
         case byClipping // Simply clip
         case byTruncatingHead // Truncate at head of line: "...wxyz"
         case byTruncatingTail // Truncate at tail of line: "abcd..."
         case byTruncatingMiddle // Truncate middle of line:  "ab...yz"
         }
         */
        label.lineBreakMode = .byCharWrapping
        // 是否能和用户交互
        label.isUserInteractionEnabled = true
        // 文字是否可变,默认是true
        label.isEnabled = true
        // 设置阴影颜色
        label.shadowColor = UIColor.blue
        // 设置阴影偏移量
        label.shadowOffset = CGSize(width: 0.5, height: 0.5)
        // 设置是否高亮和高亮颜色
        label.isHighlighted = true
        label.highlightedTextColor = UIColor.yellow
        
        self.view.addSubview(label)
    }
}
上一篇下一篇

猜你喜欢

热点阅读