我有一个UIImage我想在图像上绘制文字。我希望它看起来像这样:
我正在使用此代码绘制文字:
private static UIImage DrawText(UIImage uiImage, string sText, UIColor textColor, int iFontSize)
{
nfloat fWidth = uiImage.Size.Width;
nfloat fHeight = uiImage.Size.Height;
using (CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, (nint)fWidth, (nint)fHeight, 8, 4 * (nint)fWidth, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst))
{
ctx.DrawImage(new CGRect(0, 0, (double)fWidth, (double)fHeight), uiImage.CGImage);
ctx.SelectFont("Helvetica", iFontSize, CGTextEncoding.MacRoman);
nfloat fRed, fGreen, fBlue, fAlpha;
textColor.GetRGBA(out fRed, out fGreen, out fBlue, out fAlpha);
ctx.SetFillColor(fRed, fGreen, fBlue, fAlpha);
ctx.SetTextDrawingMode(CGTextDrawingMode.Fill);
ctx.ShowTextAtPoint(fWidth / 4, fHeight / 10, sText);
return UIImage.FromImage(ctx.ToImage());
}
}
它正确地绘制文本。问题是它周围没有灰色矩形,用灰色填充空间。我怎么能做到这一点?