2017-10-14 68 views
0

我在故事板中有一个UITextView(文本属性是属性的,并且允许编辑属性,选中,可编辑选中)。当用户按下按钮时,我想要“激活”粗体字体。所以,无论用户输入什么,都必须加粗。当按下回车键时,我想要转到下一行,并且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 
} 

我试图实现的另一个例子是stackoverflow编辑效果。当我按“{}”图标时,我处于“可编码模式”。按下键盘后,它会恢复正常。

的regularText和boldText性质只是系统字体大小为14

回答

0

只有你需要的是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 
    } 

} 
相关问题