SHARE
TWEET

Untitled

a guest Jun 15th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [assembly: ExportRenderer(typeof(GradientLabel), typeof(GradientLabelRenderer))]
  2.  
  3. namespace YourNamespace.iOS.Renderers
  4. {
  5.     public class GradientLabelRenderer : LabelRenderer
  6.     {
  7.         public override void Draw(CGRect rect)
  8.         {
  9.             base.Draw(rect);
  10.  
  11.             if (Control != null)
  12.             {
  13.                 SetTextColor();
  14.             }
  15.         }
  16.  
  17.         protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
  18.         {
  19.             base.OnElementPropertyChanged(sender, e);
  20.             SetTextColor();
  21.         }
  22.  
  23.         private void SetTextColor()
  24.         {
  25.             var image = GetGradientImage(Control.Frame.Size);
  26.             if (image != null)
  27.             {
  28.                 Control.TextColor = UIColor.FromPatternImage(image);
  29.             }
  30.         }
  31.  
  32.         private UIImage GetGradientImage(CGSize size)
  33.         {
  34.             var c1 = (Element as GradientLabel).TextColor1.ToCGColor();
  35.             var c2 = (Element as GradientLabel).TextColor2.ToCGColor();
  36.  
  37.             UIGraphics.BeginImageContextWithOptions(size, false, 0);
  38.  
  39.             var context = UIGraphics.GetCurrentContext();
  40.  
  41.             if (context == null)
  42.             {
  43.                 return null;
  44.             }
  45.  
  46.             context.SetFillColor(UIColor.Blue.CGColor);
  47.             context.FillRect(new RectangleF(new PointF(0, 0), new SizeF((float)size.Width, (float)size.Height)));
  48.  
  49.             var left = new CGPoint(0, 0);
  50.             var right = new CGPoint(size.Width, 0);
  51.             var colorspace = CGColorSpace.CreateDeviceRGB();
  52.  
  53.             var gradient = new CGGradient(colorspace, new CGColor[] { c1, c2 }, new nfloat[] { 0f, 1f });
  54.  
  55.             context.DrawLinearGradient(gradient, left, right, CGGradientDrawingOptions.DrawsAfterEndLocation);
  56.  
  57.             var img = UIGraphics.GetImageFromCurrentImageContext();
  58.             UIGraphics.EndImageContext();
  59.             return img;
  60.         }
  61.     }
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top