gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:54:25 25 4
gpt4 key购买 nike

我想在自定义 ViewDraw 方法中的给定点 (x, y) 绘制文本。

我关注了this sample来自 Xamarin 网站。

这是我创建的 View :

public class MyView : UIView
{
public override void Draw(CGRect rect)
{
using (var context = UIGraphics.GetCurrentContext())
{
DrawText(context, "hello", 20, new CGPoint(0, 0));
DrawText(context, "how are you", 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("Arial", 16)
});

using (var textLine = new CTLine(attributedString))
{
textLine.Draw(context);
}
}
}

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

我做错了什么?

最佳答案

所以您的代码有两个基本问题。

  • 每次调用 DrawText 时,您都在执行 ScaleCTMTranslateCTM
  • 您没有考虑到当您CTLine.Draw 时,“光标”被移动到该文本的末尾。

因此,调用 ScaleCTM 翻转整个内容,使文本从左到右绘制,然后调用 DrawText 并转换到要绘制文本的位置,然后 翻译回您开始的地方,以便下次您在同一点。

示例绘图覆盖:

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 "into" the view

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

DrawText(context, "Hello", textHeight, 0, 0);
DrawText(context, "How are you?", textHeight, 0, 20);
DrawText(context, "Sincerely,", textHeight, 0, 40);
DrawText(context, "StackOverflow,", 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("Arial", 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);
}

结果:

enter image description here

使用 NSMutableParagraphStyle 和 NSString.DrawString

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

var textFontAttributes = new UIStringAttributes () {Font = UIFont.FromName("ArialMT", 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("ArialMT", 16.0f), UILineBreakMode.WordWrap, UITextAlignment.Left);
context.RestoreState();
}

关于c# - 如何在 Xamarin iOS 中绘制文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44400322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com