Skip to content

Instantly share code, notes, and snippets.

@asiviero
Last active February 23, 2017 10:27
Show Gist options
  • Save asiviero/4f52ab7dea7d9252a64c to your computer and use it in GitHub Desktop.
Save asiviero/4f52ab7dea7d9252a64c to your computer and use it in GitHub Desktop.

BorderedView

A simple subclass of UIView to allow a CSS-like approach to border width and color. It allows the user to set borders to specific corners of the View, instead of adding all around it. It does so by overriding drawRect to draw the desired lines.

Besides BorderedView.borderColor and BorderedView.borderWidth, which are the parameters set for the overall case, there's a mask which defines which borders will be drawn in BorderedView.borderDrawOptions. It's also possible to specify colors and widths that differ from the overall case.

Examples

  1. Common case
  let borderedView = BorderedView()
  borderedView.borderDrawOptions = BorderedViewDrawOptions.DrawAll
  borderedView.borderWidth = 1.0
  borderedView.borderColor = UIColor.blackColor()
  1. Only left and right borders
  let borderedView = BorderedView()
  borderedView.borderDrawOptions = BorderedViewDrawOptions.DrawLeft | BorderedViewDrawOptions.DrawRight
  borderedView.borderWidth = 1.0
  borderedView.borderColor = UIColor.blackColor()
  1. Only left and right borders, different colors
  let borderedView = BorderedView()
  borderedView.borderDrawOptions = BorderedViewDrawOptions.DrawLeft | BorderedViewDrawOptions.DrawRight
  borderedView.borderWidth = 1.0
  borderedView.borderColor = UIColor.blackColor()
  borderedView.borderColorRight = UIColor.blueColor()
  1. Only left and right borders, different colors, different widths
  let borderedView = BorderedView()
  borderedView.borderDrawOptions = BorderedViewDrawOptions.DrawLeft | BorderedViewDrawOptions.DrawRight
  borderedView.borderWidth = 1.0
  borderedView.borderColor = UIColor.blackColor()
  borderedView.borderWidthRight = 5.0
  borderedView.borderColorRight = UIColor.blueColor()
//
// BorderedView.swift
// Guinder
//
// Created by Andre Siviero on 02/07/15.
// Copyright (c) 2015 Resultate. All rights reserved.
// License: MIT
import Foundation
import UIKit
struct BorderedViewDrawOptions : RawOptionSetType, BooleanType {
typealias RawValue = UInt
private var value: UInt = 0
init(_ value: UInt) { self.value = value }
init(rawValue value: UInt) { self.value = value }
init(nilLiteral: ()) { self.value = 0 }
static var allZeros: BorderedViewDrawOptions { return self(0) }
static func fromMask(raw: UInt) -> BorderedViewDrawOptions { return self(raw) }
var rawValue: UInt { return self.value }
var boolValue: Bool { return self.value != 0 }
static var None: BorderedViewDrawOptions { return self(0) }
static var DrawTop: BorderedViewDrawOptions { return self(1 << 0) }
static var DrawRight: BorderedViewDrawOptions { return self(1 << 1) }
static var DrawBottom: BorderedViewDrawOptions { return self(1 << 2) }
static var DrawLeft: BorderedViewDrawOptions { return self(1 << 3) }
static var DrawAll: BorderedViewDrawOptions { return self(0b1111) }
}
class BorderedView : UIView {
var borderColor = UIColor()
var borderWidth = CGFloat(0.0)
var borderDrawOptions = BorderedViewDrawOptions.None
// Optionals to allow further customization
var borderColorLeft:UIColor?
var borderColorTop:UIColor?
var borderColorRight:UIColor?
var borderColorBottom:UIColor?
var borderWidthLeft:CGFloat?
var borderWidthTop:CGFloat?
var borderWidthRight:CGFloat?
var borderWidthBottom:CGFloat?
override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, borderColor.CGColor)
CGContextSetLineWidth(context, borderWidth);
if(borderDrawOptions & BorderedViewDrawOptions.DrawLeft) {
CGContextSetStrokeColorWithColor(context, borderColorLeft?.CGColor != nil ? borderColorLeft!.CGColor : borderColor.CGColor)
CGContextSetLineWidth(context, borderWidthLeft != nil ? borderWidthLeft! : borderWidth);
CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextAddLineToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect));
CGContextStrokePath(context);
borderResetDefaultColorWidth(context)
}
if(borderDrawOptions & BorderedViewDrawOptions.DrawTop) {
CGContextSetLineWidth(context, borderWidthTop != nil ? borderWidthTop! : borderWidth);
CGContextSetStrokeColorWithColor(context, borderColorTop?.CGColor != nil ? borderColorTop!.CGColor : borderColor.CGColor)
CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGContextStrokePath(context);
borderResetDefaultColorWidth(context)
}
if(borderDrawOptions & BorderedViewDrawOptions.DrawRight) {
CGContextSetLineWidth(context, borderWidthRight != nil ? borderWidthRight! : borderWidth);
CGContextSetStrokeColorWithColor(context, borderColorRight?.CGColor != nil ? borderColorRight!.CGColor : borderColor.CGColor)
CGContextMoveToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
CGContextStrokePath(context);
borderResetDefaultColorWidth(context)
}
if(borderDrawOptions & BorderedViewDrawOptions.DrawBottom) {
CGContextSetLineWidth(context, borderWidthBottom != nil ? borderWidthBottom! : borderWidth);
CGContextSetStrokeColorWithColor(context, borderColorBottom?.CGColor != nil ? borderColorBottom!.CGColor : borderColor.CGColor)
CGContextMoveToPoint(context, CGRectGetMinX(rect), CGRectGetMaxY(rect));
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
CGContextStrokePath(context);
borderResetDefaultColorWidth(context)
}
}
func borderResetDefaultColorWidth(context: CGContextRef) -> Void {
CGContextSetStrokeColorWithColor(context, borderColor.CGColor)
CGContextSetLineWidth(context, borderWidth);
}
}
@vojnovski
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment