天天看點

Xamarin.iOS本地生成驗證碼

根據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;