forked from SeedLabIO/TextFieldExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
77 lines (62 loc) · 2.87 KB
/
ViewController.swift
File metadata and controls
77 lines (62 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// ViewController.swift
// TextFieldExample
//
// Created by LiangAlen on 7/15/16.
// Copyright © 2016 seedlab. All rights reserved.
//
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var textField: NSTextField!
@IBOutlet var textView: NSTextView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
textField.delegate = self
textView.delegate = self
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}
extension ViewController: NSControlTextEditingDelegate {
// https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSControlTextEditingDelegate_Protocol/#//apple_ref/occ/intfm/NSControlTextEditingDelegate/control:textView:doCommandBySelector:
func control(control: NSControl, textView: NSTextView, doCommandBySelector commandSelector: Selector) -> Bool {
print("Selector method inside text field: \(commandSelector)")
if commandSelector == #selector(insertNewline(_:)) {
if let modifierFlags = NSApplication.sharedApplication().currentEvent?.modifierFlags
where (modifierFlags.rawValue & NSEventModifierFlags.ShiftKeyMask.rawValue) != 0 {
print("Shift-Enter detected.")
} else {
print("Enter detected.")
}
// https://developer.apple.com/library/prerelease/content/qa/qa1454/_index.html
// If we want to insert new line instead of default action, which will complete editing.
textView.insertNewlineIgnoringFieldEditor(self)
return true
}
// * return true: Ignore system default behavior.
// * return false: Let system to execute its default implementation for the selector.
return false
}
}
extension ViewController: NSTextFieldDelegate { }
extension ViewController: NSTextViewDelegate {
// https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSTextViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/NSTextViewDelegate/textView:doCommandBySelector:
func textView(textView: NSTextView, doCommandBySelector commandSelector: Selector) -> Bool {
print("Selector method inside text view: \(commandSelector)")
if commandSelector == #selector(insertNewline(_:)) {
if let modifierFlags = NSApplication.sharedApplication().currentEvent?.modifierFlags
where (modifierFlags.rawValue & NSEventModifierFlags.ShiftKeyMask.rawValue) != 0 {
print("Shift-Enter detected.")
} else {
print("Enter detected.")
}
}
// * return true: Ignore system default behavior.
// * return false: Let system to execute its default implementation for the selector.
return false
}
}