swift学习之label,button,imageView,字符串,数组,字典
Posted 执笔葬青春
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,字符串,数组,字典的主要内容,如果未能解决你的问题,请参考以下文章