如何在 ios xamarin 的文本字段中禁用空格字符 [英] How to disable space character in a text field in ios xamarin
问题描述
我不想允许在显示名称字段中输入空格.
I don't want to allow to enter space in display name field.
例如,如果用户键入这样的名称名称"或名称"或名称",则最终结果应该是名称"消除空格.
For example if the user types the name like this " Name" or " Name" or " Name ", the end result should be "Name" eliminating the spaces.
我尝试过这种方法,但修剪似乎不起作用.
I tried this approach but the trim doesn't seem to work.
NameText.EditingDidEnd += delegate {
char[] arr = new char[] {' '};
NameText.Text.TrimStart(arr);
ScrollPageForKeyboard(false);
};
推荐答案
我建议阻止用户实际输入空格,而不是在他们都准备好之后删除它.
I would suggest preventing the user from actually entering whitespace versus removing it after they all ready have.
添加一个 ShouldChangeCharacters
处理程序并检查它们是否在由 NSCharacterSet.WhitespaceAndNewlines
Add a ShouldChangeCharacters
handler and check to see if they are entering any characters in the whitespace character set as defined by NSCharacterSet.WhitespaceAndNewlines
NameText.ShouldChangeCharacters += (UITextField textField, NSRange range, string replacementString) =>
{
foreach (var aChar in replacementString)
{
if (NSCharacterSet.WhitespaceAndNewlines.Contains(aChar))
return false;
}
return true;
};
这篇关于如何在 ios xamarin 的文本字段中禁用空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!