我可以在UITextField中放置不可编辑的文本 [英] Can I place noneditable text in UITextField

查看:199
本文介绍了我可以在UITextField中放置不可编辑的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UITextField中放置一些固定文本,但在插入点之前,有点像这样:

I'd like to place some fixed text inside an UITextField, but before the insert point, sort of like this:

...其中towaards是可编辑的部分。

...where "towaards" is the editable part.

重点是在上下文中显示可编辑的文本。

The point is to show the editable text in context.

这可能吗,和/或有更好的选择吗?

Is that possible, and/or are there better alternatives?

推荐答案

我想你可以通过实施 UITextFieldDelegate 。有一种方法 - textField:shouldChangeCharactersInRange:replacementString:

I guess you can do that by implementing UITextFieldDelegate. There is one method – textField:shouldChangeCharactersInRange:replacementString:.

在这种方法中你可以检查用户输入的每个字符(甚至是退格键)。首先,您在另一个 NSString 对象中实现更改,然后根据您的条件进行验证。

In this method you can check each and every character which is entering by user (even backspace). So first you implement change in another NSString object then validate with your condition.

检查新字符串是否通过验证,然后用新字符串替换旧字符串,否则保持原样。

Check whether new string passes through your validation then replace the old string with new string, otherwise leave it as it is.

例如,检查新字符串是否以字符串开头。你可以使用这个:

For example, check new string starts with your string or not. You can use this:

[myString hasPrefix:@他去了];

在同样的方法中,当用户点击 UITextField 检查 @时,你还可以再做一件事。 (towaards)字符串在textField中,然后按代码删除该文本。

Here in the same method you can also do one more thing, when user taps on UITextField check if @"(towaards)" string is there in textField then remove that text by code.

希望这就是你想要的。

这篇关于我可以在UITextField中放置不可编辑的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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