Use of tags in swift

Source: Internet
Author: User

1, Label creation
12345678910 importUIKitclassViewControllerUIViewController{    overridefuncviewDidLoad() {    super.viewDidLoad()    //设置标签x坐标:10,y坐标:20,长:300,宽:100    varlabel=UILabel(frame:CGRectMake(10,20, 300, 100))    label.text="hangge.com"    self.view.addSubview(label);    }}
2, background color and text color settings
12 label.textColor=UIColor.whiteColor()  //白色文字label.backgroundColor=UIColor.blackColor() //黑色背景
3, Alignment settings
1 label.textAlignment=NSTextAlignment.Right//文字右对齐
4, Text shadow settings
12 label.shadowColor=UIColor.grayColor()  //灰色阴影label.shadowOffset=CGSizeMake(-5,5)  //阴影的偏移量
5, font settings
1 label.font = UIFont(name:"Zapfino", size:20)
6, the way of ellipsis when the text is too long
1234 label.lineBreakMode=NSLineBreakMode.ByTruncatingTail//隐藏尾部并显示省略号label.lineBreakMode=NSLineBreakMode.ByTruncatingMiddle//隐藏中间部分并显示省略号label.lineBreakMode=NSLineBreakMode.ByTruncatingHead//隐藏头部并显示省略号label.lineBreakMode=NSLineBreakMode.ByClipping//截去多余部分也不显示省略号
7, Text size adaptive label width
1 label.adjustsFontSizeToFitWidth=true//当文字超出标签宽度时,自动调整文字大小,使其不被截断
8, so that labels can display multiple lines of text
1 label.numberOfLines=2  //显示两行文字(默认只显示一行,设为0表示没有行数限制)

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

10, Rich Text settings
123456789101112 //富文本设置varattributeString = NSMutableAttributedString(string:"welcome to hangge.com")//从文本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

Use of tags in swift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.