Adding an uneditable text suffix to a UITextField

2020-07-07 07:30发布

I have a UITextField that I'd like to add a "?" suffix to all text entered.

The user should not be able to remove this "?" or add text to the right hand side of it.

What's the best way to go about this?

9条回答
祖国的老花朵
2楼-- · 2020-07-07 08:19

You'll probably need to subclass UITextField and override its drawText: method to draw an additional "?" character to the right of the actual text. (Rather than actually add a "?" to the text of the view.

查看更多
ら.Afraid
3楼-- · 2020-07-07 08:28

I would add a method that is called when edit finishes:

`- (void)editDidFinish {
  NSString* str=[[NSString alloc] init];
  str=myEdit.text;
  [str stringByAppendingString:@"?"];
  myEdit.text=str;
}`
查看更多
霸刀☆藐视天下
4楼-- · 2020-07-07 08:30

I had this issue and I wrote a subclass to add this functionality: https://github.com/sbaumgarten/UIPlaceholderSuffixField. Hopefully you have found a solution by now but if you haven't, this should work.

查看更多
登录 后发表回答