gpt4 book ai didi

ios - 使用 MonoTouch.CoreText 绘制多行文本

转载 作者:行者123 更新时间:2023-11-29 01:50:03 24 4
gpt4 key购买 nike

我用了那个例子 http://developer.xamarin.com/recipes/ios/graphics_and_drawing/core_text/draw_unicode_text_with_coretext/

Using MonoTouch.CoreText to draw text fragments at specific coordinaates

在 UIView 上绘制文本线。

现在我需要扩展它来绘制多行文本。基本上很简单。在 Draw() 方法中,我在“\n”上拆分多行字符串 Text,然后为任何向 Y 添加 newLineDY 的单行调用 DrawTextLine()。唯一的问题是任何新的线条绘制都会在前一条线条的 X 绘制结束之后开始:

啊啊啊 bbb 抄送

如何避免X位移?可以重置吗?如何?我尝试对任何行应用负 DX,但我不知道要应用的正确值。

private const float newLineDY = 40;
public override void Draw()
{
string[] lines = Text.Split("\n".ToCharArray());
float lx = X;
float ly = Y;
foreach (string line in lines)
{
DrawTextLine(line, lx, ly);
//lx -= 100; // negative DX
ly += newLineDY;
}
}
private void DrawTextLine(string text, float x, float y)
{
CGContext gctx = UIGraphics.GetCurrentContext();
gctx.SaveState();
gctx.TranslateCTM(x, y);
//gctx.TextPosition = new CGPoint(x, y);
gctx.ScaleCTM(1, -1);
//gctx.RotateCTM((float)Math.PI * 315 / 180);

gctx.SetFillColor(UIColor.Black.CGColor);

var attributedString = new NSAttributedString(text,
new CTStringAttributes
{
ForegroundColorFromContext = true,
Font = new CTFont("Arial", 24)
});

using (CTLine textLine = new CTLine(attributedString))
{
textLine.Draw(gctx);
}
gctx.RestoreState();
}

谢谢!

最佳答案

我已经使用 attributedString.DrawString(new CGPoint(x, y)) 解决了问题,这是一个更简单的 API,如此处所建议的那样

http://monotouch.2284126.n4.nabble.com/Using-MonoTouch-CoreText-to-draw-text-fragments-at-specific-coordinates-td4658531.html

所以我的代码变成了:

    private const float newLineDY = 40;
public override void Draw()
{
string[] lines = Text.Split("\n".ToCharArray());
float lx = X;
float ly = Y;
foreach (string line in lines)
{
DrawTextLine(line, lx, ly);
ly += newLineDY;
}
}
private void DrawTextLine(string text, float x, float y)
{
NSAttributedString attributedString = new NSAttributedString(
text,
new CTStringAttributes
{
ForegroundColorFromContext = true,
Font = new CTFont("Arial", 24)
});
attributedString.DrawString(new CGPoint(x, y));
}

关于ios - 使用 MonoTouch.CoreText 绘制多行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31512065/

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