let label = UILabel(frame:CGRect(x:100,y:100,width:100,height:100));
label.text = "你好";
label.textColor = UIColor.red;
//字体剧中,left左,right右
label.textAlignment = .center;
label.backgroundColor = UIColor.white;
//文字阴影
//灰色阴影
label.shadowColor = UIColor.gray;
//阴影便宜量
label.shadowOffset = CGSize(width:1.5,height:1.5);
//字体设置
label.font = UIFont(name:"Zapfino",size:20);
//文字过长时的省略方式
label.lineBreakMode = .byTruncatingTail//隐藏尾部并且显示省略号
//中间显示省略号
label.lineBreakMode = .byTruncatingMiddle;
//文字大小自适应标签宽度
label.adjustsFontSizeToFitWidth = true;
//显示多行文字
label.numberOfLines = 2;
label.isHighlighted = true;
//设置文本高度颜色
label.highlightedTextColor = UIColor.green;
self.view .addSubview(label);
//富文本设置
let attributeString = NSMutableAttributedString(string:"你好 世界 !");
//个字符字体HelveticaNeue-Bold,16号
attributeString.addAttribute(NSAttributedStringKey.font,value:UIFont(name:"HelveticaNeue-Bold",size:16), range:NSMakeRange(0,6))
//设置字体颜色
attributeString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.blue,
range: NSMakeRange(0, 3))
//设置文字背景颜色
attributeString.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.green,
range: NSMakeRange(3,3))
label.attributedText = attributeString
swift Lable标签
最新推荐文章于 2024-05-11 09:50:35 发布
