Spelling in NSTextField with Swift - swift

How can I check the spelling of a NSTextField using swift? I'm already using controlTextDidChange to validate the text. This solution seems to mention casting the first responder as a NSTextView but I'm not sure that is possible with swift using coercion. I know this would be easier if I changed to a NSTextView but if possible I'd like to avoid this.

This should help you out.
// Focus TextField
phraseTextField.becomeFirstResponder()
// Enable Continous Spelling
let textView: NSTextView = (self.window!.firstResponder as! NSTextView)
textView.continuousSpellCheckingEnabled = true
Adapted from: How do I enable spell checking within an NSTextField on Mac OS X?
In other situations, it may just work better to change NSTextFields to NSTextViews. Simply use a "Text View" (search NSTextView in the Object Library) and spell check will be on by default. NSTextFields simply do not support spell check in some cases, as best I can tell.
Also consult: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/#//apple_ref/doc/uid/20000373-SW55

Related

Xcode UITest: How to assert the label has bold text?

I'm having a button to change the normal text to bold text in the label of my UIView. In UITesting, How to assert that the normal text changed to bold text when i pressed the button?
let app = XCUIApplication()
let label = app.staticTexts["myLabel"]
app.buttons["Bold"].tap()
XCTAssertEqual(label.value as! String, "Hello")
You cannot do it. As XCUITest is functional test, if you want to
assert visual requirements like font, colour, you should write a unit test insteads.
I don't think there is a way to acess UI components (or whatever members of UIKit, including UIFont) over XCUIElementAttributes protocol (what is used for passing accessibility information for XCTest environment).
However you are free to write whatever you want in accessibilityValue or accessibilityLabel properties and read them afterwards. For me it's the only way to get round XCTest limitations when needed (however it will corrupt accessibility user experience, so you should take it into account)

Scroll an NSTextView to bottom after adding text

I want to add text to an NSTextView and scroll it to the bottom like it is done in the console of Xcode. What is the best way to do that in swift 4? I have looked at other answers but they are not in swift or do not work perfectly in when converted to swift.
This is being written for an OS X app and not an iOS app and that is why I am asking about an NSTextView.
To add text, I am simply I am using this code self.logTextView.string += "\nnew text" This allows the text to also be put on a new line. If there is a better way to do this, would love to hear it.
Here is what I have tried already:
Scrolling NSTextView to bottom
NSTextView has an exact action: scrollToEndOfDocument(_ sender: Any?).
So, I suppose the following code is enough.
self.logTextView.string += "\nnew text"
self.logTextView.scrollToEndOfDocument(nil)

Caret cursor color and size

How do I change the size and colour of the flashing cursor in my textfield using my storyboard for my Mac app.
I assume I have to connect it into the view controller script (control drag) and edit some uitextfield color parameter for the cursor?
But I don't seem to be getting anywhere fast. I am working in swift 2.2 with Mac app Storyboards. Like Ulysses and Taskpaper.
In Swift 3:
Change YOURTEXTFIELD to your textfield and you are set:
(YOURTEXTFIELD.value(forKey: "textInputTraits") as AnyObject).setValue(UIColor.black, forKey: "insertionPointColor")
Documentation: https://developer.apple.com/documentation/appkit/nstextview/1449309-insertionpointcolor
Search for _drawInsertionPointInRect that's the private method that you need. Though you also need some other stuff I think... but it's all mixed into my code so I can't say for sure what it all is. Anyway search for _drawInsertionPointInRect and you'll get to some explanations.

How to check spelling entered in textview?

I have one UITextview in which user enter text. after that i take that text as a string and display it in another textview and want the words which are grammatically incorrect.
How to do that?
Use the UITextChecker class that's been available since ios 3.2: Apple Documentation
iOS system only provide spell checking while the user is inputting text. If the user choose to type in the wrong word or words, iOS cannot help. If you want to perform your own spell checking, you will have to include your own spell checking library.
GNU Aspell is a Free and Open Source spell checker designed to eventually replace Ispell. It can either be used as a library or as an independent spell checker.
Aspell Spell Checker on iPhone? was asked a few years ago here on SO. I am not sure if they ever got it working.
Use autocorrectionType property of UITextInputTraits protocol which is implemented by UITextView
#property(nonatomic) UITextAutocorrectionType autocorrectionType
Use it as below
To disable
myTxtview.autocorrectionType = UITextAutocorrectionTypeNo;
To enable
myTxtview.autocorrectionType = UITextAutocorrectionTypeYes;
In Apple Documentation
You can use Correction option under Text Input Traits in TextField Attributes using Interface Builder. It will take care of Valid English words.

Make portion of NSString italicized or bold, not whole string

How would I go about italicizing a single word in an NSString which will be displayed in a UILabel? Specifically, I don't want all text to be italicized, just one word.
Thanks!
(Edited) I meant UILabel, not UITextField.
I don't think that what you are asking to do is possible (I'd be happy to be proven wrong). However, this library (https://github.com/facebook/three20/) is a popular way to achieve the same result in a UILabel (not text field) . The library works fairly well, but does have a lot of limitations, especially on edge conditions, and of course, it comes with associated overhead.
I'd encourage you to think about other ways of achieving the same user outcome. Can Placeholder text help? How about hints next to your text field?
Good luck.
A native UILabel does not support NSAttributedString which is what is normally used to display strings with formatting. You could try an output the text your self using Core Text but I would suggest checking out FontLabel or the three-20 project mentioned by #JJ Rohrer
Use NSAttributedString... Find controllers to draw NSAttributedString,since UILabel wont support NSAttributedString
Controller for NSAttributedString