【问题标题】:How to draw a rounded rectangle in Xamarin.iOS?如何在 Xamarin.iOS 中绘制圆角矩形?
【发布时间】:2017-06-10 18:43:17
【问题描述】:

我想绘制一个带圆角的矩形,但我对这个平台很陌生,它与例如 WPF 或 UWP 确实不同。

我看过 Objective-C 中的示例,但我不知道如何翻译成 Xamarin.iOS。

【问题讨论】:

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


    【解决方案1】:

    圆角填充矩形路径:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 2013-12-17
    相关资源
    最近更新 更多