Nin*_*nea 3 uitextview xamarin.ios ios xamarin
我使用自定义 UITextView 并且需要在返回点击时隐藏键盘。我需要捕捉 'ShouldChangeTextInRange' 什么有 textview,我不知道为什么但没有调用方法。这是我的文本视图的代码:
public class PlaceholderTextView : UITextView
{
public PlaceholderTextView ()
{
Initialize ();
}
public PlaceholderTextView (CGRect frame)
: base (frame)
{
Initialize ();
}
public PlaceholderTextView (IntPtr handle)
: base (handle)
{
Initialize ();
}
void Initialize ()
{
Text = Placeholder;
ShouldBeginEditing = t => {
if (Text == Placeholder)
Text = string.Empty;
return true;
};
ShouldEndEditing = t => {
if (string.IsNullOrEmpty (Text))
Text = Placeholder;
return true;
};
}
public override bool ShouldChangeTextInRange (UITextRange inRange, string replacementText)
{
if (Text.Equals ("\n")) {
this.EndEditing (true);
return false;
} else {
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在您的UITextView子类中,添加IUITextViewDelegate并实现ShouldChangeText(selector = textView:shouldChangeTextInRange:replacementText:):
public class MyTextView : UITextView, IUITextViewDelegate
{
string Placeholder;
public MyTextView()
{
Initialize();
}
public MyTextView(Foundation.NSCoder coder) : base(coder)
{
Initialize();
}
public MyTextView(Foundation.NSObjectFlag t) : base(t)
{
Initialize();
}
public MyTextView(IntPtr handle) : base(handle)
{
Initialize();
}
public MyTextView(CoreGraphics.CGRect frame) : base(frame)
{
Initialize();
}
public MyTextView(CoreGraphics.CGRect frame, NSTextContainer textContainer) : base(frame, textContainer)
{
Initialize();
}
void Initialize()
{
Delegate = this;
}
[Export("textViewShouldBeginEditing:")]
public bool ShouldBeginEditing(UITextView textView)
{
if (Text == Placeholder)
Text = string.Empty;
return true;
}
[Export("textViewShouldEndEditing:")]
public bool ShouldEndEditing(UITextView textView)
{
if (string.IsNullOrEmpty(Text))
Text = Placeholder;
return true;
}
[Export("textView:shouldChangeTextInRange:replacementText:")]
public bool ShouldChangeText(UITextView textView, NSRange range, string text)
{
if (text.Contains("\n"))
{
this.EndEditing(true);
return false;
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:您不能混合使用 ObjC/Swift 风格的委托和 C# 匿名委托,否则最终会出现错误:
事件注册正在覆盖现有委托。只使用事件或您自己的委托
| 归档时间: |
|
| 查看次数: |
1640 次 |
| 最近记录: |