swift語言UILabel的使用

好記性不如爛筆頭這個在這麼多年的學習生涯中我是深有體會,所以接下來的內容會比較簡單,但是還是記下來了。

不囉嗦了我們先上代碼其他的然後再說:

import Foundation
import UIKit
class CreateLabel: UILabel {
    func createLabel() ->UILabel {
        let label = UILabel(frame:CGRectMake(10,20,300,200))
        label.text = "S去年5月攻陷巴爾米拉,至今年初才被政府軍光復。文件顯示IS敗走前,曾下令“將所有重型大炮及防空機槍,從巴爾米拉城內及周邊撤回拉卡省”。另一封向前線指揮官下達的信件寫道:“運送所有裝備和武器到協議好的撤離點,我們收到情報古賽爾及附近範圍會在2013年11月24日被轟炸。”電視臺問IS變節者,曾否在前佔領地與敘利亞政府軍,甚至俄羅斯空軍協調行軍和撤退,變節者直認不諱:“當然"
        //顏色
        label.textColor = UIColor.yellowColor()
        label.backgroundColor = UIColor.blackColor()
        //文字對齊
        label.textAlignment = .Center
        //文字陰影
        label.shadowColor = UIColor.redColor()
        //陰影偏移量
        label.shadowOffset = CGSizeMake(-5,5)
        //字體設置
        label.font = UIFont(name: "Zapfino", size: 20)
        //文字過長省略方式
        label.lineBreakMode = .ByTruncatingMiddle
        //文字大小自適應
         label.adjustsFontSizeToFitWidth = true
        //顯示多行
        label.numberOfLines = 0
        //文本高亮
        label.highlighted = true
        label.highlightedTextColor = UIColor.greenColor()
        //富文本 NSFontAttributeName(字體大小,種類) //NSForegroundColorAttributeName(字體顏色)
        // NSBackgroundColorAttributeName(字體背景顏色)
        let attributeString = NSMutableAttributedString(string: "S去年5月攻陷巴爾米拉,至今年初才被政府軍光復。文件顯示IS敗走前,曾下令“將所有重型大炮及防空機槍,從巴爾米拉城內及周邊撤回拉卡省”。另一封向前線指揮官下達的信件寫道:“運送所有裝備和武器到協議好的撤離點,我們收到情報古賽爾及附近範圍會在2013年11月24日被轟炸。”電視臺問IS變節者,曾否在前佔領地與敘利亞政府軍,甚至俄羅斯空軍協調行軍和撤退,變節者直認不諱:“當然")
        attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 15)!,range: NSMakeRange(0,6))
        attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSMakeRange(7, 16))
        attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(17, 25))
        label.attributedText = attributeString

        return label
    }
}

關於字體設置swift中是這麼說的使用css中的字體名字:

  // Returns a font using CSS name matching semantics.
    public /*not inherited*/ init?(name fontName: String, size fontSize: CGFloat)

adjustsFontSizeToFitWidth這個屬性是指當文本框太小的時候會自動縮小字體使文本全部顯示關於富文本的主要是要明白有哪些屬性是可以通過富文本來設置的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章