Created
February 28, 2020 01:41
-
-
Save yotubarail/bcc350e3fb16b4e9797f9069f0ae0509 to your computer and use it in GitHub Desktop.
UIAlertControllerにUITextViewを追加
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
class ViewController: UIViewController, UITextViewDelegate { | |
@IBOutlet weak var memoTextView: ToucheEventTextView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
memoTextView.delegate = self | |
memoTextView.tag = 123 | |
} | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if isTouch(touches: touches, view: memoTextView) { | |
print("TextView touch!!") | |
} | |
} | |
func isTouch(touches: Set<UITouch>, view:UIView) -> Bool{ | |
for touch: AnyObject in touches { | |
let t: UITouch = touch as! UITouch | |
if t.view?.tag == view.tag { | |
let alertController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertController.Style.alert) | |
let margin:CGFloat = 8.0 | |
let rect = CGRect(x: margin, y: margin, width: 255, height: 150.0) | |
let customView = UITextView(frame: rect) | |
customView.backgroundColor = UIColor.white | |
customView.textColor = .black | |
customView.font = UIFont(name: "Helvetica", size: 16) | |
customView.text = memoTextView.text | |
customView.delegate = self | |
// ツールバー生成 | |
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 40)) | |
// スタイルを設定 | |
toolBar.barStyle = UIBarStyle.default | |
// 画面幅に合わせてサイズを変更 | |
toolBar.sizeToFit() | |
// 閉じるボタンを右に配置 | |
let spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil) | |
// 閉じるボタン | |
let commitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(commitButtonTapped)) | |
// スペース、閉じるボタンを右側に配置 | |
toolBar.items = [spacer, commitButton] | |
// textViewのキーボードにツールバーを設定 | |
customView.inputAccessoryView = toolBar | |
alertController.view.addSubview(customView) | |
let somethingAction = UIAlertAction(title: "メモ編集", style: UIAlertAction.Style.default, handler: {(alert: UIAlertAction!) in | |
print(customView.text!) | |
self.memoTextView.text = customView.text | |
}) | |
let cancelAction = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler: {(alert: UIAlertAction!) in print("cancel")}) | |
alertController.addAction(somethingAction) | |
alertController.addAction(cancelAction) | |
self.present(alertController, animated: true, completion: nil) | |
return true | |
} else { | |
return false | |
} | |
} | |
return false | |
} | |
@objc func commitButtonTapped() { | |
if var topController = UIApplication.shared.keyWindow?.rootViewController { | |
while let presentedViewController = topController.presentedViewController { | |
topController = presentedViewController | |
} | |
topController.view.endEditing(true) | |
} | |
} | |
} | |
class ToucheEventTextView: UITextView { | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let next = next { | |
next.touchesBegan(touches , with: event) | |
} else { | |
super.touchesBegan(touches , with: event) | |
} | |
} | |
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let next = next { | |
next.touchesEnded(touches , with: event) | |
} else { | |
super.touchesEnded(touches , with: event) | |
} | |
} | |
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let next = next { | |
next.touchesCancelled(touches, with: event) | |
} else { | |
super.touchesCancelled(touches, with: event) | |
} | |
} | |
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let next = next { | |
next.touchesMoved(touches, with: event) | |
} else { | |
super.touchesMoved(touches, with: event) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment