Skip to content

Commit 7f7ba7d

Browse files
committed
#209: iOS text clipping
1 parent bad363d commit 7f7ba7d

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

Source/Examples/ExampleLibrary/Examples/RenderingCapabilities.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,34 @@ public static PlotModel DrawTextAlignmentRotation()
172172
return model;
173173
}
174174

175+
/// <summary>
176+
/// Shows color capabilities for the DrawText method.
177+
/// </summary>
178+
/// <returns>A plot model.</returns>
179+
[Example("DrawText - MaxSize")]
180+
public static PlotModel DrawTextMaxSize()
181+
{
182+
var model = new PlotModel();
183+
model.Annotations.Add(new DelegateAnnotation(rc =>
184+
{
185+
const string Font = "Arial";
186+
const double FontSize = 32d;
187+
const double FontWeight = FontWeights.Bold;
188+
const double D = FontSize * 1.6;
189+
const double X = 20;
190+
double y = 20 - D;
191+
var testStrings = new []{"iii","jjj","OxyPlot","Bottom","100","KML"};
192+
foreach (var text in testStrings){
193+
var maxSize = rc.MeasureText(text, Font, FontSize, FontWeight);
194+
var p=new ScreenPoint(X, y += D);
195+
rc.DrawText(p, text, OxyColors.Black, Font, FontSize, FontWeight, maxSize: maxSize);
196+
var rect=new OxyRect(p,maxSize);
197+
rc.DrawRectangle(rect,OxyColors.Undefined,OxyColors.Black);
198+
}
199+
}));
200+
return model;
201+
}
202+
175203
/// <summary>
176204
/// Draws text and shows marks for ascent/descent/baseline/x-height and the expected bounding box.
177205
/// </summary>

Source/OxyPlot.XamarinIOS/MonoTouchRenderContext.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public override void DrawText(ScreenPoint p, string text, OxyColor fill, string
315315

316316
if (maxSize.HasValue || halign != HorizontalAlignment.Left || valign != VerticalAlignment.Bottom)
317317
{
318-
width = bounds.Width;
318+
width = bounds.Left + bounds.Width;
319319
height = lineHeight;
320320
}
321321
else
@@ -338,6 +338,8 @@ public override void DrawText(ScreenPoint p, string text, OxyColor fill, string
338338

339339
var dx = halign == HorizontalAlignment.Left ? 0d : (halign == HorizontalAlignment.Center ? -width * 0.5 : -width);
340340
var dy = valign == VerticalAlignment.Bottom ? 0d : (valign == VerticalAlignment.Middle ? height * 0.5 : height);
341+
var x0 = -bounds.Left;
342+
var y0 = delta;
341343

342344
this.SetFill(fill);
343345
this.SetAlias(false);
@@ -349,12 +351,13 @@ public override void DrawText(ScreenPoint p, string text, OxyColor fill, string
349351
this.gctx.RotateCTM((float)(rotate / 180 * Math.PI));
350352
}
351353

352-
this.gctx.TranslateCTM((float)dx - bounds.Left, (float)dy + delta);
354+
this.gctx.TranslateCTM((float)dx + x0, (float)dy + y0);
353355
this.gctx.ScaleCTM(1f, -1f);
354356

355357
if (maxSize.HasValue)
356358
{
357-
this.gctx.ClipToRect(new RectangleF(0, 0, (float)Math.Ceiling(width), (float)Math.Ceiling(height)));
359+
var clipRect = new RectangleF (-x0, y0, (float)Math.Ceiling (width), (float)Math.Ceiling (height));
360+
this.gctx.ClipToRect(clipRect);
358361
}
359362

360363
textLine.Draw(this.gctx);
@@ -390,7 +393,7 @@ public override OxySize MeasureText(string text, string fontFamily, double fontS
390393
this.GetFontMetrics(font, out lineHeight, out delta);
391394
this.gctx.TextPosition = new PointF(0, 0);
392395
var bounds = textLine.GetImageBounds(this.gctx);
393-
return new OxySize(bounds.Width, lineHeight);
396+
return new OxySize(bounds.Left + bounds.Width, lineHeight);
394397
}
395398
}
396399
}

0 commit comments

Comments
 (0)