| 
using System; 
 | 
using System.Collections.Generic; 
 | 
using System.Linq; 
 | 
using System.Text; 
 | 
using UIKit; 
 | 
using Foundation; 
 | 
using Shared.IO; 
 | 
using CoreGraphics; 
 | 
  
 | 
namespace Shared 
 | 
{ 
 | 
    /// <summary> 
 | 
    /// EditTextView 文本输入框 
 | 
    /// 支持换行 
 | 
    /// </summary> 
 | 
    public class EditTextView : View 
 | 
    { 
 | 
        public Action<View, FocusEventArgs> FoucsChanged; 
 | 
        /// <summary> 
 | 
        /// 当前视图 
 | 
        /// </summary> 
 | 
        /// <value>The ios user interface text view.</value> 
 | 
        MyEditTextView iosUITextView 
 | 
        { 
 | 
            get 
 | 
            { 
 | 
                return uiView as MyEditTextView; 
 | 
            } 
 | 
            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); 
 | 
                iosUITextView.placeholderUILabel.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 true; 
 | 
            } 
 | 
            set 
 | 
            { 
 | 
                //iosUITextView.Enabled = value; 
 | 
            } 
 | 
        } 
 | 
  
 | 
  
 | 
        string _placeholderText = " "; 
 | 
        /// <summary> 
 | 
        /// 显示提示信息 
 | 
        /// </summary> 
 | 
        /// <value>The placeholder.</value> 
 | 
        public string PlaceholderText 
 | 
        { 
 | 
            get 
 | 
            { 
 | 
                return _placeholderText; 
 | 
            } 
 | 
            set 
 | 
            { 
 | 
                //iosUITextView.Placeholder = value; 
 | 
  
 | 
                _placeholderText = value; 
 | 
  
 | 
                iosUITextView.placeholderUILabel.Text = _placeholderText; 
 | 
            } 
 | 
        } 
 | 
  
 | 
  
 | 
        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); 
 | 
  
 | 
                iosUITextView.placeholderUILabel.TextColor = _placeholderTextUIColor; 
 | 
            } 
 | 
        } 
 | 
  
 | 
    
 | 
        /// <summary> 
 | 
        /// 是否用*号隐藏字符 
 | 
        /// </summary> 
 | 
        /// <value>The secure text entry.</value> 
 | 
        public bool SecureTextEntry 
 | 
        { 
 | 
            get 
 | 
            { 
 | 
                return iosUITextView.SecureTextEntry; 
 | 
            } 
 | 
            set 
 | 
            { 
 | 
                iosUITextView.SecureTextEntry = value; 
 | 
                if (value) 
 | 
                { 
 | 
                    SetTextContentType(); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
  
 | 
        /// <summary> 
 | 
        /// 解决iOS12 以上默认提示自动生成强密码 
 | 
        /// </summary> 
 | 
        public void SetTextContentType() 
 | 
        { 
 | 
            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0)) 
 | 
            { 
 | 
                iosUITextView.SetTextContentType(UITextContentType.Password); 
 | 
            } 
 | 
        } 
 | 
        public static EditTextView Instance; 
 | 
        /// <summary> 
 | 
        /// 构造函数 
 | 
        /// </summary> 
 | 
        public EditTextView() 
 | 
        { 
 | 
            iosUITextView = new MyEditTextView(this) { }; 
 | 
  
 | 
            this.TextSize = Application.FontSize; 
 | 
        } 
 | 
  
 | 
        /// <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) 
 | 
                { 
 | 
                    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 
 | 
                { 
 | 
                    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); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        /// <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(); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        /// <summary> 
 | 
        /// 刷新placeholderUILabel布局 
 | 
        /// </summary> 
 | 
        public void InitIosPlaceholderUILabelWithHeight(int mHeight) 
 | 
        { 
 | 
            var MFrame = iosUITextView.placeholderUILabel.Frame; 
 | 
            MFrame.X = 5; 
 | 
            MFrame.Y = 0; 
 | 
            MFrame.Height = mHeight; 
 | 
            MFrame.Width = iosUITextView.Frame.Size.Width - 20; 
 | 
            iosUITextView.placeholderUILabel.Frame = MFrame; 
 | 
        } 
 | 
  
 | 
        /// <summary> 
 | 
        /// 隐藏软键盘 
 | 
        /// </summary> 
 | 
        public void HideSoftInput() 
 | 
        { 
 | 
            Foucs = false; 
 | 
        } 
 | 
  
 | 
        public Action<View, string> KeyEventAction; 
 | 
        class MyEditTextView : UIKit.UITextView, IUITextViewDelegate 
 | 
        { 
 | 
            public UILabel placeholderUILabel; 
 | 
            [Weak] EditTextView EditTextView; 
 | 
            public MyEditTextView(EditTextView EditTextView) 
 | 
            { 
 | 
                this.EditTextView = EditTextView; 
 | 
                TextColor = UIColor.White; 
 | 
                placeholderUILabel = new UILabel(new CGRect(5, 0, this.Frame.Size.Width - 20, 40)); 
 | 
                placeholderUILabel.Lines = 0; 
 | 
                placeholderUILabel.TextColor = UIColor.Gray; 
 | 
  
 | 
  
 | 
                this.AddSubview(placeholderUILabel); 
 | 
  
 | 
                this.Delegate = this; 
 | 
                EditorEnterAction += () => { 
 | 
                    this.EditTextView?.EditorEnterAction?.Invoke(this.EditTextView); 
 | 
                }; 
 | 
  
 | 
                //EditingChanged += (sender, e) => 
 | 
                //{ 
 | 
                //    this.EditTextView?.TextChangeEventHandler?.Invoke(this.EditTextView, Text); 
 | 
                //}; 
 | 
                //EditingDidBegin += (sender, e) => 
 | 
                //{ 
 | 
                //    Instance = this.EditTextView; 
 | 
                //    this.EditTextView?.FoucsChanged?.Invoke(this.EditTextView, new FocusEventArgs { Focus = true }); 
 | 
                //}; 
 | 
                //EditingDidEnd += (sender, e) => 
 | 
                //{ 
 | 
                //    this.EditTextView?.FoucsChanged?.Invoke(this.EditTextView, new FocusEventArgs { Focus = false }); 
 | 
                //}; 
 | 
            } 
 | 
  
 | 
            //public void InitPlaceholderUILabelWithHeight(int mHeight) { 
 | 
            //    var MFrame = placeholderUILabel.Frame; 
 | 
            //    MFrame.Height = mHeight; 
 | 
            //    MFrame.Width = this.Frame.Size.Width - 20; 
 | 
            //    placeholderUILabel.Frame = MFrame; 
 | 
            //} 
 | 
  
 | 
            public override void MovedToSuperview() 
 | 
            { 
 | 
                base.MovedToSuperview(); 
 | 
                if (Superview == null) 
 | 
                { 
 | 
                    Shared.HDLUtils.WriteLine("从父控件中移除"); 
 | 
                } 
 | 
                else { 
 | 
                    Shared.HDLUtils.WriteLine("添加到父控件"); 
 | 
                } 
 | 
            } 
 | 
  
 | 
            public override void DeleteBackward() 
 | 
            { 
 | 
                base.DeleteBackward(); 
 | 
                EditTextView?.KeyEventAction?.Invoke(EditTextView, "Del"); 
 | 
            } 
 | 
  
 | 
            internal System.Action EditorEnterAction; 
 | 
  
 | 
  
 | 
            [Export("textViewDidChange:")] 
 | 
            public new void Changed(UITextView textView) 
 | 
            { 
 | 
                this.EditTextView?.TextChangeEventHandler?.Invoke(this.EditTextView, Text); 
 | 
  
 | 
                //取消按钮点击权限,并显示文字 
 | 
                if (textView.Text.Length == 0) 
 | 
                { 
 | 
                    placeholderUILabel.Hidden = false; 
 | 
                } 
 | 
                else 
 | 
                { 
 | 
                    placeholderUILabel.Hidden = true; 
 | 
                } 
 | 
            } 
 | 
  
 | 
            [Export("textViewDidBeginEditing:")] 
 | 
            public void EditingStarted(UITextView textView) 
 | 
            { 
 | 
                Instance = this.EditTextView; 
 | 
                this.EditTextView?.FoucsChanged?.Invoke(this.EditTextView, new FocusEventArgs { Focus = true }); 
 | 
            } 
 | 
            [Export("textViewDidEndEditing:")] 
 | 
            public void EditingEnded(UITextView textView) 
 | 
            { 
 | 
                this.EditTextView?.FoucsChanged?.Invoke(this.EditTextView, new FocusEventArgs { Focus = false }); 
 | 
            } 
 | 
  
 | 
             
 | 
  
 | 
  
 | 
            nfloat x; 
 | 
        
 | 
            /// <summary> 
 | 
            /// 点击开始 
 | 
            /// </summary> 
 | 
            /// <param name="touches">Touches.</param> 
 | 
            /// <param name="evt">Evt.</param> 
 | 
            public override void TouchesBegan(NSSet touches, UIEvent evt) 
 | 
            { 
 | 
                EditTextView?.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) 
 | 
            { 
 | 
                EditTextView?.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) 
 | 
            { 
 | 
                EditTextView?.TouchEvent(EventActions.Up, (touches.AnyObject as UITouch).LocationInView(this)); 
 | 
            } 
 | 
  
 | 
            public override void TouchesCancelled(NSSet touches, UIEvent evt) 
 | 
            { 
 | 
                EditTextView?.TouchEvent(EventActions.Cancel, (touches.AnyObject as UITouch).LocationInView(this)); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
    //public class FocusEventArgs : EventArgs 
 | 
    //{ 
 | 
    //    /// <summary> 
 | 
    //    /// true 为获取到焦点 
 | 
    //    /// false 焦点消失 
 | 
    //    /// </summary> 
 | 
    //    public bool Focus; 
 | 
    //} 
 | 
  
 | 
  
 | 
//    -(void) textViewDidChange:(UITextView*) textView 
 | 
//    { 
 | 
//        self.descLab.hidden = YES; 
 | 
//        self.sendBtn.userInteractionEnabled = YES; 
 | 
//        self.sendBtn.backgroundColor = MainColor; 
 | 
//        //实时显示字数 
 | 
//        self.stringlenghtLab.text = [NSString stringWithFormat:@"%ld/100",(long)textView.text.length]; 
 | 
     
 | 
//    //字数限制 
 | 
//    if (textView.text.length >= 100) { 
 | 
//            textView.text = [textView.text substringToIndex: 100]; 
 | 
//        } 
 | 
     
 | 
//    //取消按钮点击权限,并显示文字 
 | 
//    if (textView.text.length == 0) { 
 | 
//            self.descLab.hidden = NO; 
 | 
//            self.sendBtn.userInteractionEnabled = NO; 
 | 
//            self.sendBtn.backgroundColor = [UIColor lightGrayColor]; 
 | 
//        } 
 | 
//    } 
 | 
  
 | 
//-(BOOL) textView:(UITextView*) textView shouldChangeTextInRange:(NSRange) range replacementText:(NSString*) text 
 | 
//    { 
 | 
//    if ([text isEqualToString:@"\n"]) { 
 | 
  
 | 
  
 | 
//        [self.FKTextView resignFirstResponder]; 
 | 
         
 | 
//        return NO; 
 | 
//    } 
 | 
     
 | 
//    return YES; 
 | 
//} 
 | 
} 
 |