swift富文本 swift一个label不同颜色字体

这篇博客介绍了如何在Swift中实现UILabel、UITextField及UIButton等组件的富文本显示,使得文字能呈现不同颜色。文章展示了具体的效果,并提供了一个适用的示例。
摘要由CSDN通过智能技术生成
  • 此方法适用于textFiled的占位字符
  • textView
  • UILabel
  • UIButton需要文字的地方都可以
func FuWenBen() -> NSMutableAttributedString {
        
        //定义富文本即有格式的字符串
        let attributedStrM : NSMutableAttributedString = NSMutableAttributedString()
        
        //        左上角星星
        let xingxing = NSAttributedString.init(string: "*", attributes: [NSAttributedString.Key.backgroundColor : UIColor.black , NSAttributedString.Key.foregroundColor : UIColor.red , NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 20) ,NSAttributedString.Key.baselineOffset : (20)])
        
        let wo = NSAttributedString.init(string: "我", at
好的,下面是一个使用Swift语言编写的富文本编辑器用户界面的示例: ```swift import UIKit class RichTextEditorViewController: UIViewController, UITextViewDelegate { var textView: UITextView! var toolbar: UIToolbar! override func viewDidLoad() { super.viewDidLoad() // 设置文本视图 textView = UITextView(frame: view.bounds) textView.autoresizingMask = [.flexibleWidth, .flexibleHeight] textView.delegate = self view.addSubview(textView) // 设置工具栏 toolbar = UIToolbar(frame: CGRect(x: 0, y: view.bounds.height - 44, width: view.bounds.width, height: 44)) toolbar.autoresizingMask = [.flexibleWidth, .flexibleTopMargin] view.addSubview(toolbar) // 添加工具栏按钮 let boldButton = UIBarButtonItem(title: "B", style: .plain, target: self, action: #selector(boldButtonTapped)) let italicButton = UIBarButtonItem(title: "I", style: .plain, target: self, action: #selector(italicButtonTapped)) let underlineButton = UIBarButtonItem(title: "U", style: .plain, target: self, action: #selector(underlineButtonTapped)) let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) toolbar.items = [boldButton, italicButton, underlineButton, flexibleSpace] // 设置文本视图的样式 let font = UIFont.systemFont(ofSize: 16) let attributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: font] textView.typingAttributes = attributes } // 处理工具栏按钮的点击事件 @objc func boldButtonTapped() { let range = textView.selectedRange let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.font] = UIFont.boldSystemFont(ofSize: 16) textView.textStorage.addAttributes(newAttributes, range: range) } @objc func italicButtonTapped() { let range = textView.selectedRange let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.font] = UIFont.italicSystemFont(ofSize: 16) textView.textStorage.addAttributes(newAttributes, range: range) } @objc func underlineButtonTapped() { let range = textView.selectedRange let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.underlineStyle] = NSUnderlineStyle.single.rawValue textView.textStorage.addAttributes(newAttributes, range: range) } // UITextViewDelegate方法 func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { // 处理删除样式 if text == "" { let existingAttributes = textView.textStorage.attributes(at: range.location, effectiveRange: nil) var newAttributes = existingAttributes newAttributes[NSAttributedString.Key.font] = UIFont.systemFont(ofSize: 16) newAttributes[NSAttributedString.Key.underlineStyle] = NSUnderlineStyle.none.rawValue textView.textStorage.addAttributes(newAttributes, range: range) } return true } } ``` 这个示例代码包括一个文本视图和一个工具
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值