Skip to content

Instantly share code, notes, and snippets.

@vojnovski
Forked from asiviero/BorderedView.swift
Last active March 12, 2020 19:42
Show Gist options
  • Save vojnovski/819149315fad80dcc777 to your computer and use it in GitHub Desktop.
Save vojnovski/819149315fad80dcc777 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.

Updated for Swift 2.0

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 : OptionSetType, 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.init(0) }
static func fromMask(raw: UInt) -> BorderedViewDrawOptions { return self.init(raw) }
var rawValue: UInt { return self.value }
var boolValue: Bool { return self.value != 0 }
static var None: BorderedViewDrawOptions { return self.init(0) }
static var DrawTop: BorderedViewDrawOptions { return self.init(1 << 0) }
static var DrawRight: BorderedViewDrawOptions { return self.init(1 << 1) }
static var DrawBottom: BorderedViewDrawOptions { return self.init(1 << 2) }
static var DrawLeft: BorderedViewDrawOptions { return self.init(1 << 3) }
static var DrawAll: BorderedViewDrawOptions { return self.init(0b1111) }
}
class BorderedView : UIView {
var borderColor = UIColor.darkGrayColor()
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) {
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, borderColor.CGColor)
CGContextSetLineWidth(context, borderWidth);
if(borderDrawOptions.intersect(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.intersect(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.intersect(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.intersect(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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment