0

enter image description here

I have in a project the above UITextField with a button inside. When user is typing the cursor should move but when it reaches the UIButton should stop. Actually the width of the editable area of the UITextField should be until the user reaches the start of the dark blue UIButton image. Text written should not go beyond the UIButton but the user should be able to continue writing.

Is this somehow possible? Any help appreciated.

stefanosn
  • 3,264
  • 10
  • 53
  • 79
  • Set the `rightView` property of the `UITextField`, that should do the trick. – Larme May 18 '22 at 15:18
  • Could you please explain with a small example sir? – stefanosn May 18 '22 at 15:40
  • `UIButton *myButon = [...]; [myTextview setRightView: myButton]`? – Larme May 18 '22 at 15:41
  • Does this answer your question? [Add a button on right view of UItextfield in such way that, text should not overlap the button](https://stackoverflow.com/questions/42082339/add-a-button-on-right-view-of-uitextfield-in-such-way-that-text-should-not-over) – Larme May 18 '22 at 15:41
  • Let me check sir – stefanosn May 18 '22 at 15:48
  • No this does not solve the issue because the button is already in the uitextfield using constraints – stefanosn May 18 '22 at 16:06
  • And could you set it as `rightView` instead? Or you could put a transparent rightView that will make placeholder for your button. Or you can override I guess `textRectForBounds:` or `editingRectForBounds:` to make the text area not contains the button... – Larme May 18 '22 at 16:09
  • Let me try it sir, i do not know how to do it i will search for override textRectForBounds – stefanosn May 18 '22 at 16:20
  • I did this sir UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.searchBarIconView.frame.size.width+22, self.searchBarTextField.frame.size.height)]; rihtView.backgroundColor = self.searchBarTextField.backgroundColor; self.searchBarTextField.rightView = rightView; self.searchBarTextField.rightViewMode = UITextFieldViewModeAlways; but i had to use also self.searchBarTextField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 0); to move cursor a bit to the right because it didnt show correctly on the left side... – stefanosn May 18 '22 at 16:36

0 Answers0