Created
October 23, 2020 09:33
-
-
Save yosshi4486/ff37ffee79886fd2beabde375f65ba9a to your computer and use it in GitHub Desktop.
CustomNewLineTextView is able to make new line by shift + enter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CustomNewLineTextView.swift | |
// LifeList | |
// | |
// Created by seijin4486 on 2020/10/23. | |
// | |
import UIKit | |
/// Shift + Enterで改行可能なTextView | |
/// | |
/// Shift + EnterをUIKeyCommandで奪って改行処理をさせるので、該当のコマンドに関してはデリゲートメソッドが呼ばれない。 | |
class CustomNewLineTextView: UITextView { | |
override var keyCommands: [UIKeyCommand]? { | |
// キーボードへのinputは、returnなので"\r". うっかり"\n"としてしまう事があるが、それは押下したキーではないため. | |
return [UIKeyCommand(input: "\r", modifierFlags: .shift, action: #selector(newLine(sender:)))] | |
} | |
@objc func newLine(sender: UIKeyCommand) { | |
// プログラム経由のテキスト更新はデリゲートメソッドは呼ばれない | |
insertText("\n") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment