【发布时间】:2017-05-19 07:58:52
【问题描述】:
我有一个 UIView,我将在其中为按钮编写操作。在 XIB 文件中,我在键盘上方有一个带有这些按钮的栏。如何将这些按钮连接到具有 UITextView 的 ViewController 以及单击按钮时的操作应在 UITextView 中完成(例如:当我单击“粗体”按钮时,UITextView 中的选定文本应为粗体) [![在此处输入图片描述][2]][2]
import UIKit
protocol NoteViewDelegate {
func didUpdateNoteWithTitle(newTitle : String, andBody newBody : String)
}
class NewNoteViewController: UIViewController , UITextViewDelegate {
var delegate : NoteViewDelegate?
@IBOutlet weak var textBody: UITextView!
var strBodyText : String!
override func viewDidLoad() {
super.viewDidLoad()
self.textBody.text = self.strBodyText
self.textBody.becomeFirstResponder()
self.textBody.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(NewNoteViewController.updateTextView(notification:)) , name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NewNoteViewController.updateTextView(notification:)) , name: Notification.Name.UIKeyboardWillHide, object: nil)
textBody.inputAccessoryView = Bundle.main.loadNibNamed("CustomAccessoryView", owner: self, options: nil)?.first as? UIView
}
func updateTextView (notification:Notification) {
let userInfo = notification.userInfo!
let keyboardEndFrameScreenCoordinates = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let keyboardEndFrame = self.view.convert(keyboardEndFrameScreenCoordinates, to: view.window)
if notification.name == Notification.Name.UIKeyboardWillHide {
textBody.contentInset = UIEdgeInsets.zero
}else{
textBody.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardEndFrame.height, right: 0)
textBody.scrollIndicatorInsets = textBody.contentInset
}
textBody.scrollRangeToVisible(textBody.selectedRange)
}
【问题讨论】:
标签: ios swift2 swift3 xib bold