swift中UILable的使用

来源:互联网 发布:魏强斌 知乎 编辑:程序博客网 时间:2024/05/20 22:26
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.Leftlabel.font = UIFont(name: "Copperplate", size: 15.0)label.lineBreakMode = NSLineBreakMode.ByTruncatingTaillabel.adjustsFontSizeToFitWidth = true
// 行数显示,默认为1行// 多行显示label.numberOfLines = 0// 只显示2行// label.numberOfLines = 2
// 文本高亮label.highlighted = truelabel.highlightedTextColor = UIColor.blueColor()
// 阴影设置label.shadowColor = UIColor.brownColor()label.shadowOffset = CGSizeMake(2.0, 2.0)
// layer层设置label.layer.cornerRadius = 5.0label.layer.masksToBounds = truelabel.layer.borderColor = UIColor.redColor().CGColorlabel.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), (CGRectGetWidth(self.view.frame) - 10.0 * 2), 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(), range: NSMakeRange(0, 5))// 设置文字背景颜色attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(), range: NSMakeRange(0, 5))// 富文本内容显示label02.attributedText = attributeString


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












0 0
原创粉丝点击