using System; using System.Collections.Generic; using System.Linq; using System.Text; using UIKit; using Foundation; using Shared.IO; using CoreGraphics; namespace Shared { /// <summary> /// 文本输入框 /// </summary> public class EditText : View { public Action<View, FocusEventArgs> FoucsChanged; /// <summary> /// 当å‰è§†å›¾ /// </summary> /// <value>The ios user interface text view.</value> MyEditText iosUITextView { get { return uiView as MyEditText; } set { uiView = value; } } //一端å£è¿›å…¥é…ç½®æ¨¡å¼ 1æç¤ºä¸è¦æŽ¥å…¥è®¾å¤‡ 2获å–设备列表 3接入设备 4è¿½åŠ è®¾å¤‡ /// <summary> /// æ–‡å—å¤§å° /// </summary> /// <value>The size of the text.</value> public float TextSize { get { return (float)iosUITextView.Font.PointSize; } set { iosUITextView.Font = UIFont.SystemFontOfSize(value);// .SetTextSize (ComplexUnitType.Sp, value); } } /// <summary> /// 是å¦è®¾ç½®ä¸ºåŠ è½½æ—¶æ˜¾ç¤ºè¾“å…¥é”®ç›˜ /// </summary> /// <value><c>true</c> if is first responder; otherwise, <c>false</c>.</value> public bool IsFirstResponder { get { return iosUITextView.IsFirstResponder; } set { if (value) { iosUITextView.BecomeFirstResponder(); } else { iosUITextView.ResignFirstResponder(); } } } int textID; /// <summary> /// æ ¹æ®ID获å–对应的备注 /// </summary> /// <value>The text I.</value> public int TextID { get { return textID; } set { textID = value; Text = Language.StringByID(TextID); } } /// <summary> /// 是å¦ä½¿èƒ½ /// </summary> /// <value>是å¦ä½¿èƒ½</value> public override bool Enable { get { return iosUITextView.Enabled; } set { iosUITextView.Enabled = value; } } string _placeholderText = " "; /// <summary> /// 显示æç¤ºä¿¡æ¯ /// </summary> /// <value>The placeholder.</value> public string PlaceholderText { get { return iosUITextView.Placeholder; } set { //iosUITextView.Placeholder = value; _placeholderText = value; setPlaceholderTextAndColor(); } } UIColor _placeholderTextUIColor; uint placeholderTextColor; /// <summary> /// Gets or sets the color of the placeholder text. /// </summary> /// <value>The color of the placeholder text.</value> public uint PlaceholderTextColor { get { return placeholderTextColor; } set { placeholderTextColor = value; byte r, g, b, a; r = (byte)(placeholderTextColor / 256 / 256 % 256); g = (byte)(placeholderTextColor / 256 % 256); b = (byte)(placeholderTextColor % 256); a = (byte)(placeholderTextColor / 256 / 256 / 256 % 256); //iosUITextView.SetValueForKeyPath(UIColor.FromRGBA(r, g, b, a), new NSString("_placeholderLabel.textColor")); _placeholderTextUIColor = UIColor.FromRGBA(r, g, b, a); setPlaceholderTextAndColor(); } } /// <summary> /// 通过attributedPlaceholder属性修改Placeholder颜色 /// </summary> private void setPlaceholderTextAndColor() { //2019-09-23 适é…iOS13 var normalAttributedTitle = new NSAttributedString(_placeholderText, foregroundColor: _placeholderTextUIColor); iosUITextView.AttributedPlaceholder = normalAttributedTitle; } /// <summary> /// 是å¦ç”¨*å·éšè—å—符 /// </summary> /// <value>The secure text entry.</value> public bool SecureTextEntry { get { return iosUITextView.SecureTextEntry; } set { iosUITextView.SecureTextEntry = value; // if (value) // { // ResetTextContentType(); // } } } /// <summary> /// 暂时解决iOS13 以上默认æç¤ºè‡ªåŠ¨ç”Ÿæˆå¼ºå¯†ç ï¼Œä¸æ˜¯æœ€ä¼˜æ–¹æ¡ˆ /// </summary> public void ResetTextContentType() { if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0)) { iosUITextView.SetTextContentType(UITextContentType.Password); //HDLUtils.WriteLine(" GetTextContentType: " + iosUITextView.GetTextContentType().ToString()); //if (iosUITextView.GetTextContentType() == UITextContentType.NewPassword) //{ // iosUITextView.SetTextContentType(UITextContentType.Password); //} } } bool isNumberKeyboardType = false; //设置键盘类型 public bool IsNumberKeyboardType { get { return isNumberKeyboardType; } set { isNumberKeyboardType = value; if (value) { iosUITextView.KeyboardType = UIKeyboardType.NumberPad; } else { iosUITextView.KeyboardType = UIKeyboardType.Default; } } } public static EditText Instance; /// <summary> /// æž„é€ å‡½æ•° /// </summary> public EditText() { iosUITextView = new MyEditText(this) { }; ResetTextContentType(); } /// <summary> /// 创新需è¦åˆ›æ–°çš„ä¿¡æ¯ /// </summary> public override void Refresh() { base.Refresh(); IsSelected = isSelected; } /// <summary> /// 文本 /// </summary> /// <value>The text.</value> public string Text { get { return iosUITextView.Text; } set { iosUITextView.Text = value; TextChangeEventHandler?.Invoke(this, value);//èµ‹å€¼ï¼Œå¢žåŠ è§¦å‘事件 } } uint textColor; /// <summary> /// æ–‡å—颜色 /// </summary> /// <value>The color of the text.</value> public uint TextColor { get { return textColor; } set { textColor = value; byte r, g, b, a; r = (byte)(textColor / 256 / 256 % 256); g = (byte)(textColor / 256 % 256); b = (byte)(textColor % 256); a = (byte)(textColor / 256 / 256 / 256 % 256); iosUITextView.TextColor = UIKit.UIColor.FromRGBA(r, g, b, a); } } bool isSelected; /// <summary> /// Gets or sets a value indicating whether this instance is selected. /// </summary> /// <value><c>true</c> if this instance is selected; otherwise, <c>false</c>.</value> public bool IsSelected { get { return isSelected; } set { isSelected = value; if (!IsCanRefresh) { return; } if (isSelected) { if (null == SelectedImagePath) { byte r, g, b, a; r = (byte)(SelectedBackgroundColor / 256 / 256 % 256); g = (byte)(SelectedBackgroundColor / 256 % 256); b = (byte)(SelectedBackgroundColor % 256); a = (byte)(SelectedBackgroundColor / 256 / 256 / 256 % 256); iosUITextView.BackgroundColor = UIKit.UIColor.FromRGBA(r, g, b, a); } else { iosUITextView.Background = UIImage.FromFile(FileUtils.GetImageFilePath(SelectedImagePath)); } } else { if (null == UnSelectedImagePath) { byte r, g, b, a; r = (byte)(BackgroundColor / 256 / 256 % 256); g = (byte)(BackgroundColor / 256 % 256); b = (byte)(BackgroundColor % 256); a = (byte)(BackgroundColor / 256 / 256 / 256 % 256); iosUITextView.BackgroundColor = UIKit.UIColor.FromRGBA(r, g, b, a); } else { iosUITextView.Background = UIImage.FromFile(FileUtils.GetImageFilePath(UnSelectedImagePath)); } } } } /// <summary> /// 选择时背景颜色 /// </summary> /// <value>The color of the text.</value> public uint SelectedBackgroundColor { get; set; } TextAlignment textAlignment = TextAlignment.Center; /// <summary> /// Texts the alignment. /// </summary> public TextAlignment TextAlignment { get { return textAlignment; } set { textAlignment = value; switch (value) { case TextAlignment.TopLeft: //iosUITextView.TextAlignment = UITextAlignment.Left; break; case TextAlignment.TopCenter: //iosUITextView.TextAlignment = UITextAlignment.Center| UITextAlignment.Left; break; case TextAlignment.TopRight: // iosUITextView.TextAlignment = UITextAlignment.Right; break; case TextAlignment.CenterLeft: iosUITextView.TextAlignment = UITextAlignment.Left; break; case TextAlignment.Center: iosUITextView.TextAlignment = UITextAlignment.Center; break; case TextAlignment.CenterRight: iosUITextView.TextAlignment = UITextAlignment.Right; break; case TextAlignment.BottomLeft: // iosUITextView.TextAlignment = UIKit.UIControlContentVerticalAlignment.Bottom; // iosUITextView.TextAlignment = UIKit.UIControlContentHorizontalAlignment.Left; break; case TextAlignment.BottomCenter: // iosUITextView.TextAlignment = UIKit.UIControlContentVerticalAlignment.Bottom; // iosUITextView.TextAlignment = UIKit.UIControlContentHorizontalAlignment.Center; break; case TextAlignment.BottomRight: // iosUITextView.VerticalAlignment = UIKit.UIControlContentVerticalAlignment.Bottom; // iosUITextView.HorizontalAlignment = UIKit.UIControlContentHorizontalAlignment.Right; break; } } } /// <summary> /// 选择时背景图路径 /// </summary> /// <value>The selected image path.</value> public string SelectedImagePath { get; set; } /// <summary> /// éžé€‰ä¸çжæ€çš„背景图路径 /// </summary> /// <value>The un selected image path.</value> public string UnSelectedImagePath { get; set; } /// <summary> /// 输入文å—å˜åŒ–事件 /// </summary> public Action<View, string> TextChangeEventHandler; /// <summary> /// 按了返回按键事件 /// </summary> public Action<View> EditorEnterAction; /// <summary> /// 设置焦点 /// </summary> public bool Foucs { get { return iosUITextView.IsFirstResponder; } set { if (value) { //显示键盘 iosUITextView.BecomeFirstResponder(); } else { //éšè—键盘 iosUITextView.ResignFirstResponder(); } } } public Action<View, string> KeyEventAction; class MyEditText : UIKit.UITextField, IUITextFieldDelegate { [Weak] EditText editText; public MyEditText(EditText editText) { this.editText = editText; TextColor = UIColor.White; this.Delegate = this; EditorEnterAction += () => { this.editText?.EditorEnterAction?.Invoke(this.editText); }; EditingChanged += (sender, e) => { this.editText?.TextChangeEventHandler?.Invoke(this.editText, Text); }; EditingDidBegin += (sender, e) => { Instance = this.editText; this.editText?.FoucsChanged?.Invoke(this.editText, new FocusEventArgs { Focus = true }); }; EditingDidEnd += (sender, e) => { this.editText?.FoucsChanged?.Invoke(this.editText, new FocusEventArgs { Focus = false }); }; } public override void MovedToSuperview() { base.MovedToSuperview(); if (Superview == null) { //Shared.HDLUtils.WriteLine("从父控件ä¸ç§»é™¤"); } else { //Shared.HDLUtils.WriteLine("æ·»åŠ åˆ°çˆ¶æŽ§ä»¶"); } } public override void DeleteBackward() { base.DeleteBackward(); editText?.KeyEventAction?.Invoke(editText, "Del"); } internal System.Action EditorEnterAction; [Export("textFieldShouldReturn:")] public new bool ShouldReturn(UITextField textField) { ResignFirstResponder(); EditorEnterAction?.Invoke(); return true; } nfloat x; /// <summary> /// 点击开始 /// </summary> /// <param name="touches">Touches.</param> /// <param name="evt">Evt.</param> public override void TouchesBegan(NSSet touches, UIEvent evt) { editText?.TouchEvent(EventActions.Down, (touches.AnyObject as UITouch).LocationInView(this)); } /// <summary> /// 移动 /// </summary> /// <param name="touches">Touches.</param> /// <param name="evt">Evt.</param> public override void TouchesMoved(NSSet touches, UIEvent evt) { editText?.TouchEvent(EventActions.Move, (touches.AnyObject as UITouch).LocationInView(this)); } /// <summary> /// 点击弹起 /// </summary> /// <param name="touches">Touches.</param> /// <param name="evt">Evt.</param> public override void TouchesEnded(NSSet touches, UIEvent evt) { editText?.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this)); } public override void TouchesCancelled(NSSet touches, UIEvent evt) { editText?.TouchEvent(EventActions.Cancel, (touches.AnyObject as UITouch).LocationInView(this)); } } } public class FocusEventArgs : EventArgs { /// <summary> /// true 为获å–到焦点 /// false 焦点消失 /// </summary> public bool Focus; } }