0

I am trying to create a textfield input, that forces the user to enter a email address but forces the domain (the domain logic is validated when the user enters return), so the user enters a email associated with that domain only. This needs to be very clear on the UI itself, I've tried using the UITextField but since the placeholder is cleared after the user starts editing it doesn't work so well. Here is a sample screenshot of the Slack app that shows similar textfield-

Eg. The user enters team domain, and ".slack.com" is appended to the user text.

enter image description here

Viren Rajput
  • 5,426
  • 5
  • 30
  • 41

2 Answers2

1

Setting up a NSAttributedString as the textField.text from textField:shouldChangeCharactersInRange:replacementString: invocations will give you the desired behaviour.

ystack
  • 1,785
  • 12
  • 23
0

When the user stars typing you can append the ending domain to what the user types. You can also play with attributed strings changing the color of the appended text so it looks as it's still part of the placeholder. This should be done intextField:shouldChangeCharactersInRange:replacementString: Check the official documentation https://developer.apple.com/reference/uikit/uitextfielddelegate/1619599-textfield?language=objc

For setting the cursor in the desired position you can use UITextPosition which is extensively explained in this answer

Community
  • 1
  • 1
Joan Cardona
  • 3,463
  • 2
  • 25
  • 43
  • Hi Joan thank you for your answer. I have question though, would that mean that the cursor moves to the end of the appended string? Because I do want the cursor to stay at the user input and not allow the user to edit the domain? – Viren Rajput Feb 14 '17 at 18:45