Swift UIlabel 的高级用法,文本显示表情,图片
生活随笔
收集整理的這篇文章主要介紹了
Swift UIlabel 的高级用法,文本显示表情,图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
可以定義一個UIlabel的擴展代碼如下
class CPSPFMarkLabel: UILabel {convenience init(text: String) {self.init(frame:CGRect.zero)let markLabelText = " 清明時節雨紛紛?" + textself.textAlignment = .centerself.textColor = UIConstants.AppFontColorself.font = UIConstants.DefaultFont15self.backgroundColor = UIConstants.AppWhiteColorlet markAttribute = NSMutableAttributedString(string: markLabelText)markAttribute.addAttribute(NSAttributedStringKey.foregroundColor, value:UIConstants.AppRedColor, range: NSRange(location: 6,length: text.count))//以上是富文本顯示let arkattch = NSTextAttachment() //定義一個attachmentmarkattch.image = UIImage(named: "image_timing")//初始化圖片markattch.bounds = CGRect(x: 0, y: -1, width: 15, height: 15) //初始化圖片的 boundslet markattchStr = NSAttributedString(attachment: markattch) // 將attachment 加入富文本中markAttribute.insert(markattchStr, at: 0)// 將markattchStr 加入原有文字的某個位置self.attributedText = markAttribute} }
總結
以上是生活随笔為你收集整理的Swift UIlabel 的高级用法,文本显示表情,图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux每日命令(17):which命
- 下一篇: 蜂鸟智游大数据:“人在囧途”的春运,航空