1

I have an NSTextField that autoresizes. Its text is centered.

When I start typing in the field and then resize the enclosing NSWindow, the cursor stays where it's at rather than repositioning to the appropriate place :

cursor is way on the left while it should be placed at the end of the text

I've also made an XCode project demonstrating this problem : https://www.dropbox.com/sh/cohhmslyl9ti43b/AAC6ULteopsQCMDsEArJU15Ta?dl=0

Does anyone know what is happening here?

  • I've submitted this as a bug upon Apple's request. The ID is 21541483. – Bruno Vandekerkhove Jun 25 '15 at 07:44
  • From Apple : *"We believe this issue has been addressed in the latest OS X v10.11 Developer beta. This is a pre-release OS X v10.11 update. Please refer to the release notes for complete installation instructions. Please test with this release. If you still have issues, please provide any relevant logs or information that could help us investigate."* – Bruno Vandekerkhove Jul 09 '15 at 14:22

1 Answers1

1

Interesting question, I've never applied auto-layout to a text field so I was curious myself.

My solution was to listen for the NSWindowDelegate method, -windowDidResize. Upon that, I would check to see if the text field was the first responder. If it was, I set it to be first responder again, which resets the cursor in the correct location.

Code:

   NSResponder *firstResponder = [[NSApp keyWindow] firstResponder];
   if ([firstResponder isKindOfClass:[NSText class]] && (NSTextField*)[(NSText *)firstResponder delegate] == self.centeredText) {
      [self.centeredText becomeFirstResponder];
   }

I reuploaded the project so you could look too: https://www.dropbox.com/s/lhsspi5gasbwq8j/CursorDoesNotUpdate%202.zip?dl=0

A O
  • 5,516
  • 3
  • 33
  • 68
  • Thanks, it works but selects the content of the text field. What do you think of the docs, that say *"Use the NSWindow makeFirstResponder: method, not this method, to make an object the first responder. Never invoke this method directly."* ? – Bruno Vandekerkhove Jun 22 '15 at 21:49
  • 1
    Hm didn't read that :) My solution should be changed, then. Instead of making it first responder, find a way to make the NSTextField focused? I don't have my Macbook w/ me so I can't look into it until later – A O Jun 22 '15 at 22:24
  • I edited your answer based on : http://stackoverflow.com/questions/764179/focus-a-nstextfield It works fine now, thanks again! – Bruno Vandekerkhove Jun 22 '15 at 22:31