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 ...
随机推荐
- ECS服务器快速迁移
ECS服务器快速迁移 前提 一.停机 二.创建镜像 三.复制镜像 前提 服务器都在同一个区域,举例:华南1(深圳) 同一个账号 具体迁移步骤如下: 一.停机 先从阿里云ECS控制台,将要迁移的两台EC ...
- 蓝桥杯——螺旋折线(2018JavaB组第7题19分)
螺旋折线(18JB-7-19') 如图p1.pgn所示的螺旋折线经过平面上所有整点恰好一次. 对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度. ...
- CSUST 4019 听党指挥(思维+模拟)
题目链接 题目大意 给你一个长度为n的序列(n为偶数),序列为[1,2,3,....n],操作m次,进行m次操作后输出这个序列 有三种操作 1:每次将最左边的元素移到最右边,重复x次 2:每次将最右边 ...
- Pytest学习(十二)-生成HTML报告插件之pytest-html的使用
环境前提 Python3.6+ 安装插件 pip3 install pytest-html -i http://pypi.douban.com/simple/ --trusted-host pypi. ...
- Cys_Control(三) MTextBox
一.查看TextBox原样式 通过Blend查看TextBox原有样式 <Window.Resources> <SolidColorBrush x:Key="TextBox ...
- 【原创】Linux虚拟化KVM-Qemu分析(七)之timer虚拟化
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: KVM版本:5.9 ...
- PyQt(Python+Qt)学习随笔:在一个窗口点击按钮弹出另一个窗口的实现方法及注意事项
在Qt Designer中定义了两个窗口,一个主窗口一个弹出窗口,需要实现在主窗口点击一个按钮时弹出弹出窗口. 经老猿验证: 1.弹窗的窗口类型无特殊要求,只要是QWidget等窗口部件就可以,也可以 ...
- 性能测试学习之路 (一)认识jmeter(性能测试流程 && 性能测试通过标准 &&jmeter体系结构)
性能测试是通过自动化的测试工具模拟多种正常.峰值以及异常负载条件来对系统的各项性能指标进行测试. 1 性能测试技能树 性能测试是一项综合性的工作,致力于暴露性能问题,评估系统性能趋势.性能测试工作实质 ...
- MacOS上Parallels Desktop安装MacOSHighSierra
下载dmg文件 http://www.macoshome.com/macos/977.html 配置宿主机时间2015-10-27 23:25 为了防止"macOS未能安装在您的电脑上_安装 ...
- Hangfire&Autofac与ASP.NET CORE注入失败
Hangfire.Autofac与ASP.NET CORE注入失败 项目里面使用了Hangfire,因为之前没用过吧,遇到了个问题,就是使用了ico容器后,再用Hangfire总是注入不上对象,总是后 ...