Skip to content

Commit d74b610

Browse files
committed
Update 2019-01-25-Showing-Gridlines-In-Xamarin-Forms-Previewer.md
1 parent 4114490 commit d74b610

File tree

1 file changed

+205
-3
lines changed

1 file changed

+205
-3
lines changed

_drafts/2019-01-25-Showing-Gridlines-In-Xamarin-Forms-Previewer.md

+205-3
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,212 @@ namespace PreviewGridLines.Views
5151
}
5252
```
5353

54-
In your platform-specific projects you'll need a subclass of `ViewRenderer`. Here's one for iOS:
54+
In your platform-specific projects you'll need a subclass of `ViewRenderer`. The one for iOS looks like this:
5555

5656
```
57-
PreviewGridRenderer
57+
using System.ComponentModel;
58+
using System.Linq;
59+
using CoreGraphics;
60+
using PreviewGridLines.Helpers;
61+
using PreviewGridLines.iOS.Renderers;
62+
using PreviewGridLines.Views;
63+
using UIKit;
64+
using Xamarin.Forms;
65+
using Xamarin.Forms.Platform.iOS;
66+
67+
[assembly: ExportRenderer(typeof(PreviewGrid), typeof(PreviewGridRenderer))]
68+
69+
namespace PreviewGridLines.iOS.Renderers
70+
{
71+
public class PreviewGridRenderer : ViewRenderer<PreviewGrid, UIView>
72+
{
73+
private bool _isShowingGridLines;
74+
private CGColor _gridLinesColor;
75+
private CGColor _gridFillColor;
76+
private CGColor _paddingFillColor;
77+
78+
protected override void OnElementChanged(ElementChangedEventArgs<PreviewGrid> e)
79+
{
80+
base.OnElementChanged(e);
81+
82+
if (e.NewElement != null)
83+
{
84+
UpdateBackgroundColor();
85+
UpdateShowingGridLines();
86+
UpdateGridLinesColor();
87+
}
88+
}
89+
90+
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
91+
{
92+
base.OnElementPropertyChanged(sender, e);
93+
94+
if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
95+
{
96+
UpdateBackgroundColor();
97+
}
98+
if (e.PropertyName == PreviewGrid.IsShowingGridLinesProperty.PropertyName)
99+
{
100+
UpdateShowingGridLines();
101+
}
102+
if (e.PropertyName == PreviewGrid.GridLinesColorProperty.PropertyName)
103+
{
104+
UpdateGridLinesColor();
105+
}
106+
}
107+
108+
private void UpdateBackgroundColor()
109+
{
110+
if (!(Element is PreviewGrid element) || !DesignMode.IsDesignModeEnabled)
111+
{
112+
return;
113+
}
114+
115+
Layer.BackgroundColor = element.BackgroundColor.ToCGColor();
116+
}
117+
118+
private void UpdateShowingGridLines()
119+
{
120+
if (!(Element is PreviewGrid element) || !DesignMode.IsDesignModeEnabled)
121+
{
122+
return;
123+
}
124+
125+
_isShowingGridLines = element.IsShowingGridLines;
126+
SetNeedsDisplay();
127+
}
128+
129+
private void UpdateGridLinesColor()
130+
{
131+
if (!(Element is PreviewGrid element) || !DesignMode.IsDesignModeEnabled)
132+
{
133+
return;
134+
}
135+
136+
var elementColor = element.GridLinesColor;
137+
138+
_gridLinesColor = elementColor.ToCGColor();
139+
_gridFillColor = elementColor.MultiplyAlpha(0.2f).ToCGColor();
140+
_paddingFillColor = elementColor.MultiplyAlpha(0.1f).ToCGColor();
141+
142+
SetNeedsDisplay();
143+
}
144+
145+
public override void Draw(CGRect rect)
146+
{
147+
base.Draw(rect);
148+
149+
if (!(Element is PreviewGrid element) ||
150+
!_isShowingGridLines ||
151+
!DesignMode.IsDesignModeEnabled)
152+
{
153+
return;
154+
}
155+
156+
// Reflect on Grid object to get its calculated column widths and row heights
157+
var columnDefinitions = element.ColumnDefinitions;
158+
var columnWidths = columnDefinitions.Select(
159+
d => (double)ReflectionHelper.GetPrivatePropertyValue("ActualWidth", d))
160+
.ToList();
161+
var rowDefinitions = element.RowDefinitions;
162+
var rowHeights = rowDefinitions.Select(
163+
d => (double)ReflectionHelper.GetPrivatePropertyValue("ActualHeight", d))
164+
.ToList();
165+
166+
var padding = element.Padding;
167+
var columnSpacing = (float)element.ColumnSpacing;
168+
var rowSpacing = (float)element.RowSpacing;
169+
170+
var x = (float)padding.Left;
171+
var y = (float)padding.Top;
172+
var w = (float)Bounds.Width - (float)padding.Left - (float)padding.Right;
173+
var h = (float)Bounds.Height - (float)padding.Top - (float)padding.Bottom;
174+
175+
using (var g = UIGraphics.GetCurrentContext())
176+
{
177+
g.SetLineWidth(0.5f);
178+
179+
// Fill padding area
180+
g.SaveState();
181+
182+
g.SetStrokeColor(_gridLinesColor);
183+
g.SetFillColor(_paddingFillColor);
184+
185+
var paddingPath = new CGPath();
186+
paddingPath.AddRect(new CGRect(0f, 0f, Bounds.Width, Bounds.Height));
187+
paddingPath.AddRect(new CGRect(x, y, w, h));
188+
g.AddPath(paddingPath);
189+
190+
g.DrawPath(CGPathDrawingMode.EOFillStroke);
191+
192+
g.RestoreState();
193+
194+
var gridPath = new CGPath();
195+
196+
// Draw columns
197+
// Fill column spacing & stroke column boundaries
198+
var columnX = x;
199+
for (var column = 0; column < columnWidths.Count - 1; column++)
200+
{
201+
columnX += (float)columnWidths[column];
202+
203+
// Fill column spacing
204+
g.SaveState();
205+
206+
g.SetStrokeColor(_gridFillColor);
207+
g.SetFillColor(_gridFillColor);
208+
209+
var columnSpacingPath = new CGPath();
210+
columnSpacingPath.AddRect(new CGRect(columnX, y, columnSpacing, h));
211+
g.AddPath(columnSpacingPath);
212+
213+
g.DrawPath(CGPathDrawingMode.FillStroke);
214+
215+
g.RestoreState();
216+
217+
// Add column divider line
218+
columnX += (float)columnSpacing / 2f;
219+
gridPath.MoveToPoint(columnX, y);
220+
gridPath.AddLineToPoint(columnX, y + h);
221+
columnX += (float)columnSpacing / 2f;
222+
}
223+
224+
// Draw rows
225+
// Fill row spacing & stroke row boundaries
226+
var rowY = y;
227+
for (var row = 0; row < rowHeights.Count - 1; row++)
228+
{
229+
rowY += (float)rowHeights[row];
230+
231+
// Fill row spacing
232+
g.SaveState();
233+
234+
g.SetStrokeColor(_gridFillColor);
235+
g.SetFillColor(_gridFillColor);
236+
237+
var rowSpacingPath = new CGPath();
238+
rowSpacingPath.AddRect(new CGRect(x, rowY, w, rowSpacing));
239+
g.AddPath(rowSpacingPath);
240+
241+
g.DrawPath(CGPathDrawingMode.FillStroke);
242+
243+
g.RestoreState();
244+
245+
// Add row divider line
246+
rowY += (float)rowSpacing / 2f;
247+
gridPath.MoveToPoint(x, rowY);
248+
gridPath.AddLineToPoint(x + w, rowY);
249+
rowY += (float)rowSpacing / 2f;
250+
}
251+
252+
// Draw column & row dividers
253+
g.AddPath(gridPath);
254+
g.SetStrokeColor(_gridLinesColor);
255+
g.DrawPath(CGPathDrawingMode.Stroke);
256+
}
257+
}
258+
}
259+
}
58260
```
59261

60-
I've left out most of the boilerplate code. If you want to see the whole example, it's available on my GitHub page.
262+

0 commit comments

Comments
 (0)