Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Patrick Wondrak
Patrick Wondrak
3,358 Points

Swift Custom Cursor Position

I'm trying to have a custom cursor position when I am entering text and I have the below code written

func textFieldDidBeginEditing(textField: UITextField) {

let arbitraryValue: Int = 5
if let newPosition = textField.positionFromPosition(textField.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: arbitraryValue) {

    textField.selectedTextRange = textField.textRangeFromPosition(newPosition, toPosition: newPosition)

when the text field is empty it does not apply. If there is text already in the field box then it executes. Is this because it is written in the "textFieldDidBegin" function? If so, where do I put the code so it applies when the text field is empty as well as when there is text in there?

Patrick have you tried 'textFieldShouldBeginEditing:' The text field calls this method when the user performs an action that would normally initiate the editing of the text fieldโ€™s text.

Not 100% sure if this will solve your problem but I think its worth a shot.

Patrick Wondrak
Patrick Wondrak
3,358 Points

I have not tried that. I will for sure. It is currently in "textFieldDidBeginEdititng" I didn't know there was a "should begin editing" I will try that when I get home and report back! Thanks so much!