如何在Xamarin iOS中使用半透明背景在图像上绘制文字?

时间:2017-05-19 07:13:32

标签: ios xamarin xamarin.ios

我有一个UIImage我想在图像上绘制文字。我希望它看起来像这样: enter image description here

我正在使用此代码绘制文字:

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());
    }
}

它正确地绘制文本。问题是它周围没有灰色矩形,用灰色填充空间。我怎么能做到这一点?

0 个答案:

没有答案