一萬小時.iOS App學習:UILabel(2)

MikeWu
MikeWu
Aug 29, 2017 · 4 min read

一個標籤,是由兩大物件所組成,一個就是標籤的本體,另一個就是標籤內所顯示的文字內容。針對這兩個物件,它們本身都具有其所屬的屬性。以下先針對內容文字的屬性部份作介紹。

設定標籤內顯示的文字

變數名稱.text = “內容

myLabel.text = “Hello!Welcome to iOS World!”

設定標籤文字的顏色

變數名稱.textColor = UIColor.顏色

myLabel.textColor = UIColor.white

設定標籤內文字的對齊方式

變數名稱.textAlignment = NSTextAlignment.位置

myLabel.textAlignment = NSTextAlignment.center

設定標籤文字的大小

變數名稱.font = UIFont.systemFont(ofSize:大小)

myLabel.font = UIFont.systemFont(ofSize: 24)

設定標籤粗體文字與大小

變數名稱.font = UIFont.boldSystemFont(ofSize:大小)

myLabel.font = UIFont.boldSystemFont(ofSize: 24)

設定標籤斜體文字

變數名稱.font = UIFont.italicSystemFont(ofSize:大小)

myLabel.font = UIFont.italicSystemFont(ofSize: 24)

設定標籤文字的字型與大小

變數名稱.font = UIFont(name: 字型名稱, size: 大小值)

myLabel.font = UIFont(name: “Helvetica-Light”, size: 12)

設定標籤文字的陰影顏色

變數名稱.shadowColor = UIColor.顏色

myLabel.shadowColor = UIColor.black

設定標籤文字的陰影偏移量

變數名稱.shadowOffset = CGSize(width:數值, height:數值)

myLabel.shadowOffset = CGSize(width:2, height:2)

※設置陰影方向與大小:(0,-1):上陰影、(0,1):下陰影、(-1,0):左陰影、(1,0):右陰影

設定標籤文字顯示的行數(預設值為1)

變數名稱.numberOfLines = 數值

myLabel.numberOfLines = 2

設定標籤文字過多時,所顯示的方式

變數名稱.lineBreakMode = NSLineBreakMode.顯示方式

mylabel.lineBreakMode = NSLineBreakMode.byTruncatingTail

設定自動調整文字大小來適應標籤寬度

變數名稱.adjustsFontSizeToFitWidth = true

mylabel.adjustsFontSizeToFitWidth = true

參考資料:

http://www.jianshu.com/p/6cb0b523ecea

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade