zoukankan      html  css  js  c++  java
  • iOS -Swift 3.0 -UILabel属性大全

      昨天研究了一下苹果近两年新出的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坐标:10y坐标: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、设置有多行显示:高度使用

                  */

            // 文本有多大,窗口有多大

            // 细节不管高度宽度是否足够,都显示相应的高度

            // 细节: numberOfLines1,那么就是单行显示

            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.

        }

     

     

    }

     

  • 相关阅读:
    java web开发中会遇到的异步执行方案
    MySQL中进行树状所有子节点的查询
    node.js ----NPM使用介绍
    Node.js--学习笔记
    node.js介绍及Win7环境安装测试(转)
    Jmeter中Websocket协议支持包的使用(转)
    jmeter---将回应数据写入到文件
    JMeter 插件 Json Path 解析 HTTP 响应 JSON 数据(转)
    python + Pyglet ---播放视频
    转 RTSP客户端模拟器(TCP方式,Python实现)
  • 原文地址:https://www.cnblogs.com/luorende/p/5856292.html
Copyright © 2011-2022 走看看