前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swift - Button,Label

Swift - Button,Label

作者头像
Python疯子
发布2018-09-06 16:03:17
1.7K0
发布2018-09-06 16:03:17
举报
文章被收录于专栏:Python疯子Python疯子

Swift-Button的常用

代码语言:javascript
复制
func setButton()
 {
    // 创建一个类型为contactAdd的按钮
    let button:UIButton = UIButton(type:.contactAdd)
    
    // 设置按钮的位置和大小
    button.frame = CGRect(x:10, y:150, width:100, height:30)
    
    //设置按钮文字
    button.setTitle("普通按钮", for:.normal)
    //        button.setTitle("触摸状态", for: .highlighted)
    //        button.setTitle("禁用状态", for: .disabled)
    
    // 设置文字颜色
    button.setTitleColor(UIColor.red, for: UIControlState.normal)
    button.setTitleColor(UIColor.blue, for: .highlighted)
    
    // 设置按钮阴影颜色
    button.setTitleShadowColor(UIColor.green, for: UIControlState.normal)
    
    // 改变图片 但改后的图片是按钮的默认色:蓝色 丢失了图片的原色
    button.setImage(UIImage(named: "cocktail_dog"), for: UIControlState.normal)
    
    // 改变图片 保证图片不失真
    let buttonImage = UIImage(named:"cocktail_dog")?.withRenderingMode(.alwaysOriginal)
    button.setImage(buttonImage, for: UIControlState.normal)
    
    button.adjustsImageWhenHighlighted = false //使触摸模式下按钮也不会变暗(半透明)
    button.adjustsImageWhenDisabled = false //使禁用模式下按钮也不会变暗(半透明)
    
    // button的处理事件
    //        button .addTarget(self, action: #selector(buttonAction), for:.touchUpInside)
    button .addTarget(self, action: #selector(action2(button:)), for:.touchUpInside)
    self.view.addSubview(button)
}

func buttonAction() {
    print("按钮无数据")
}

func action2(button:UIButton)
{
    print("按钮事件:%@", button.title(for: .normal))
    let str:String =  button.title(for: .normal)!
    
    if str == "按钮" {
        print("按钮相等")
    }
}

关于Button的一些类型

代码语言:javascript
复制
/*
 按钮有下面四种类型:
 UIButtonType.ContactAdd:前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
 UIButtonType.DetailDisclosure:前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
 UIButtonType.System:前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
 UIButtonType.Custom:定制按钮,前面不带图标,默认文字颜色为白色,无触摸时的高亮效果
 UIButtonType.InfoDark:为感叹号“!”圆形按钮
 UIButtonType.InfoLight:为感叹号“!”圆形按钮
 
 常用的触摸事件类型:
 TouchDown:单点触摸按下事件,点触屏幕
 TouchDownRepeat:多点触摸按下事件,点触计数大于1,按下第2、3或第4根手指的时候
 TouchDragInside:触摸在控件内拖动时
 TouchDragOutside:触摸在控件外拖动时
 TouchDragEnter:触摸从控件之外拖动到内部时
 TouchDragExit:触摸从控件内部拖动到外部时
 TouchUpInside:在控件之内触摸并抬起事件
 TouchUpOutside:在控件之外触摸抬起事件
 TouchCancel:触摸取消事件,即一次触摸因为放上太多手指而被取消,或者电话打断
 */

Swift里Label的使用

代码语言:javascript
复制
func setLabel() {
    let label = UILabel(frame:CGRect(x:50, y:300, width:200, height:30))
    label.text = "这是一个Label"
    self.view .addSubview(label)
    
    label.backgroundColor = UIColor.lightGray
    label.textColor = UIColor.red
    label.textAlignment = NSTextAlignment.center
    
//        label.shadowColor = UIColor.green
//        label.shadowOffset = CGSizeMake(5, 5)
   
    // 字体的设置
    label.font = UIFont.systemFont(ofSize: 15)
//        label.font = UIFont(name:"Zapfino", size:12)
    
    // 文字过长时的省略方式
    /*
     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.byWordWrapping                  // 截去多余部分也不显示省略号
 //        label.lineBreakMode=NSLineBreakMode.byCharWrapping        // 截去多余部分也不显示省略号
 //        label.lineBreakMode=NSLineBreakMode.byClipping        // 截去多余部分也不显示省略号
 //        label.lineBreakMode=NSLineBreakMode.byTruncatingHead  //隐藏头部并显示省略号
 //        label.lineBreakMode=NSLineBreakMode.byTruncatingTail  //隐藏尾部并显示省略号
//         label.lineBreakMode=NSLineBreakMode.byTruncatingMiddle   //隐藏中间部分并显示省略号
    
    // 文字大小自适应标签宽度
//        label.adjustsFontSizeToFitWidth=true //当文字超出标签宽度时,自动调整文字大小,使其不被截断
    
    // 自动换行,0表示没有行数限制 注意行高
//        label.numberOfLines=2  //显示两行文字
    
    //设置文本高亮
//        label.isHighlighted = true
    //设置文本高亮颜色
//        label.highlightedTextColor = UIColor.green
    
    
    // 富文本的显示
    //富文本设置
    let attributeString = NSMutableAttributedString(string:"welcome to hangge.com")
    //从文本0开始6个字符字体HelveticaNeue-Bold,16号
    attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 18)!,
                                 range: NSMakeRange(0,6))
    //设置字体颜色
    attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue,
                                 range: NSMakeRange(0, 3))
    //设置文字背景颜色
    attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.green,
                                 range: NSMakeRange(7,3))
    label.attributedText = attributeString
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.10.18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档