swift-UILabel

本文介绍如何使用SwiftUI创建UILabel并设置样式,包括文本、颜色、背景、边框等属性,同时展示了如何添加阴影效果及文本省略显示,并实现富文本效果。
 1 // Mark: 2. 创建label
 2     private func creatLabel(title: NSString)->UILabel{
 3                 /// 创建label
 4         let titleL = UILabel(frame: CGRectMake(100, 64, 200, 60))
 5         
 6         titleL.text = title as String
 7         titleL.textColor = UIColor.redColor()
 8         titleL.backgroundColor = UIColor.yellowColor()
 9         titleL.layer.cornerRadius = 10.0
10         titleL.layer.masksToBounds = true
11         titleL.textAlignment = NSTextAlignment.Center
12         titleL.font = UIFont.systemFontOfSize(16.0)
13         // 阴影
14         titleL.shadowColor = UIColor.cyanColor()
15         titleL.shadowOffset = CGSizeMake(-2, 2)
16         // 省略方式
17         titleL.lineBreakMode = NSLineBreakMode.ByTruncatingTail
18 //        titleL.adjustsFontSizeToFitWidth = true
19         titleL.numberOfLines = 2
20         
21         // 富文本
22         let attr = NSMutableAttributedString(string: "NSMutableAttributedStringNSMutableAttributedStringNSMutableAttributedString")
23         
24         attr.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(25.0), range: NSMakeRange(0, 6))
25         attr.addAttribute(NSForegroundColorAttributeName, value: UIColor.purpleColor(), range: NSMakeRange(7, 15))
26         titleL.attributedText = attr
27         
28         self.view.addSubview(titleL)
29         return titleL;
30     }

 

转载于:https://www.cnblogs.com/guangleijia/p/5498319.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值