2

I have a UITextField that will represent an integer number with a fraction (numerator and denominator). As an example: "27 3/16". I want to make the denominator "/16" at the end both un-editable and also un-selectable.

I can use the delegate method textField:shouldChangeCharactersInRange:replacementString: to prevent the "/16?" from being edited with an approach a bit like this.

Is there some way that I can also prevent the "/16" from being selectable at all? So, the caret can't be moved in to it, and the selection marque can't be made around it.

If this isn't possible, is there a hook so that once the user finishes placing their selection, I can update the selection and move the caret to just before this piece of the text.

Thanks.

Community
  • 1
  • 1
Benjohn
  • 13,228
  • 9
  • 65
  • 127

1 Answers1

0

You can have a UILabel and UITextField constrained via autolayout next to each other, the UILabel containing the denumerator and the UITextField the numerator.

Here is an answered question for how to do that: Using Auto Layout to have UILabel and UITextField next to each other (the essence is, you need to adjust the content hugging priority of the UITextField to make it always as wide as the contained text.

If your text field gets too small to be tapped, you can apply the code of this answer to a UITextView and make the tappable area of your view bigger: UIButton: Making the hit area larger than the default hit area (but better use method swizzling than overriding a method in a category like in the answer! Or a subclass.)

Community
  • 1
  • 1
stefreak
  • 1,460
  • 12
  • 30
  • Interesting – I was considering that but discounted it because I felt that the hit target for the empty field would get to small. I'll reconsider though… Thanks. – Benjohn Jan 14 '15 at 12:43
  • 1
    To make the tappable area bigger, you can apply the code of this answer to a UITextField too, in fact to any view: http://stackoverflow.com/questions/808503/uibutton-making-the-hit-area-larger-than-the-default-hit-area – stefreak Jan 14 '15 at 12:45