Xamarin.iOS本地生成验证码

来源:互联网 发布:java环境变量配置成功 编辑:程序博客网 时间:2024/05/19 16:21

根据Objc的编程思路,写成了Xamarin的代码,总体简单:

1.设定需要生成的字符集合

2.通过C#随机生成数(范围[0,集合元素个数])来指定集合下标的字符

3.将字符通过MonoTouch.CoreGraphics.UIGraphics来描绘在视图上,然后添加一些干扰线

public class PooCodeView:UIView{private List<string> changeArray;private List<string> changeString;/// <summary>/// 设置显示文字大小/// </summary>/// <value>The font.</value>public UIFont Font{ get; set;}/// <summary>/// 获取验证码值/// </summary>/// <value>The code value.</value>public string CodeValue{get{ if(changeString!=null && changeString.Count == 4){return changeString[0] + changeString[1] + changeString[2] + changeString[3];}return string.Empty;}}public PooCodeView (RectangleF frame):base(frame){this.changeArray = new List<string>(){"0","1","2","3","4","5","6","7","8","9","a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};this.changeString = new List<string>();this.Change ();}public override void TouchesBegan (NSSet touches, UIEvent evt){base.TouchesBegan (touches, evt);this.Change ();this.SetNeedsDisplay ();}/// <summary>/// 刷新显示/// </summary>/// <value><c>true</c> if this instance is relod; otherwise, <c>false</c>.</value>public bool IsRelod{set{ this.Change ();this.SetNeedsDisplay ();}}private void Change(){this.changeString.Clear ();Random random = new Random ();for(int i=0;i<4;i++){int index = random.Next(0,this.changeArray.Count-1);var num = this.changeArray[index];this.changeString.Add (num);}}public override void Draw (RectangleF rect){base.Draw (rect);var size = new NSString ("S").StringSize(Font);float width = rect.Size.Width / this.changeString.Count - size.Width;float height = rect.Size.Height - size.Height;float pX, pY;float red, grenn, blue;PointF point;UIColor color;//文字显示Random random = new Random ();var txtContext = UIGraphics.GetCurrentContext ();for(int i = 0;i<this.changeString.Count;i++){red = random.Next () % 100 / 100.0f;grenn = random.Next () % 100 / 100.0f;blue = random.Next () % 100 / 100.0f;color = UIColor.FromRGB (red,grenn,blue);txtContext.SetFillColorWithColor (color.CGColor);pX = random.Next(0,this.changeArray.Count-1) % width + rect.Size.Width/this.changeString.Count * i;pY = random.Next (0, this.changeArray.Count-1) % height;point = new PointF (pX,pY);var c = this.changeString[i];NSString txtC = new NSString (c);txtC.DrawString (point,Font);}//干扰线var context = UIGraphics.GetCurrentContext ();context.SetLineWidth (1.0f);for(int count = 0;count < 2;count ++){red = random.Next () % 100 / 100.0f;grenn = random.Next () % 100 / 100.0f;blue = random.Next () % 100 / 100.0f;color = UIColor.FromRGB (red,grenn,blue);context.SetStrokeColor (color.CGColor);pX = random.Next () % (int)rect.Size.Width;pY = random.Next () % (int)rect.Size.Height;context.MoveTo (pX,pY);pX = random.Next () % (int)rect.Size.Width;pY = random.Next () % (int)rect.Size.Height;context.AddLineToPoint (pX,pY);context.StrokePath ();}}}
调用:
PooCodeView codeView = new PooCodeView (new RectangleF(13,10,80,txth));codeView.Font = ViewStyle.NavigationTitleFont ();codeView.BackgroundColor = UIColor.Clear;



0 0