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坐标: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属性大全的更多相关文章
- iOS -Swift 3.0 -UIButton属性大全
// // ViewController.swift // Swift-UIButton // // Created by luorende on 16/9/9. // Copyright © ...
- IOS开发UI基础UILabel属性
UILabel属性 1.text:设置标签显示的文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; N ...
- iOS -Swift 3.0 -String(字符串常规用法)
// // ViewController.swift // Swift-String // // Created by luorende on 16/9/10. // Copyright © 2016 ...
- iOS -Swift 3.0 -Array(数组与可变数组相关属性及用法)
// // ViewController.swift // Swift-Array // // Created by luorende on 16/9/12. // Copyright © 2016年 ...
- iOS -Swift 3.0 -for(循环语句用法)
// // ViewController.swift // Swift-循环语句 // // Created by luorende on 16/12/08. // Copyright © 2016年 ...
- iOS开发系列--Swift 3.0
概述 从写第一篇Swift文章的时候到现在Swift已经从1.2发展到了今天的3.0,这期间由于Swift目前还在发展阶段并不能向下兼容,因此第一篇文章中的部分代码在当前的Xcode环境中已经无法运行 ...
- Swift 3.0 令人兴奋,但Objective-C也有小改进--Objective-C的类属性
由于Swift 3.0 出了太多令人兴奋的新特性,人们很容易忽略 Objective-C中的小改动.或许你会觉得苹果提及Objective-C 很可能是为了提高和Swift互操作性(译者注:互操作性主 ...
- iOS开发——新特性OC篇&Swift 2.0新特性
Swift 2.0新特性 转眼间,Swift已经一岁多了,这门新鲜.语法时尚.类型安全.执行速度更快的语言已经渐渐的深入广大开发者的心.我同样也是非常喜爱这门新的编程语言. 今年6月,一年一度 ...
- iOS开发——新特性Swift篇&Swift 2.0 异常处理
Swift 2.0 异常处理 WWDC 2015 宣布了新的 Swift 2.0. 这次重大更新给 Swift 提供了新的异常处理方法.这篇文章会主要围绕这个方面进行讨论. 如何建造异常类型? 在 i ...
随机推荐
- 序列化各个类型成JSON XML实例
LitJson组件序列化请参考http://www.cnblogs.com/leee/p/4437230.html XML序列化请参考http://www.cnblogs.com/leee/p/424 ...
- 【翻译】Kinect v2程序设计(C++) BodyIndex篇
通过Kinect SDK v2预览版,取得BodyIndex(人体区域)的方法和示例代码. 上一节,介绍了从Kinect v2预览版用Kinect SDK v2预览版获取Depth数据的方法. 这 ...
- 浏览器 user-agent 字符串的故事
你是否好奇标识浏览器身份的User-Agent,为什么每个浏览器都有Mozilla字样? 故事还得从头说起,最初的主角叫NCSA Mosaic,简称Mosaic(马赛克),是1992年末位于伊利诺伊大 ...
- Xamarin 示例Standard Controls报错:xamarin Failed to compile interface file. See Build Output for details
Standard Controls 示例下载地址: http://developer.xamarin.com/content/StandardControls/ Xamarin官网上的IOS示例“St ...
- EXT.NET入门必读
Ext.Net是一个对ExtJS进行封装了的.net控件库,可以在ASP.NET WebForm和MVC中使用.从今天开始记录我的学习笔记,这是第一篇,今天学习了如何在WebForm中使用Ext.Ne ...
- bower 基本应用
1.安装 npm install -g bower 2.指定下载目录: 在根目录建立文件 .bowerrc { "directory": "vendor/bower_d ...
- Kafka可靠性的思考
首先kafka的throughput 很牛逼,参考:http://engineering.linkedin.com/kafka/benchmarking-apache-kafka-2-million- ...
- BLE Device Monitor的使用
1 综述 BLE Device Monitor是一个用来显示任意蓝牙低功耗设备服务(services).特征(characteristics).属性(attributes)的windows程序.除了测 ...
- android source compiler
- MSChart参考
MSChart在vs2008中使用遇到一个问题,坐标轴的标题为中文时被图表区域遮挡了一部分. 解决办法:在说明文字前加\n实现换一行显示. //this.Chart1.ChartAreas[0].Ax ...