[英]UITextField Max Length in C# with Xamarin.iOS
I would like to set a limit to the number of characters that can be entered into a UITextField in an iOS app to 25 characters. 我想设置一个限制,可以在iOS应用程序中输入UITextField的字符数为25个字符。
According to this post , it could be done in Objective-C like this: 根据这篇文章 ,它可以在Objective-C中完成,如下所示:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 25) ? NO : YES;
}
How can I do this exact same thing in C# with Xamarin.iOS? 如何在C#中使用Xamarin.iOS完成同样的事情?
This is untested, but I believe it may work: 这是未经测试的,但我相信它可能有效:
UITextField myField;
myField.ShouldChangeCharacters = (textField, range, replacementString) => {
var newLength = textField.Text.Length + replacementString.Length - range.Length;
return newLength <= 25;
};
I ended up doing it with a delegate rather than lamba notation: 我最后用一个代表而不是lamba表示法来做它:
UITextField myField;
myField.ShouldChangeCharacters = new UITextFieldChange(delegate(UITextField textField, MonoTouch.Foundation.NSRange range, string replacementString) {
int newLength = textField.Text.Length + replacementString.Length - range.Length;
return newLength <= 25;
});
But I think that Rolf's way using lambda is easier to read. 但我认为Rolf使用lambda的方式更容易阅读。
It's really been a while since it has been answered and it's a good solution, but it can be achieved in a different way using the NotificationCenter
. 它已经有一段时间了,因为它已被回答,这是一个很好的解决方案,但它可以使用
NotificationCenter
以不同的方式实现。 This way, you can even add a callback and make it work in a separated class like a small component. 这样,您甚至可以添加回调并使其在分离的类(如小组件)中工作。
I'm adding it here just in case it can help someone else. 我在这里添加它以防万一它可以帮助别人。
NSNotificationCenter.DefaultCenter.AddObserver(
UITextField.TextFieldTextDidChangeNotification, TextChangedEvent);
private void TextChangedEvent(NSNotification notification) {
UITextField field = (UITextField)notification.Object;
if (notification.Object == field) {
if (field.Text.Length >= 25) {
field.Text = field.Text.Substring (0, 24);
}
}
}
After 2 days working with this issue, I found out the solution for it by myself. 在处理了这个问题2天后,我自己找到了解决方案。 It's working with copy/paste case.
它正在使用复制/粘贴案例。 Hope it will useful.
希望它会有用。
public static bool CheckTexfieldMaxLength (UITextField textField, NSRange range, string replacementString, int maxLength)
{
int maxLength = 10;
int newLength = (textField.Text.Length - (int)range.Length) + replacementString.Length;
if (newLength <= maxLength) {
return true;
} else {
if (range.Length == 0 && range.Location > 0 && replacementString.Length > 0 && textField.Text.Length >= maxLength)
return false;
int emptySpace = maxLength - (textField.Text.Length - (int)range.Length);
textField.Text = textField.Text.Substring (0, (int)range.Location)
+ replacementString.Substring (0, emptySpace)
+ textField.Text.Substring ((int)range.Location + (int)range.Length, emptySpace >= maxLength ? 0 : (maxLength - (int)range.Location - emptySpace));
return false;
}
}
My solution is using the EditingChange event: 我的解决方案是使用EditingChange事件:
_myUTextField.EditingChanged += (sender, e) =>{
if (_myUTextField.Text.Length > maxLength){
_myUTextField.Text = _myUTextField.Text.Substring(0, _myUTextField.Text.Length - 1);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.