NSTextField wrong cursor until focused - objective-c

I am able to set clickable links on NSTextField elements, using NSAttributedString. The text shows correctly (blue coloured, underlined) as soon as the view appears.
However, the cursor shows on hover is the editing (I-Beam) cursor, until first click (anywhere) on the NSTextField. Once it is clicked, when hovering over the "hyperlinked" part of the text, the hand point cursor is shown as expected.
I was able to subclass NSTextField and override resetCursorRects, however, this allows me changing the cursor for the entire NSTextField bound, where I want it to show only when hovering over the hyperlinked parts of the text (Or to use cursorRects, but that seems as an overkill, to iterate on the (already iterated on creation) entire text).
I also tried setting the NSCursor attribute of the attributed string, but that did not solve the issue.

This is not directly supported by NSTextField, I believe. It starts working when you click in the textfield because at that point the field editor for the textfield is created; the field editor is an NSTextView, and that does what you want it to do. So you basically have two choices. You can (1) do the easy thing and use NSTextView instead of NSTextField (is there any particular reason why you can't?), or (2) you can set up the right cursor rects yourself in resetCursorRects by looping through the rects for the character ranges that have the link attribute set, but this means setting up a layout manager and so forth – work that is all done for you if you use NSTextView. A third possibility is to make the textfield's field editor come up right away, before the user clicks – in other words, make the textfield be the first responder. But that only works if you have only one such textfield in your window, and if nothing else in the window ever wants to be the first responder, so it is not a very satisfactory fix, probably.

Related

NSTextView no cursor if it's empty

Working with series of NSTextView, connected with a single NSLayoutManager. Very common configuration. Every time user hits Enter, new NSTextView created. In other words, every paragraph is in its own NSTextView instance.
But it's been days I'm struggling with this issue: if last NSTextView in chain is empty (say, user just hit Enter and moved cursor back somewhere in previous text view), then the cursor disappears when come back in this empty NSTextView. If I click by mouse, I have to click twice in the empty text view, so the cursor appears. If I move the cursor with arrow keys, then, again, I have to press an arrow key twice.
I've tried update cursor, but nothing happens. This issue is happens if text view is empty and the last one in series. Where should I find the answer? Searched this forum, Apple docs, googled it — nothing.
create first text view
let textContainer = NSTextContainer(containerSize: containerSize)
layoutManager.addTextContainer(textContainer)
let textView = NSTextView(frame: frame, textContainer: textContainer)
someView.addSubview(textView)
create second text view
let anotherTextContainer = NSTextContainer(containerSize: containerSize)
layoutManager.addTextContainer(anotherTextContainer)
let anotherTextView = NSTextView(frame: anotherFrame, textContainer: anotherTextContainer)
someView.addSubview(anotherTextView)
Now run, type something and hit Enter. Now, if you move the cursor up and then back down, it won't show up. You have to press down key again, then the cursor will show up. Same with mouse: you'll have to do left click for two times and only after that the cursor will show up in the second text view.
Here's the example: https://www.dropbox.com/s/47domcy0nuisncc/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202014-06-28%2013.37.58.png
Here's a sample project: https://www.dropbox.com/s/aw01oo0faajr4rn/Test.zip. I made it from scratch, when you hit Enter key, it places a second text view and you can see, that issue occurs even in a new clean projects.
It looks like a bug of Apple engineers. Found the same behavior in other apps.

NSPopUpButton in NSToolbar such annoying

Problem solved!:
Just check the "Unified Title And Toolbar" option of the NSWindow and the 1pixel-down problem goes away!
To change the toolbar height just select the Toolbar Item - Custom View and change size in the Size inspector.
==============================
If you know Xcode 5s layout than you should recognise this:
I want to build it for my own. So I dragged a Toolbar in the Window and added a NSPopUpButton. Then I changed the PopUp Button Cell Style to Radio and turned off the Arrows. So far so good.
The first thing I noticed is that the Toolbars has different heights. Does anybody know how to change this behaviour (without subclassing NSToolbar)?
The second and more annoying thing I noticed is that if I choose an Item from the PopUp Button the Image for the NSMenuItem move 1 pixel down.
EDIT: Xcode NSMenuItems don't move 1pixel down
Any suggestions about that thing?
NSToolbar, sadly, can’t really be subclassed. It’s a poorly-written class that tries to be very “magic,” so it’s not even a subclass of NSView—you can’t control how it draws at all, it creates a private view.
You can set its “sizeMode” but I assume you’ve already done that and found that the number of pixels high isn’t what you want.
The easiest thing to do is just leave space for your widgets at the top of your window (above the document content) and have autolayout position your buttons for you. (I haven’t been able to use a real NSToolbar in years because of its limitations.)
As for the popUp menu being mis-aligned with the button: where the menu draws is basically hard-coded, so if you use a button style that NSPopUpButton doesn't expect then the menu will be offset some.
If you’ve already tried just unchecking the “draws border” flag on a default-style NSPopUpButton (one fresh off the palette), There are two solutions for to try: One is to keep trying different buttonStyles that look correct to your eye until you find one that’s not offset. Two is to leave the buttonStyle do the default for NSPopUpButtons but subclass the buttonCell and have it not draw the border (but still leave room for it).

Resetting the selection cursor in a UITextView

I've looked at similarly titled questions, such as :
UITextView disabling text selection
but that wasn't quite what I was trying to achieve.
My app has a UITextView that only requires vertical scrolling. When holding down a touch, the cursor obviously appears at the point of the touch. However, when I change the text of the view, the cursor is always put at the end of the UITextView, which of course scrolls my text all the way to the bottom. I've tried setting the selected range, but even with the editable set to NO, this causes the keyboard to popup. I want my view to be able to display my text and vertically scroll, and to be able to reset its scroll position when I change the text. If I could achieve this and not be able to select, copy, paste, edit, etc. I would be perfectly content.
The last time I tried to deal with this I gave up and ended up using a UILabel, however I need to be able to scroll this time around. Would I be better off putting a UILabel inside a scroll view as a work around? Or perhaps releasing the textView would work, but since I have it hooked as a referencing outlet in IB, I imagine I would have to recreate it programmatically, and I'd like to avoid something so hacky if I can.
I'm at my whit's end with something I've always felt should be simple, so thanks in advance for any and all help!

Creating a view for user to fill up a form

I'm creating a view which provides some fields for the user to fill in - address, phone number etc.
I've seen apps that have populated fields with grey text like 'Fill in your name here' on the Name field, and when the user taps on it the text is gone, a keyboard appears and the view is zoomed in to the textfield(or whatever it is). Also, after filling up that field tapping the 'Next' button on the keyboard brings the user to the next field. I'm trying to do the same but I have no idea where to get sources on this. Pardon my poor googling skills ;p
Tried UITextView and UITextField but there isn't anything related to this on the Interface Builder. Figured it lies with the codes?
It'd be great if I can get some explanation or some links on how this works (: Thanks!
EDIT: okay I played around with the Interface Builder a lil more and realized I could set placeholder as the grey text.
It is in fact a UITextField.
You can get the greyed out text by setting its placeholder property.
I am not sure what you mean by zooming but usually they do use a scroll view or adjust the frame so that it isn't blocked by the keyboard.
The Next button is made available using a UIToolbar instance as the text field's inputAccessoryView. However the implementation is one's own and is not framework provided. You can look at this example to get started.

iOS Longpress to select text

I am building a simple table based search app. The user can ofcourse search through keyboard. But I also want to have another mode of search.
Say a user longpress on a UILabel then based on where the press is happening, that word should get selected & search should happen (no keyboard) on that word.
I know how to detect longpress events but does anyone know how to copy, detect & access the exact term where the longpress has occurred?
UPDATE: I am aware that I can get the object where longpress occurred from which I can get the content of the label text. But I need the exact word on top of which the finger was placed.
Nothing easy comes to mind for UILabel or UITextView. If you're wiling to lay the text out yourself with Core Text, then you can pretty easily find words near the touch. The trouble with that is you'd lose the existing selection features of UILabel and have to reimplement them (or do "shadow layout" in Core Text hoping things will lay out identically in the UILabel... slowly descending into madness here.)
Rather than going down this rabbit hole, I'd recommend using the standard text selection mechanism and add "search" to the menu.
Just put a UILongPressGestureRecognizer on your label then when the gesture recognizer fires you can access which object it came from and read the label value from there.
myLongPressRecognizer.view