昨天研究了一下苹果近两年新出的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.

}

}

iOS -Swift 3.0 -UILabel属性大全的更多相关文章

  1. iOS -Swift 3.0 -UIButton属性大全

    // //  ViewController.swift //  Swift-UIButton // //  Created by luorende on 16/9/9. //  Copyright © ...

  2. IOS开发UI基础UILabel属性

    UILabel属性 1.text:设置标签显示的文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first";  N ...

  3. iOS -Swift 3.0 -String(字符串常规用法)

    // // ViewController.swift // Swift-String // // Created by luorende on 16/9/10. // Copyright © 2016 ...

  4. iOS -Swift 3.0 -Array(数组与可变数组相关属性及用法)

    // // ViewController.swift // Swift-Array // // Created by luorende on 16/9/12. // Copyright © 2016年 ...

  5. iOS -Swift 3.0 -for(循环语句用法)

    // // ViewController.swift // Swift-循环语句 // // Created by luorende on 16/12/08. // Copyright © 2016年 ...

  6. iOS开发系列--Swift 3.0

    概述 从写第一篇Swift文章的时候到现在Swift已经从1.2发展到了今天的3.0,这期间由于Swift目前还在发展阶段并不能向下兼容,因此第一篇文章中的部分代码在当前的Xcode环境中已经无法运行 ...

  7. Swift 3.0 令人兴奋,但Objective-C也有小改进--Objective-C的类属性

    由于Swift 3.0 出了太多令人兴奋的新特性,人们很容易忽略 Objective-C中的小改动.或许你会觉得苹果提及Objective-C 很可能是为了提高和Swift互操作性(译者注:互操作性主 ...

  8. iOS开发——新特性OC篇&Swift 2.0新特性

    Swift 2.0新特性     转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心.我同样也是非常喜爱这门新的编程语言. 今年6月,一年一度 ...

  9. iOS开发——新特性Swift篇&Swift 2.0 异常处理

    Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...

随机推荐

  1. PHP 开发 APP 接口 学习笔记与总结 - 静态缓存

    存储静态缓存即把缓存写入文件. file.php <?php class Cache{ //静态缓存文件后缀名 const EXT = 'txt'; //定义缓存文件存放路径 private $ ...

  2. SET GLOBAL slow_query_log=1

    - Variable 'slow_query_log' is a GLOBAL variable and should be set with SET GLOBAL SHOW VARIABLES LI ...

  3. design the relations

    Computer Science An Overview _J. Glenn Brookshear _11th Edition A pivotal step in designing a relati ...

  4. 蓝牙BLE ATT剖析(一)

    一.概述 The attribute protocol allows a device referred to as the server to expose a set of attributes ...

  5. GCD的简单介绍

    一)GCD 的使用方式 dispatch_async(dispatch_queue_t queue, dispatch_block_t block); async表明运行方式 queue则是你把任务交 ...

  6. ThreadLocal知识总结

    以前学习<Thinking in Java>时,了解过ThreadLocal,但没做笔记.现在又忘记了.嗯,要勤动笔.API中ThreadLocal的方法: public T get() ...

  7. Qt 之 自定义按钮 在鼠标 悬浮、按下、松开后的效果(全部通过QSS实现)

    http://blog.csdn.net/goforwardtostep/article/details/53464925

  8. 【Android测试】【随笔】在手机里用命令行创建中文文件夹

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4580319.html 不知道为啥当时自己写了一段在手机里用 ...

  9. 设计模式:访问者模式(Visitor)

    定  义:表示作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 结构图: 示例: . 状态类: //状态的抽象类 abstract class Act ...

  10. windows2008一键安装环境的配置说明

    windows 2008 一键安装包下载地址为 http://gongdan.oss-cn-hangzhou.aliyuncs.com/market/cmISV/34320/product/cmgj0 ...