昨天研究了一下蘋果近兩年新出的Swift語言,感覺學起來並不是很吃力,畢竟自己有過Objective-C的語言功底,所以各方面的屬性控件還是一眼就可以認出的,只是Swift的寫法與Objective-C寫法不同而已,這點還是要花點時間來習慣就好了,下面來看Swift的UILabel的相關屬性與寫法吧:
注意:剛開始初始化的時候,有語法報錯,不必理會,接着往下寫就好了
//
// ViewController.swift
// Swift-UILabel
//
// Created by luorende on 16/9/9.
// Copyright © 2016年 luorende. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//設置標簽x坐標:10,y坐標:20,長:300,寬:100
let label=UILabel(frame:CGRectMake(10,20, 300, 100))
// 顯示文本【需要顯示什么就設置這個 text 的屬性即可】
label.text=" Welcome to study Swift !"
// label的字體顏色
label.textColor=UIColor.redColor() //紅色文字
// label的背景顏色
label.backgroundColor=UIColor.blackColor() //黑色背景
// label的文字對齊方式
/**
case Left(左對齊)
case Center(居中)
case Right(右對齊)
*/
label.textAlignment=NSTextAlignment.Right //文字右對齊
// label陰影顏色【要設置偏移位置】(字體的陰影顏色)
label.shadowColor=UIColor.grayColor() //灰色陰影
// label陰影偏移位置
label.shadowOffset=CGSizeMake(-5,5) //陰影的偏移量
// 多行顯示,默認是一行的,0表示的多行顯示(與高度有關)Label自適應自動換行
label.numberOfLines=0 //顯示兩行文字(默認只顯示一行,設為0表示沒有行數限制)
// 自適應(不建議使用)
/*
1、沒有設置多行顯示:寬度自適應
2、設置有多行顯示:高度使用
*/
// 文本有多大,窗口有多大
// 細節: 不管高度寬度是否足夠,都顯示相應的高度
// 細節: numberOfLines為1,那么就是單行顯示
label.adjustsFontSizeToFitWidth=true //當文字超出標簽寬度時,自動調整文字大小,使其不被截斷
//設置label文本高亮
label.highlighted = true
//設置label文本高亮顏色
label.highlightedTextColor = UIColor.greenColor()
// label圓角屬性
label.layer.masksToBounds = true;
// label圓角半徑
label.layer.cornerRadius = 10;
// label圓角邊框顏色
label.layer.borderColor = UIColor.blueColor().CGColor;
// label圓角邊框寬度
label.layer.borderWidth = 1;
// label的字體大小
/**
systemFontOfSize(20) -> UIFont (文字大小)
boldSystemFontOfSize(20) -> UIFont (加粗類型)
italicSystemFontOfSize(20) -> UIFont (斜體類型)
*/
label.font = UIFont.systemFontOfSize(50)
// 設置字體時,同時設置大小
label.font = UIFont(name:"您好!", size:50)
// label的特殊屬性
/**
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=NSLineBreakMode.ByTruncatingTail //隱藏尾部並顯示省略號
label.lineBreakMode=NSLineBreakMode.ByTruncatingMiddle //隱藏中間部分並顯示省略號
label.lineBreakMode=NSLineBreakMode.ByTruncatingHead //隱藏頭部並顯示省略號
label.lineBreakMode=NSLineBreakMode.ByClipping //截去多余部分也不顯示省略號
// 將視圖添加到(self.view-->父視圖)界面中;
self.view.addSubview(label);
//富文本設置
let attributeString = NSMutableAttributedString(string:"Welcome to study Swift !")
//從文本0開始6個字符字體HelveticaNeue-Bold,16號字體大小
attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!,range: NSMakeRange(0,6))
//設置字體顏色
attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),range: NSMakeRange(0, 3))
//設置文字背景顏色
attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(),range: NSMakeRange(3,3))
label.attributedText = attributeString
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}