在Xamarin.iOS中如何绘制圆角矩形?

3

我想画一个带有圆角的矩形,但是我对这个平台还很陌生,它和WPF或UWP非常不同。

我在Objective-C中看到了一些例子,但我不知道如何将其翻译成Xamarin.iOS。

1个回答

2
一个圆角填充矩形路径:
var rectanglePath = UIBezierPath.FromRoundedRect(new CGRect(0.0f, 0.0f, 200.0f, 100.0f), 50.0f);
UIColor.Red.SetFill();
rectanglePath.Fill();

使用ImageContext创建UIImage:

UIGraphics.BeginImageContext(new CGSize(200.0f, 100.0f));
var rectanglePath = UIBezierPath.FromRoundedRect(new CGRect(0.0f, 0.0f, 200.0f, 100.0f), 50.0f);
UIColor.Red.SetFill();
rectanglePath.Fill();
var image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();

Via UIView subclass:

public class RoundedBox : UIView
{
    public RoundedBox()
    {
    }

    public RoundedBox(Foundation.NSCoder coder) : base(coder)
    {
    }

    public RoundedBox(Foundation.NSObjectFlag t) : base(t)
    {
    }

    public RoundedBox(IntPtr handle) : base(handle)
    {
    }

    public RoundedBox(CoreGraphics.CGRect frame) : base(frame)
    {
    }

    public override void Draw(CGRect rect)
    {
        var rectanglePath = UIBezierPath.FromRoundedRect(rect, 50.0f);
        UIColor.Red.SetFill();
        rectanglePath.Fill();
    }
}

非常感谢!以防万一,您能否请看一下我关于在Xamarin iOS中绘制文本的另一个问题?https://stackoverflow.com/questions/44400322/how-to-draw-text-in-xamarin-ios - SuperJMN

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接