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
注意:多行显示时,参数numberOfLines,font,frame中的height,三者是有关联的。如果height小于fone,或小于numberOfLines的行数时,则会显示不全。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。