按下键盘中的返回键时,UITextView 不记得字体样式

Posted

技术标签:

【中文标题】按下键盘中的返回键时,UITextView 不记得字体样式【英文标题】:UITextView not remembering the font styles when return key in keyboard is pressed 【发布时间】:2017-10-14 14:21:47 【问题描述】:

我在情节提要中有一个 UITextView (文本属性已属性化,并选中允许编辑属性,选中,可编辑)。当用户按下按钮时,我想“激活”粗体字体。因此,无论用户从那里输入什么,都必须以粗体显示。当按下返回键时,我想转到下一行,UITextView 应该记住所有文本的属性。问题是按回车键后,所有字符都变为粗体。

这是我的代码。

var isBoldTyping: Bool = false

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool 
    print("text: \(text)", range)
    if text == "\n" 
        textView.text.append("\n")


        if isBoldTyping 
            //textView.typingAttributes = [NSAttributedStringKey.font.rawValue: regularText]
            isBoldTyping = false
        

    

    if isBoldTyping 
        textView.textStorage.beginEditing()
        textView.textStorage.addAttributes([NSAttributedStringKey.font: boldText], range: range)
        print("Atttributed adding")
        textView.textStorage.endEditing()
     else 
        textView.textStorage.beginEditing()
        textView.textStorage.addAttributes([NSAttributedStringKey.font: regularText], range: range)
        print("Atttributed adding")
        textView.textStorage.endEditing()
    

    return true

我想要实现的另一个示例是 *** 编辑效果。当我按下“”图标时,我处于“可编码模式”。当在键盘中按下 enter 时,它会恢复正常。

regularText和boldText属性就是14号的系统字体。

【问题讨论】:

【参考方案1】:

如果我清楚地了解您想要实现的目标,您只需要typingAttributes。当\n 出现在replacementTexttypingAttributes 中时,您需要禁用粗体。我给你写一个小例子

class ViewController: UIViewController, UITextViewDelegate 

    let textView = UITextView()
    let boldButton = UIButton(type: .system)

    override func viewDidLoad() 
        super.viewDidLoad()

        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.delegate = self
        textView.typingAttributes = [NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 14.0)]

        view.addSubview(textView)

        view.addConstraint(NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: textView, attribute: .leading, multiplier: 1.0, constant: 0.0))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: textView, attribute: .top, multiplier: 1.0, constant: 0.0))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: textView, attribute: .bottom, multiplier: 1.0, constant: 0.0))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: textView, attribute: .trailing, multiplier: 1.0, constant: 0.0))

        boldButton.setTitle("BOLD", for: .normal)
        boldButton.addTarget(self, action: #selector(toggleBold), for: .touchUpInside)
        boldButton.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(boldButton)
        view.addConstraint(NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: boldButton, attribute: .leading, multiplier: 1.0, constant: 0.0))
        view.addConstraint(NSLayoutConstraint(item: boldButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 64.0))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: boldButton, attribute: .bottom, multiplier: 1.0, constant: 0.0))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: boldButton, attribute: .trailing, multiplier: 1.0, constant: 0.0))

    

    @objc func toggleBold() 
        boldButton.isSelected = !boldButton.isSelected
        if boldButton.isSelected 
            textView.typingAttributes = [NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: 14.0)]
         else 
            textView.typingAttributes = [NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 14.0)]
        
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool 
        if text.contains("\n") && boldButton.isSelected 
            toggleBold()
        
        return true
    


【讨论】:

以上是关于按下键盘中的返回键时,UITextView 不记得字体样式的主要内容,如果未能解决你的问题,请参考以下文章

如何知道 iPhone 键盘中按下的键

UITextView:输入退格键时字符数不正确

按下返回键时如何通过多个文本字段输入进行选项卡?

xcode中的UITextView问题

android:按下完成键时软键盘执行动作

觅求高手.java键盘事件.