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

时间:2017-06-10 18:43:18

标签: c# .net xamarin xamarin.ios drawing

我想绘制一个带圆角的矩形,但我对平台并不熟悉,它与WPF或UWP非常不同。

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

1 个答案:

答案 0 :(得分: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();

通过UIView子类:

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