Swift文本標(biāo)簽(UILabel)的用法

import UIKit

class ViewController: UIViewController        ,UITextFieldDelegate,UITextViewDelegate,UIActionSheet  Delegate,UIAlertViewDelegate{
override func viewDidLoad() {
    super.viewDidLoad()
    
  //UILabel的創(chuàng)建
    let label:UILabel = UILabel()
    //設(shè)置frame大小
    label.frame = CGRectMake(10, 40, 200, 30)
    label.backgroundColor = UIColor.grayColor()
    label.text = "www.baidu.com"
    label.textColor = UIColor.redColor()
    //設(shè)置文字對齊方式
    label.textAlignment = NSTextAlignment.Center
    //設(shè)置陰影顏色
    label.shadowColor = UIColor.blackColor()
    //設(shè)置陰影大小
    label.shadowOffset = CGSizeMake(-5, 5)
    label.font = UIFont(name: "Zapfino", size: 20)
    //文字過長時省略形式
    label.lineBreakMode = NSLineBreakMode.ByTruncatingMiddle
    //文字大小自適應(yīng)標(biāo)簽寬度
    label.adjustsFontSizeToFitWidth = true
    //顯示文字行數(shù)設(shè)置(默認(rèn)顯示是1行)
    label.numberOfLines = 0
    //設(shè)置文本高亮
    label.highlighted = true
    //設(shè)置文本高亮顏色
    label.highlightedTextColor = UIColor.greenColor()
    
    
    //富文本設(shè)置
    
//        var attributeString =NSMutableAttributedString(string: "www.cometoqingdao")
//        attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!, range: NSMakeRange(0,6))
//        attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, 3))
//        attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.blueColor(), range: NSMakeRange(3, 3))
//        label.attributedText = attributeString
    self.view.addSubview(label)
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容