微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - 文本标签UILabel的用法

1,标签的创建
1
2
3
4
5
6
7
8
9
10
import UIKit
class ViewController : UIViewController {
override func viewDidLoad() {
super .viewDidLoad()
//设置标签x坐标:10,y坐标:20,长:300,宽:100
var label= UILabel (frame: CGRectMake (10,300,100))
label.text= "hangge.com"
self .view.addSubview(label);
}
}

2,背景颜色和文字颜色的设置
2
label.textColor= UIColor .whiteColor() //白色文字
label.backgroundColor= .blackColor() //黑色背景
3,对齐方式的设置
1
label.textAlignment= NSTextAlignment . Right //文字右对齐
4,文字阴影的设置
label.shadowColor=.grayColor()//灰色阴影
label.shadowOffset= CGSizeMake (-5,5) //阴影的偏移量

5,字体的设置
label.font =UIFont(name:"Zapfino",size:20)
6,文字过长时的省略方式
4
label.lineBreakMode= NSLineBreakMode ByTruncatingTail //隐藏尾部并显示省略号
ByTruncatingMiddle //隐藏中间部分并显示省略号
ByTruncatingHead //隐藏头部并显示省略号
ByClipping //截去多余部分也不显示省略号
7,文字大小自适应标签宽度
label.adjustsFontSizetoFitWidth=true//当文字超出标签宽度时,自动调整文字大小,使其不被截断
8,使标签可以显示多行文字
label.numberOfLines=2//显示两行文字认只显示一行,设为0表示没有行数限制)

9,设置文本高亮
//设置文本高亮
label.highlighted = true
//设置文本高亮颜色
label.highlightedTextColor = .greenColor()

10,富文本设置
5
6
7
8
10
11
12
//富文本设置
attributeString = NSMutableAttributedString (string: "welcome to hangge.com" )
//从文本0开始6个字符字体HelveticaNeue-Bold,16号
attributeString.addAttribute( NSFontAttributeName ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,value: "HelveticaNeue-Bold" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,size: 16)!,
range: NSMakeRange (0,6))
//设置字体颜色
NSForegroundColorAttributeName .blueColor(),
//设置文字背景颜色
NSBackgroundColorAttributeName .greenColor(),
(3,3))
label.attributedText = attributeString

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐