为UITextField添加前缀 [英] Add a prefix to UITextField

查看:95
本文介绍了为UITextField添加前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个'$'登录文本字段,无法删除。用户应该能够在其后面输入值,如果他按下退格键,它应该只删除他输入的值。

I want a '$' Sign in the text field which cannot be erased. A user should be able to enter values after it and if he presses backspace it should only erase the value he entered.

之前我不能使用UILabel的原因是因为UITextfield中的文本是居中对齐的,当用户输入值时,它会增长到任一侧。

The reason I can't use UILabel before it is because the text in UITextfield is center aligned and it grows to either side as the user enters values.

请提供任何解决方案

推荐答案


  1. 最初将文本字段的文本设置为$。

  2. 将文本字段的委托设置为某些您的对象(可能是包含它的视图控制器)。

  3. 实现 -textField:shouldChangeCharactersInRange:replacementString:并返回如果建议的更改将删除$。

  1. Set the text field's text to "$" initially.
  2. Set the text field's delegate to some object of yours (probably the view controller containing it).
  3. Implement -textField:shouldChangeCharactersInRange:replacementString: and return NO if the proposed change would delete the "$".

此外,您可能需要实现 -textFieldDidBeginEditing:将光标定位在$之后(如果它已经不存在)。

Also, you might need to implement -textFieldDidBeginEditing: to position the cursor after the "$" if it's not there already.

这篇关于为UITextField添加前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆