#Add a top-right banner to your view ##Usage
let banner = CMBannerView(frame: CGRectMake(0, 0, 120, 120), text: "New", color: UIColor.redColor(), width: 30)
myView.addSubview(banner)
##Implementation
//
// CMBannerView.swift
//
//
// Created by Célian MOUTAFIS on 18/01/2016.
// Copyright © 2016 Mouce All rights reserved.
//
import UIKit
@IBDesignable
public class SRBannerView: UIView {
var OFFSET : CGFloat = 25.0
var WIDTH : CGFloat = 25.0;
var text = NSLocalizedString("NEW",comment:"")
var color = UIColor.redColor()
var font : UIFont = UIFont(name: "AvenirNext-Medium", size: 14)!
var fontColor : UIColor = UIColor.whiteColor()
init(frame : CGRect, text: String, color : UIColor = UIColor.redColor(), fontColor : UIColor = UIColor.whiteColor(), width : CGFloat = 25.0, font : UIFont = UIFont(name: "AvenirNext-Medium", size: 14)!){
self.text = text
self.WIDTH = width
self.color = color
self.fontColor = fontColor
self.font = font
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override public func drawRect(rect: CGRect) {
self.drawBackground(rect);
self.drawText(rect)
}
private func drawBackground(rect: CGRect){
self.backgroundColor = UIColor.clearColor()
let color = self.color
color.setFill();
let path = UIBezierPath()
path.moveToPoint(CGPointMake(CGRectGetMaxX(rect) - WIDTH - OFFSET, 0));
path.addLineToPoint(CGPointMake(CGRectGetMaxX(rect) - OFFSET, 0));
path.addLineToPoint(CGPointMake(CGRectGetMaxX(rect), OFFSET));
path.addLineToPoint(CGPointMake(CGRectGetMaxX(rect) , OFFSET + WIDTH));
path.addLineToPoint(CGPointMake(CGRectGetMaxX(rect) - WIDTH - OFFSET, 0));
path.closePath();
path.fill();
}
private func drawText(rect : CGRect){
// set the text color to white
let fieldColor: UIColor = UIColor.whiteColor()
// set the font
let fieldFont = self.font
let attributes: [String : AnyObject] = [
NSForegroundColorAttributeName: fieldColor,
NSFontAttributeName: fieldFont
]
let text : NSString = self.text
let size = text.sizeWithAttributes(attributes)
let ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx)
//Translate context along X axis
let t : CGAffineTransform = CGAffineTransformMakeTranslation(CGRectGetMaxX(rect) - OFFSET, 0 )
CGContextConcatCTM(ctx,t)
//rotate context around Z-Axis
let r : CGAffineTransform = CGAffineTransformMakeRotation(CGFloat(M_PI / 4.0))
CGContextConcatCTM(ctx,r)
//Translate context to center the text along X-Axis
let delta_X = (sqrt(2 * OFFSET * OFFSET ) - size.width) / 2
let t2:CGAffineTransform = CGAffineTransformMakeTranslation( delta_X , 0 )
CGContextConcatCTM(ctx, t2)
//Translate context to center the text along Y-Axis
let delta_Y = ( sqrt(2 * pow(WIDTH + OFFSET,2)) - sqrt (2 * pow(OFFSET, 2))) / 2
let t3:CGAffineTransform = CGAffineTransformMakeTranslation(0 , (delta_Y - size.height ) / 2)
CGContextConcatCTM(ctx, t3)
//Finally draaw the text
text.drawInRect(rect, withAttributes: attributes)
CGContextRestoreGState(ctx);
}
}