聲明:以下內(nèi)容只是用來作為個人學(xué)習(xí)、熟悉swift語言開發(fā)或者今后查閱資料使用见秽,也會隨著語言的不不斷更新而不斷的進(jìn)行更新發(fā)布....
UILabel用來在界面上顯示一行或多行文本踱讨,下面就介紹常見屬性與基本用法。
1、UILabel創(chuàng)建####
//創(chuàng)建的時候設(shè)置frame
let testLable=UILabel(frame:CGRect(x:20,y:100,width:self.view.frame.size.width - 40,height:400))
//先創(chuàng)建铡羡,在進(jìn)行位置大小設(shè)定,和上面的作用一樣的
let Label = UILabel()
Label.frame = CGRect(x:20,y:100,width:self.view.frame.size.width - 40,height:400)
//添加
self.view.addSubview(Label);
2意鲸、UILabel常見屬性設(shè)置####
//顯示文本內(nèi)容
testLable.text = "nice to meet you!"
//背景顏色
testLable.backgroundColor = UIColor.green
//文字顏色
testLable.textColor = UIColor.white
//文字對齊方式
testLable.textAlignment = NSTextAlignment.left
其中文字對齊方式有以幾種:
/* Values for NSTextAlignment */
@available(iOS 6.0, *)
public enum NSTextAlignment : Int {
case left // Visually left aligned
case center // Visually centered
case right // Visually right aligned
/* !TARGET_OS_IPHONE */
// Visually right aligned
// Visually centered
case justified // Fully-justified. The last line in a paragraph is natural-aligned.
case natural // Indicates the default alignment for script
}
//陰影顏色
testLable.shadowColor = UIColor.black
//陰影偏移位置
testLable.shadowOffset = CGSize(width:-5,height:5)
//根據(jù)視圖寬度自動調(diào)整文字大小
testLable.adjustsFontSizeToFitWidth = true
//高亮?xí)r候的文字顏色
testLable.highlightedTextColor = UIColor.cyan
//設(shè)置圓角
testLable.layer.cornerRadius = 20
testLable.layer.masksToBounds = true
//邊框的寬度和顏色
testLable.layer.borderColor = UIColor.green.cgColor
testLable.layer.borderWidth = 2
//文字類型/大小
testLable.font = UIFont.boldSystemFont(ofSize: 20) //加粗類型
testLable.font = UIFont.systemFont(ofSize: 20)//文字大小
testLable.font = UIFont.italicSystemFont(ofSize: 20)//斜體類型
//大小和文字一起設(shè)置
testLable.font = UIFont(name:"您好",size:50)
//顯示樣式
testLable.lineBreakMode = NSLineBreakMode.byCharWrapping
// NSParagraphStyle
@available(iOS 6.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.numberOfLines = 2//最多顯示2行
label.numberOfLines = 0// 默認(rèn)沒有行數(shù)顯示
label.numberOfLines = 1//只能顯示一行
3烦周、富文本設(shè)置####
//創(chuàng)建對象
let attributeString = NSMutableAttributedString(string:"Welcome to Swift! Welcome to Swift! Welcome to Swift! Welcome to Swift!")
//設(shè)置字體大小/字體類型
attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!, range: NSMakeRange(0, 6))
//設(shè)置背景顏色
attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(0, 3))
//設(shè)置文檔背景色 attributeString.addAttribute(NSBackgroundColorDocumentAttribute, value: UIColor.lightGray, range: NSMakeRange(10, 10))
//設(shè)置下劃線
attributeString.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.StyleSingle.rawValue, range: NSMakeRange(5,12))
testLable.attributedText = attributeString;
4、文本高度計算####
關(guān)于lable顯示文本的高度計算也是老生常談怎顾,下面就簡單的舉個例子以extension為例:
extension NSString {
func textSizeWithFont(font: UIFont, constrainedToSize size:CGSize) -> CGSize {
var textSize:CGSize!
if CGSizeEqualToSize(size, CGSizeZero) {
let attributes = NSDictionary(object: font, forKey: NSFontAttributeName)
textSize = self.sizeWithAttributes(attributes)
} else {
let option = NSStringDrawingOptions.UsesLineFragmentOrigin
let attributes = NSDictionary(object: font, forKey: NSFontAttributeName)
let stringRect = self.boundingRectWithSize(size, options: option, attributes: attributes, context: nil)
textSize = stringRect.size
}
return textSize
}
}