加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift中UILable的使用

发布时间:2020-12-14 06:42:48 所属栏目:百科 来源:网络整理
导读:let text = "swift中使用label。label在swift中的使用方法。swift中使用label。label在swift中的使用方法。swift中使用label。label在swift中的使用方法。" // 实例化let label:UILabel = UILabel(frame: CGRectMake(10.0,10.0,(CGRectGetWidth(self.view.fr
let text = "swift中使用label。label在swift中的使用方法。swift中使用label。label在swift中的使用方法。swift中使用label。label在swift中的使用方法。"
// 实例化
let label:UILabel = UILabel(frame: CGRectMake(10.0,10.0,(CGRectGetWidth(self.view.frame) - 10.0 * 2),60.0))
// 加到父视图
self.view.addSubview(label)
// 内容
label.text = text
// 字体属性设置
label.textColor = UIColor.blackColor()
label.textAlignment = NSTextAlignment.Left
label.font = UIFont(name: "Copperplate",size: 15.0)
label.lineBreakMode = NSLineBreakMode.ByTruncatingTail
label.adjustsFontSizeToFitWidth = true
// 行数显示,默认为1行
// 多行显示
label.numberOfLines = 0
// 只显示2行
// label.numberOfLines = 2
// 文本高亮
label.highlighted = true
label.highlightedTextColor = UIColor.blueColor()
// 阴影设置
label.shadowColor = UIColor.brownColor()
label.shadowOffset = CGSizeMake(2.0,2.0)
// layer层设置
label.layer.cornerRadius = 5.0
label.layer.masksToBounds = true
label.layer.borderColor = UIColor.redColor().CGColor
label.layer.borderWidth = 1.0
// 其他属性
label.backgroundColor = UIColor.greenColor()
label.hidden = false

// 富文本属性
let label02:UILabel = UILabel(frame: CGRectMake(10.0,(CGRectGetHeight(label.frame) + CGRectGetMaxY(label.frame) + 10.0),40.0))
self.view.addSubview(label02)
label02.backgroundColor = UIColor.yellowColor()
label02.font = UIFont(name: "Thonburi",size: 12.0)

let text00 = "label的用法"
// 定义富文本
let attributeString = NSMutableAttributedString(string: text00)
// 从文本0开始6个字符字体HelveticaNeue-Bold,16号
attributeString.addAttribute(NSFontAttributeName,value: UIFont(name: "HelveticaNeue-Bold",size: 20.0)!,range: NSMakeRange(0,5))
// 设置字体颜色
attributeString.addAttribute(NSForegroundColorAttributeName,value: UIColor.blueColor(),5))
// 设置文字背景颜色
attributeString.addAttribute(NSBackgroundColorAttributeName,value: UIColor.greenColor(),5))
// 富文本内容显示
label02.attributedText = attributeString


注意:多行显示时,参数numberOfLinesfontframe中的height,三者是有关联的。如果height小于fone,或小于numberOfLines的行数时,则会显示不全。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读