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
- Xamarin.iOS本地生成验证码
- IOS 生成本地验证码
- iOS 生成本地验证码
- iOS 生成本地验证码
- iOS生成本地验证码
- iOS 生成本地验证码以及使用
- iOS本地动态验证码生成
- iOS 本地动态验证码生成
- iOS--本地验证码
- SQLite xamarin iOS 本地数据库
- 本地动态生成验证码
- ios 验证码生成
- Android本地验证码的生成
- android 本地生成图形验证码
- Xamarin.iOS指定颜色值生成图片
- iOS随机生成验证码
- iOS随机生成验证码
- iOS OC生成验证码
- 两种最常见的HTTP请求类型
- UIImagePickerController返回的图片可能是旋转的需要用imageOrientation将其矫正
- MSP430 PROFILE功能
- 程序变量命名推荐规范
- Unity手游之路<二>Java版服务端使用protostuff简化protobuf开发
- Xamarin.iOS本地生成验证码
- 用Canvas为网页添加动态背景
- OA系统的价值
- Unity手游之路<三> 基于Unity+Java的聊天室源码
- 放松地方和噶
- nodejs之多进程
- Ubuntu下获取Android源码
- Android自定义控件属性
- Unity手游之路<四>3d旋转-四元数,欧拉角和变幻矩阵