Link in an Editable NSTextView (like Apple's Notes) - swift

I have an NSTextView that is initially set as read-only like this:
taskDescription.isEditable = false
I set a click gesture recognizer in viewDidLoad() on it like this so that when the user clicks the field, it gets set as editable.
override func viewDidLoad() {
super.viewDidLoad()
let clickGesture = NSClickGestureRecognizer(target: self, action: #selector(setDescriptionEditState))
clickGesture.numberOfClicksRequired = 1
taskDescription.addGestureRecognizer(clickGesture)
}
And here is the function called on click:
#objc func setDescriptionEditState(){
//Make it editable
taskDescription.isEditable = true
//Don't show automatic link detect while editing (I want plain text)
taskDescription.checkTextInDocument(nil)
//Put the cursor in the NSTextView
taskDescription.window?.makeFirstResponder(taskDescription)
}
Then when I load an NSAttributedString into it with a link in it, it shows up fine:
Last of all, I implement this delegate method for NSTextView:
func textView(_ textView: NSTextView, clickedOnLink link: Any, at charIndex: Int) -> Bool {
print(link) //<-- Never called
}
What's happening is the clickGesture is getting called instead of the link click. I know this because if I manually set the NSTextView to isEditable = false then the link works fine.
How can I allow the link click to happen while still allowing the user to click on the NSTextView to switch it into edit mode?
--- Update ---
I set the content of this NSTextView with an NSAttributedString. When logged to the console, it looks like this:
I want {
NSColor = "...";
NSFont = "...";
}a big link{
NSColor = "...";
NSFont = "...";
NSLink = "google.com";
} here.{
NSColor = "...";
NSFont = "...";
}
So the link is specified with an NSLink.

The key is to override the function "touchesBegan" and implement your own logic.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
guard let location = touch?.location(in: self.view) else { return }
if !popupContainer.frame.contains(location) {
goodTouch = true
} else {
goodTouch = false
}
}
The variable "goodTouch" is established as a class property. You can then check the status of this variable at the beginning of the functions in question and then route your application appropriately from there. (This was specifically adapted from an iOS project I wrote, but should be the same for Cocoa). Make sure if you implement this, you reset the "goodTouch" variable to false after inspecting it to be ready for the next round of touches.
EDIT
I think this is a more appropriate response to your specific example, but I didn't want to delete the code from above:
override func mouseDown(with event: NSEvent) {
let textField = NSTextField() //this is for demo only...reference your actual object
let location = event.locationInWindow
let cgPoint = CGPoint(x: location.x, y: location.y)
if textField.bounds.contains(cgPoint) {
//fire the link here, over ride the click gesture recognizer
} else {
//change the editable style of the text field
}
}

Related

Swift 5 - Mac OS - NSTrackingArea overlapping views

Currently I have a little issue when it comes to buttons(NSButton) which have a tracking area and views(NSView overlay) above these buttons, this is my setup:
Custom button:
class AppButton: NSButton {
override func updateTrackingAreas() {
super.updateTrackingAreas()
let area = NSTrackingArea(
rect: self.bounds,
options: [.mouseEnteredAndExited, .activeAlways],
owner: self,
userInfo: nil
)
self.addTrackingArea(area)
}
override func mouseEntered(with event: NSEvent) {
NSCursor.pointingHand.set()
}
override func mouseExited(with event: NSEvent) {
NSCursor.arrow.set()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
An instance of this button class is used i a very basic NSView.
When I hover over the button, the cursor changes correctly.
When I click the button a new overlay(NSView) is opened above the button...
This is where the problem starts:
When I hover over the overlay where my button is placed, the cursor still changes...
I did not know that a NSTrackingArea is going through all views..
How can i solve this issue?
Can I set any property on the overlay(NSView) to somehow disable the NSTrackingArea on the button?
Thanks!!
You can subclass NSView and add local monitoring for events. Check if the event has occurred over the view and if true return nil. This will avoid propagating the events being monitored. If the event is outside the view frame you can propagate it normally returning the event monitored.
class CustomView: NSView {
override func viewWillMove(toSuperview newSuperview: NSView?) {
super.viewWillMove(toSuperview: newSuperview)
wantsLayer = true
layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
layer?.borderWidth = 1
NSEvent.addLocalMonitorForEvents(matching: [.mouseEntered, .mouseExited, .leftMouseDown]) { event in
if self.frame.contains(event.locationInWindow) {
// if cursor is over the view just return nil to do not propagate the events
return nil
}
return event
}
}
}
If you are trying to stop mouse events from a viewController, add this code in viewDidLoad
NSEvent.addLocalMonitorForEvents(matching: [.mouseEntered, .mouseExited, .leftMouseDown]) { event in
if self.view.frame.contains(event.locationInWindow) {
return nil
}
return event
}

Swift - How to get the sender tag for an array of buttons using UILongPressGestureRecognizer?

I have buttons in the storyboard that I put into a Referencing Outlet Collection. I'm using UITapGestureRecognizer and UILongPressGestureRecognizer for all of these buttons, but how can I print exactly which button gets tapped? Bellow is what I tried but doesn't work. I get an error that says "Value of type 'UILongPressGestureRecognizer' has no member 'tag'." I'm trying to build the button grid for the Minesweeper game. Thank you for your help.
class ViewController: UIViewController {
#IBOutlet var testButtons: [UIButton]! // There are 100 buttons in this array
override func viewDidLoad() {
super.viewDidLoad()
let testButtonPressed = UILongPressGestureRecognizer(target: self, action: #selector(testPressed))
testButtonPressed.minimumPressDuration = 0.5
// These indexes are just to test how to recognize which button gets pressed
testButtons[0].addGestureRecognizer(testButtonPressed)
testButtons[1].addGestureRecognizer(testButtonPressed)
}
#objc func testPressed(_ sender: UILongPressGestureRecognizer) {
print("Test button was pressed")
print(sender.tag) // THIS DOESN'T WORK, BUT CONCEPTUALLY THIS IS WHAT I WANT TO DO
}
This error occurs because UILongPressGestureRecognizer object has no tag property
You can access sender's button in a way like that:
#objc func testPressed(_ sender: UILongPressGestureRecognizer) {
guard let button = sender.view as? UIButton else { return }
print(button.tag)
}
I think that the best solution to handle button's actions is to add #IBAction
(you can add it like #IBOutlet with a minor change - set Action connection type)
And then in #IBAction block you cann access all button properties (like tag and others)
instead of using gesture I think it would be better to use #IBAction and connect the buttons With it here is a small example
UILongPressGestureRecognizer which is a subclass of UIGestureRecognizer, can be used only once per button or view. Because UILongPressGestureRecognizer has only a single view property. In your code, it will always be testButtons[1] calling the testPressed action. So you have to first modify the viewDidLoad code like this :-
for button in testButtons {
let testButtonPressed = UILongPressGestureRecognizer(target: self, action: #selector(testPressed))
testButtonPressed.minimumPressDuration = 0.5
button.addGestureRecognizer(testButtonPressed)
button.addGestureRecognizer(testButtonPressed)
}
Then you can access the button from testPressed like this (I hope you've already set the tag in the storyboard) :-
#objc func testPressed(_ sender: UILongPressGestureRecognizer) {
if sender.state == .began {
if let button = sender.view as? UIButton {
print(button.tag)
}
}
}
You need to set tags before pressing!
On the viewDidLoad() method you must add something like:
testButtons.enumerated().forEach {
let testButtonPressed = UILongPressGestureRecognizer(target: self, action: #selector(testPressed))
testButtonPressed.minimumPressDuration = 0.5
$0.element.addGestureRecognizer(testButtonPressed)
$0.element.tag = $0.offset
}
And when the long press is receiving you need to get a tag from view not from the sender!
print(sender.view?.tag)
Since a gesture recognizer should only be associated with a single view, and doesn't directly support using an identity tag to match it with buttons. When creating an array of buttons for a keyboard, with a single gesture response function, I found it easier to use the gesture recognizer "name" property to identify the associated button.
var allNames: [String] = []
// MARK: Long Press Gesture
func addButtonGestureRecognizer(button: UIButton, name: String) {
let longPrssRcngr = UILongPressGestureRecognizer.init(target: self, action: #selector(longPressOfButton(gestureRecognizer:)))
longPrssRcngr.minimumPressDuration = 0.5
longPrssRcngr.numberOfTouchesRequired = 1
longPrssRcngr.allowableMovement = 10.0
longPrssRcngr.name = name
allNames.append(name)
button.addGestureRecognizer(longPrssRcngr)
}
// MARK: Long Key Press
#objc func longPressOfButton(gestureRecognizer: UILongPressGestureRecognizer) {
print("\nLong Press Button => \(String(describing: gestureRecognizer.name)) : State = \(gestureRecognizer.state)\n")
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
if let keyName = gestureRecognizer.name {
if allNames.contains(keyName) {
insertKeyText(key: keyName)
} else {
print("No action available for key")
}
}
}
}
To implement, call the addButtonGestureRecognizer function after creating the button, and provide a name for the button (I used the button text) e.g.
addButtonGestureRecognizer(button: keyButton, name: buttonText)
The button name is stored in the "allNames" string array so that it can be matched later in "longPressOfButton".
When the button name is matched in the "longPressOfButton" response function, it passes it to "addKeyFunction" where it is processed.

NSTextField force becomeFirstResponder removes text

Using NSTextField on Custom NSTableCellView.
When the user presses anywhere on the cell, I would like NSTextField to become the first responder.
When I do, the text disappears, and the user needs to press any keyboard key for it to come back, how to disable this behavior?
Code:
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
if (event.clickCount == 2) {
self.beginEditing()
} else {
self.endEditing()
}
}
private func beginEditing() {
self.lblTitle.isEditable = true
self.window?.makeFirstResponder(self.lblTitle)
self.lblTitle.backgroundColor = Colors.clear
self.lblTitle.borderColor = Colors.clear
}
private func endEditing() {
self.lblTitle.isEditable = false
self.lblTitle.backgroundColor = Colors.red
}
Removing this lines solved it
self.lblTitle.backgroundColor = Colors.clear
self.lblTitle.borderColor = Colors.clear

Where do I call the Subclass? (reselectable UISegmentControl Swift 3)

I have a subclass for a UISegmentedControl which is called SegControl in a Cocoa Touch Class.
class SegControl: UISegmentedControl {
override var = "test"
}
And now I have a UISegmentControl in my ViewController what do I have to do that this conforms to my custom class?
EDIT:
Full code for a Reselectable UISegmentControl for Swift 3
class ReselectableSegmentedControl: UISegmentedControl {
#IBInspectable var allowReselection: Bool = true
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let previousSelectedSegmentIndex = self.selectedSegmentIndex
super.touchesEnded(touches, with: event)
if allowReselection && previousSelectedSegmentIndex == self.selectedSegmentIndex {
if let touch = touches.first {
let touchLocation = touch.location(in: self)
if bounds.contains(touchLocation) {
self.sendActions(for: .valueChanged)
}
}
}
}
}
UISegmentControl:
var savedSegment: Int = -1
#IBAction func segmentChanged(_ sender: ReselectableSegmentedControl) {
if (sender.selectedSegmentIndex == savedSegment) {
sender.selectedSegmentIndex = UISegmentedControlNoSegment
savedSegment = UISegmentedControlNoSegment
}
else {
savedSegment = sender.selectedSegmentIndex
}
Your question isn't entirely clear but to start you need to replace any references to UISegmentedControl with SegControl. This includes updating any references in code as well as changing the class name of the object in any storyboard or xib.
You are also going to need to add the proper init methods to your subclass so you can create instances of it.
And of course you need to added whatever additional code that makes having this subclass useful.
add a default UISegmentControl in your layout builder
then click on it and in the side menu under identity inspector -> class
write your class name SegControl

Swift checking textfield input live

I've been struggling to find how to check the input of a textfield as a user is typing.
if a user types a word, it should change a label and an image according to some defined rules.
my code is working, but I'm always a step behind. (as it reads the content always before the next character is entered.
If it just to check the length I could use countElements(textfield) + 1, but I want it to also show that a user cannot use certain characters as they are typing, therefore that would not work for checking undesired characters.
I am assuming the function I am using is not the right one "shouldChangeCharacters". So I am a bit lost as to what to use. Is there a way to read a println or NSLog command to return to an outlet?
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let passwordcheck = UserPasswordTextField.text
if UserPasswordTextField.isFirstResponder() {
if isValidPassword(passwordcheck) {
PWimg.image = UIImage(named: "passwordapprovedicon")
} else if passwordcheck.isEmpty {
PWimg.image = UIImage(named: "passwordiconwrong")
} else {
PWimg.image = UIImage(named: "passwordiconwrong")
}
}
func isValidPassword(testStr2:String) -> Bool {
println("validate password: \(testStr2)")
let passwordRegEx = "[A-Z0-9a-z._%+-:/><#]{6,30}"
if let passwordTest = NSPredicate(format: "SELF MATCHES %#", passwordRegEx) {
return passwordTest.evaluateWithObject(testStr2)
}
return false
Listen for UIControlEventEditingChanged events from the text field. Register for them either with
the Interface Builder: drag from the text field to the file, and select the action type "Editing Changed"
the following code:
override func viewDidLoad() {
super.viewDidLoad()
// ...
textField.addTarget(self, action:"edited", forControlEvents:UIControlEvents.EditingChanged)
}
func edited() {
println("Edited \(textField.text)")
}
Updated for swift 3:
override func viewDidLoad() {
super.viewDidLoad()
//...
textField.addTarget(self, action: #selector(textFieldDidChange), for:.editingChanged)
}
func textFieldDidChange(){
print(textField.text)
}
Updated for swift 4.2: just add #objc to func textFieldDidChange()
override func viewDidLoad()
{
super.viewDidLoad()
textField.addTarget(self,
action : #selector(textFieldDidChange),
for : .editingChanged)
}
#objc func textFieldDidChange()
{ print(textField.text ?? "Doh!") }