swift学习之label,button,imageView,字符串,数组,字典
import UIKit
class ViewController: UIViewController,UITextFieldDelegate {
var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//背景颜色
self.view.backgroundColor = UIColor.whiteColor()
//label
let lable = UILabel(frame:CGRect(x: 10, y: 30, width: 200, height: 70))
lable.backgroundColor = UIColor.blueColor()
lable.font = UIFont(name: "HelveticaNeue-Bold", size:18)
self.view.addSubview(lable)
lable.textAlignment = NSTextAlignment.Center
lable.textColor = UIColor.purpleColor()
lable.shadowColor = UIColor.lightGrayColor()
lable.shadowOffset = CGSize(width: -5, height: 5)
lable.lineBreakMode = NSLineBreakMode.ByCharWrapping
lable.adjustsFontSizeToFitWidth = true
lable.highlighted = true
lable.highlightedTextColor = UIColor.redColor()
let mutableStr = NSMutableAttributedString(string: "我是中国人我骄傲")
mutableStr.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 38)!, range: NSMakeRange(0,4))
mutableStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.grayColor(),
range: NSMakeRange(0, 4))
//设置文字背景颜色
mutableStr.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(),
range: NSMakeRange(3,3))
lable.attributedText = mutableStr
//button
// let button = UIButton(frame:CGRectMake(10, 150, 100, 30))
let button:UIButton = UIButton(type:.ContactAdd)
//设置按钮位置和大小
button.frame = CGRectMake(10, 150, 100, 30)
//设置按钮文字
button.setTitle("按钮", forState:UIControlState.Normal)
self.view.addSubview(button);
button.setTitle("普通状态", forState:UIControlState.Normal) //普通状态下的文字
button.setTitleColor(UIColor.redColor(),forState: .Normal) //普通状态下文字的颜色
button.setTitleShadowColor(UIColor.greenColor(),forState:.Normal) //普通状态下文字阴影的颜色
button.backgroundColor=UIColor.grayColor()
button.setImage(UIImage(named:"icon1"),forState:.Normal) //设置图标
button.adjustsImageWhenHighlighted=false //使触摸模式下按钮也不会变暗
button.adjustsImageWhenDisabled=false //使禁用模式下按钮也不会变暗
button.setBackgroundImage(UIImage(named:"background1"),forState:.Normal)
//不传递触摸对象(即点击的按钮)
button.addTarget(self,action:#selector(ViewController.tapped),forControlEvents:.TouchUpInside)
// button.addTarget(self,action:#selector(tapped(_:)),forControlEvents:.TouchUpInside)
// UITextField
textField = UITextField(frame: CGRect(x: 30, y: 190, width: 160, height: 30))
textField.borderStyle = UITextBorderStyle.RoundedRect
self.view.addSubview(textField)
textField.delegate = self
textField.placeholder = "请输入用户名"
textField.adjustsFontSizeToFitWidth = true //当文字超出文本框宽度时,自动调整文字大小
textField.minimumFontSize = 14 //最小可缩小的字号
textField.textAlignment = .Left //水平左对齐
textField.contentVerticalAlignment = .Center //垂直居中对齐
//背景图片设置
// textField.borderStyle = .None //先要去除边框样式
// textField.background=UIImage(named:"background1");
//清除按钮(输入框内右侧小叉)
textField.clearButtonMode=UITextFieldViewMode.WhileEditing //编辑时出现清除按钮
// UIImageView的使用
let imageView = UIImageView(image: UIImage(named: "icon"))
imageView.frame = CGRectMake(30, 230, 100, 100)
self.view.addSubview(imageView)
let tap111 = UITapGestureRecognizer(target: self, action:#selector(ViewController.tap))
imageView.userInteractionEnabled = true
imageView.addGestureRecognizer(tap111)
//字符串使用
//1 判断字符串是否为空
let test1Str="111"
let test1Str2:String = String("ssss");
print("test1Str" + (test1Str.isEmpty ? "没有值" : "有值"))
print("test1Str2" + (test1Str2.isEmpty ? "没有值" : "有值"))
print("88737979879879")
//数组的使用
let array = ["1","2","3"]
for item in array{
print("------" + item);
}
//字典的使用
let dict = ["1":"111","2":"222","3":"333"]
let arr:NSMutableArray=NSMutableArray.init(capacity: 100);
for key in dict.values{
print(key)
arr.addObject(key)
}
print(arr)
}
func tap(){
print("++++++++++++11111111")
}
// 传递触摸对象(即点击的按钮),需要在定义action参数时,方法名称后面带上冒号
func tapped(button:UIButton){
print(button.titleForState(.Normal))
}
//收起键盘
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
textField.resignFirstResponder()
}
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField.resignFirstResponder()
return true;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
swift学习之label,button,imageView,字符串,数组,字典的更多相关文章
- Swift学习—字符串&数组&字典
字符串 OC和Swift中字符串的区别 在OC中字符串类型时NSString,在Swift中字符串类型是String OC中字符串@"",Swift中字符串"" ...
- Swift(一,创建对象,类型推导,基本运算,逻辑,字符串,数组,字典)
swift用起来刚开始感觉有点怪怪的,但用了一段时间觉得还是挺好用的,哈哈.毕竟都是要有一个过程的嘛. 我就写一些自己在使用swift的时候的注意点吧,如有不正之处,还请指正! 一.在开发中优先使用常 ...
- Object-C 语法 字符串 数组 字典 和常用函数 学习笔记
字符串 //取子字符串 NSString *str1=@"今天的猪肉真贵,200块一斤"; NSString *sub1=[str1 substringFromIndex:4]; ...
- swift学习第五天:字符串
字符串的介绍 字符串在任何的开发中使用都是非常频繁的 OC和Swift中字符串的区别 在OC中字符串类型时NSString,在Swift中字符串类型是String OC中字符串@"" ...
- swift学习之Label
//UILabel的使用方法 let label:UILabel = UILabel(frame: CGRect(x: 0, y: 100, width: view. ...
- IOS NS 字符串 数组 字典 文件 动态 静态 操作
ios 常用字符串的操作 //将NSData转化为NSString NSString* str = [[NSString alloc] initWithData:response e ...
- oc字符串+数组+字典操作题目
1. 判断中间目录是否存在 (10分) 比如 传入字符串 @"/home/qianfeng/oc.txt" 和 @"qianfeng" 返回:YES 传入字符串 ...
- python学习之路二(字符串,字典,序列和元组)
# -*- coding: utf-8 -* ''' Created on 2013-7-26 @author: lixingle ''' #!/usr/bin/python import math# ...
- Swift学习笔记(二十二)——字典的基本操作
(1)计算字典长度 . (2)推断字典是否为空 . (3)查询字典元素 . (4)取出字典元素进行拼接 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/fo ...
随机推荐
- iOS 百度地图使用详解
最近仿照美团做了款应用,刚好用到百度地图,高德地图之前用的比较多,只是这个项目的后台服务器是另外一个公司做的,他们用的就是百度地图,现在网上用百度地图的还不算太多,博文也是断断续续的,主要是中间跳跃有 ...
- Java基础教程——接口
接口 接口只是一种约定.--Anders 接口定义了一种规范--多个类共同的公共行为规范. 对于接口的实现者--规定了必须向外提供哪些服务 对于接口的调用者--规定了可以调用哪些服务,如何调用这些服务 ...
- [题解] 洛谷 P3393 逃离僵尸岛
题目TP门 很明显是一个最短路,但是如何建图才是关键. 对于每一个不可遍历到的点,可以向外扩散,找到危险城市. 若是对于每一个这样的城市进行搜索,时间复杂度就为\(O(n^2)\),显然过不了.不妨把 ...
- 在EXCEL带有字母的数字下拉如何能自动排序
在excel中0,1,2,3,4,5,6,7,8,9会自动排序,a,b,c,d,e,f,g.....会自动排序,所以可以分布来实现. 例如排序:fish1a.png,fish1b.png,fish1c ...
- Arcgis基于高程(DEM)计算地形湿度指数(TWI),以及坡度(Slope)度单位转换为弧度
以30m*30m分辨率的图层为例 一.基于表面工具箱Surface计算Slope 1.如下图输入图层DEM,输出Slope 2.单位转换: Scale_slope=Slope*pi/180 二.基于水 ...
- Pytest自动化测试 - allure报告进阶
Allure除了具有Pytest基本状态外,其他几乎所有功能也都支持. 1.严重性 如果你想对测试用例进行严重等级划分,可以使用 @allure.severity 装饰器,它可以应用于函数,方法或整个 ...
- Spring 源码学习 04:初始化容器与 DefaultListableBeanFactory
前言 在前一篇文章:创建 IoC 容器的几种方式中,介绍了四种方式,这里以 AnnotationConfigApplicationContext 为例,跟进代码,看看 IoC 的启动流程. 入口 从 ...
- Python中str类型的字符串写入二进制文件时报TypeError错的处理方式
在用二进制模式打开文件情况下,写入一个str对象时报错:TypeError: a bytes-like object is required, not 'str' 出现该问题是因为Python严格区分 ...
- Python学习随笔:PyCharm的错误检测使用及调整配置减少错误数量
老猿使用PyCharm有将近一个月了,发现PyCharm并不能很好的完成语法检查,有时运行时突然终止,仔细核查却发现是基本的语法错误,不过有次无意中移动鼠标到代码最右边的边框时发现其实PyCharm有 ...
- PyQt学习随笔:Qt中Model/View中的Model Index
Qt中Model/View中的Model Index是一个类,该类用于定位Model/View中数据模型中的数据. Model Index是从QAbstractItemModel派生的子类,用于在项视 ...