菜鸟教程小白 发表于 2022-12-12 13:33:25

c# - 如何在 Xamarin iOS 中绘制文本?


                                            <p><p>我想在自定义 <code>View</code> 的 <code>Draw</code> 方法中的给定点 (x, y) 处绘制文本。</p>

<p>我关注了<a href="https://developer.xamarin.com/recipes/ios/graphics_and_drawing/core_text/draw_unicode_text_with_coretext/" rel="noreferrer noopener nofollow">this sample</a>来自 Xamarin 网站。 </p>

<p>这是我创建的 View :</p>

<pre><code>public class MyView : UIView
{
    public override void Draw(CGRect rect)
    {
      using (var context = UIGraphics.GetCurrentContext())
      {
            DrawText(context, &#34;hello&#34;, 20, new CGPoint(0, 0));
            DrawText(context, &#34;how are you&#34;, 20, new CGPoint(0, 40));
      }
    }

    private void DrawText(CGContext context, string text, int textHeight, CGPoint point)
    {
      var x = point.X;
      var y = point.Y + textHeight;

      context.TranslateCTM(x, y);

      context.ScaleCTM(1, -1);
      context.SetFillColor(UIColor.Red.CGColor);

      var attributedString = new NSAttributedString(text,
            new CTStringAttributes
            {
                ForegroundColorFromContext = true,
                Font = new CTFont(&#34;Arial&#34;, 16)
            });

      using (var textLine = new CTLine(attributedString))
      {
            textLine.Draw(context);
      }
    }
}
</code></pre>

<p>问题在于 <code>DrawText</code> 方法<strong>只能正常工作一次</strong>。第一次调用它时,会绘制文本,但<strong>它在连续调用时不起作用</strong>(它什么也不绘制,或者它绘制的内容不可见)。</p>

<p>我做错了什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>因此,您的代码有两个基本问题。</p>

<ul>
<li>每次调用 <code>DrawText</code></li> 时,您都在执行 <code>ScaleCTM</code> 和 <code>TranslateCTM</code>
<li>您没有考虑到,当您<code>CTLine.Draw</code> 时,“光标”会移动到该文本的末尾。</li>
</ul>

<p>所以,调用 <code>ScaleCTM</code> 来翻转整个东西,使文本从左到右绘制,然后调用 <code>DrawText</code> 并转换到您要绘制文本的位置,然后 <strong >翻译回</strong>到您开始的位置,以便下次您在同一点。</p>

<h3>示例绘制覆盖:</h3>

<pre><code>public override void Draw(CGRect rect)
{
    var context = UIGraphics.GetCurrentContext();
    context.ScaleCTM(1, -1); // you flipped the context, now you must use negative Y values to draw &#34;into&#34; the view

    var textHeight = new CTFont(&#34;Arial&#34;, 16).CapHeightMetric; // lets use the actaul height of the font captials.

    DrawText(context, &#34;Hello&#34;, textHeight, 0, 0);
    DrawText(context, &#34;How are you?&#34;, textHeight, 0, 20);
    DrawText(context, &#34;Sincerely,&#34;, textHeight, 0, 40);
    DrawText(context, &#34;StackOverflow,&#34;, textHeight, 0, 60);
}

void DrawText(CGContext context, string text, nfloat textHeight, nfloat x, nfloat y)
{
    context.TranslateCTM(-x, -(y + textHeight));
    context.SetFillColor(UIColor.Red.CGColor);

    var attributedString = new NSAttributedString(text,
      new CTStringAttributes
      {
            ForegroundColorFromContext = true,
            Font = new CTFont(&#34;Arial&#34;, 16)
      });

    CGRect sizeOfText;
    using (var textLine = new CTLine(attributedString))
    {
      textLine.Draw(context);
      sizeOfText = textLine.GetBounds(CTLineBoundsOptions.UseOpticalBounds);
    }
    // Reset the origin back to where is was
    context.TranslateCTM(x - sizeOfText.Width, y + sizeOfText.Height);
}
</code></pre>

<h3>结果:</h3>

<p> <a href="/image/mgJii.png" rel="noreferrer noopener nofollow"><img src="/image/mgJii.png" alt="enter image description here"/></a> </p>

<h3>使用 NSMutableParagraphStyle 和 NSString.DrawString</h3>

<pre><code>var context = UIGraphics.GetCurrentContext();
CGRect textRect = new CGRect(0.0f, 0.0f, 200.0f, 100.0f);
{
    var textContent = &#34;Hello\nHow are you?\nSincerely,\nStackOverflow&#34;;
    UIColor.Red.SetFill();
    var textStyle = new NSMutableParagraphStyle ();
    textStyle.Alignment = UITextAlignment.Left;

    var textFontAttributes = new UIStringAttributes () {Font = UIFont.FromName(&#34;ArialMT&#34;, 16.0f), ForegroundColor = UIColor.Red, ParagraphStyle = textStyle};
    var textTextHeight = new NSString(textContent).GetBoundingRect(new CGSize(textRect.Width, nfloat.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin, textFontAttributes, null).Height;
    context.SaveState();
    context.ClipToRect(textRect);
    new NSString(textContent).DrawString(new CGRect(textRect.GetMinX(), textRect.GetMinY() + (textRect.Height - textTextHeight) / 2.0f, textRect.Width, textTextHeight), UIFont.FromName(&#34;ArialMT&#34;, 16.0f), UILineBreakMode.WordWrap, UITextAlignment.Left);
    context.RestoreState();
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于c# - 如何在 Xamarin iOS 中绘制文本?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44400322/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44400322/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c# - 如何在 Xamarin iOS 中绘制文本?