Skip to content

Instantly share code, notes, and snippets.

@alexrainman
Last active April 25, 2019 16:15
Show Gist options
  • Save alexrainman/59ea0ee9e829f17d5630 to your computer and use it in GitHub Desktop.
Save alexrainman/59ea0ee9e829f17d5630 to your computer and use it in GitHub Desktop.

Revisions

  1. alexrainman revised this gist Jun 7, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UIViewWithBorders.cs
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ namespace YourNamespace
    {
    /*
    UIView subview that allows individual borders to be drawn.
    Nil colors for a side will not be drawn. If borderColorAll
    Null colors for a side will not be drawn. If borderColorAll
    is used, it will have priority and each individual color does
    not need to be assigned. Be sure to set borderWidths accordingly.
    Borders are drawn from the view's edge inward.
  2. alexrainman revised this gist Jun 7, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion UIViewWithBorders.cs
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ public class UIViewWithBorders : UIView
    public UIColor BorderColorTop { get; set; }
    public UIColor BorderColorBottom { get; set; }
    public UIColor BorderColorLeft { get; set; }
    public UIColor BorderColorRight { get; set; }
    public UIColor BorderColorRight { get; set; }

    public UIViewWithBorders(CGRect frame) : base(frame)
    {
  3. alexrainman renamed this gist Jun 7, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. alexrainman created this gist Jun 7, 2015.
    83 changes: 83 additions & 0 deletions UIViewWithBorders
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    using System;
    using UIKit;
    using CoreGraphics;

    // ported from https://github.com/natrosoft/NAUIViewWithBorders

    namespace YourNamespace
    {
    /*
    UIView subview that allows individual borders to be drawn.
    Nil colors for a side will not be drawn. If borderColorAll
    is used, it will have priority and each individual color does
    not need to be assigned. Be sure to set borderWidths accordingly.
    Borders are drawn from the view's edge inward.
    */
    public class UIViewWithBorders : UIView
    {
    /* If set, overrides individual widths */
    public nfloat BorderWidthAll { get; set; }
    /* If set, overrides individual colors */
    public UIColor BorderColorAll { get; set; }

    /* For specifying individual widths */
    public UIEdgeInsets BorderWidth { get; set; }
    public UIColor BorderColorTop { get; set; }
    public UIColor BorderColorBottom { get; set; }
    public UIColor BorderColorLeft { get; set; }
    public UIColor BorderColorRight { get; set; }

    public UIViewWithBorders(CGRect frame) : base(frame)
    {
    }

    public override void Draw (CGRect rect)
    {
    base.Draw (rect);

    if (BorderWidthAll > 0) {
    BorderWidth = new UIEdgeInsets(BorderWidthAll, BorderWidthAll, BorderWidthAll, BorderWidthAll);
    }

    if (BorderColorAll != null) {
    BorderColorTop = BorderColorBottom = BorderColorLeft = BorderColorRight = BorderColorAll;
    }

    var xMin = rect.GetMinX();
    var xMax = rect.GetMaxX ();

    var yMin = rect.GetMinY ();
    var yMax = rect.GetMaxY ();

    var fWidth = this.Frame.Size.Width;
    var fHeight = this.Frame.Size.Height;

    var context = UIGraphics.GetCurrentContext();

    DrawBorders (context, xMin, xMax, yMin, yMax, fWidth, fHeight);
    }

    void DrawBorders(CGContext context, nfloat xMin, nfloat xMax, nfloat yMin, nfloat yMax, nfloat fWidth, nfloat fHeight)
    {
    if (BorderColorTop != null) {
    context.SetFillColor (BorderColorTop.CGColor);
    context.FillRect (new CGRect(xMin, yMin, fWidth, BorderWidth.Top));
    }

    if (BorderColorLeft != null) {
    context.SetFillColor (BorderColorLeft.CGColor);
    context.FillRect (new CGRect (xMin, yMin, BorderWidth.Left, fHeight));
    }

    if (BorderColorRight != null) {
    context.SetFillColor (BorderColorRight.CGColor);
    context.FillRect (new CGRect(xMax - BorderWidth.Right, yMin, BorderWidth.Right, fHeight));
    }

    if (BorderColorBottom != null) {
    context.SetFillColor (BorderColorBottom.CGColor);
    context.FillRect (new CGRect(xMin, yMax - BorderWidth.Bottom, fWidth, BorderWidth.Bottom));
    }
    }
    }
    }