wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
 
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;
    }
}