swift——富文字文字的簡單使用
如果需要一個字元前後文字顏色不一樣,也就是說一個字串分成多個部分,每個部分的屬性(顏色,字型,大小等)不一樣,那就是富文字文字 NSMutableAttributedString
直接上程式碼了
let str = "今宵杯中映著明月 物華天寶人傑地靈"
let attrStr = NSMutableAttributedString.init(string: str)
attrStr.addAttribute(NSAttributedStringKey.foregroundColor, value:UIColor.orange, range:NSRange.init(location:0, length: 8))
attrStr.addAttribute(NSAttributedStringKey.foregroundColor, value:UIColor.red, range:NSRange.init(location:9, length: 8))
label1.attributedText = attrStr
/// 僅字型和大小
label2.attributedText = NSAttributedString.init(string:"這是一串文字", attributes: [NSAttributedStringKey.backgroundColor:UIColor.cyan, NSAttributedStringKey.font:UIFont.systemFont(ofSize:28)])
/// 背景色
label3.attributedText = NSAttributedString.init(string:"這是一個有背景色的文字", attributes: [NSAttributedStringKey.backgroundColor:UIColor.green, NSAttributedStringKey.font:UIFont.systemFont(ofSize:18)])
/// 陰影
let shadow = NSShadow.init()
shadow.shadowColor = UIColor.red
shadow.shadowOffset = CGSize.init(width: 2, height: 2)
label4.attributedText = NSAttributedString.init(string:"這是一個有陰影的文字", attributes: [NSAttributedStringKey.foregroundColor:UIColor.red, NSAttributedStringKey.font:UIFont.systemFont(ofSize:18), NSAttributedStringKey.shadow: shadow])
/// 下劃線
label5.attributedText = NSAttributedString.init(string:"這是一個有下劃線的文字", attributes: [NSAttributedStringKey.foregroundColor:UIColor.purple, NSAttributedStringKey.font:UIFont.systemFont(ofSize:18), NSAttributedStringKey.underlineStyle:NSUnderlineStyle.styleSingle.rawValue])
效果圖:
轉載請註明出處,謝謝。
相關文章
- artEditor移動端富文字編輯器使用簡單介紹
- BSText - YY大神的富文字框架 YYText 的 Swift 版本框架Swift
- [Djangorestframework]-富文字編輯器的使用DjangoRESTFramework
- Swift 4.0 字串擷取,拼接,字串富文字顯示Swift字串
- js簡單富文字編輯器效果程式碼例項JS
- 簡單的文字處理
- UILable富文字UI
- SSM使用UEditor富文字編輯器SSM
- 富文字編譯器UEditor+SSM的使用編譯SSM
- Flutter 封裝:富文字 RichText 極簡封裝Flutter封裝
- 富文字編譯器編譯
- iOS 富文字屬性iOS
- Squire – 簡潔的 HTML5 富文字編輯器UIHTML
- iOS 富文字的應用(圖片與文字混編)iOS
- 富文字中的段落屬性
- iOS使用UITableView實現的富文字編輯器iOSUIView
- Swift 計算文字的sizeSwift
- 富文字編輯器初探
- WebView 顯示HTML富文字WebViewHTML
- Javascript富文字編輯器JavaScript
- 九、Vue+Element使用富文字編輯器Vue
- 【開源我寫的富文字】打造全網最勁富文字系列之技術選型
- HTML 頁面使用 wangeditor 富文字編輯器HTML
- iOS使用NSMutableAttributedString實現富文字小結iOS
- 在VueJS中使用 froala 富文字編輯器VueJS
- 關於專案中使用的富文字編輯器markdown和傳統的富文字編輯器的對比和選擇
- [Flutter package] 富文字便捷配置庫FlutterPackage
- 線上富文字編輯器初探
- 微信小程式富文字寫法微信小程式
- ProseMirror - 模組化的富文字編輯框架ROS框架
- 漂亮的 Django Markdown 富文字 app 外掛DjangoAPP
- FCKeditor富文字編輯框的配置薦
- 輕量級web富文字框——wangEditor使用手冊(4)——配置下拉選單Web
- PDF轉文字檔案的最簡單方法
- 日常筆記一:擷取富文字編輯器中的文字內容筆記
- Swift語音和文字的轉換Swift
- laravel-admin 使用wang-editor 富文字外掛Laravel
- 富文字編輯器 VUE-QUILL-EDITOR 使用教程 (最全)VueUI