Skip to content

Instantly share code, notes, and snippets.

@remirobert
Created April 30, 2015 12:24
Show Gist options
  • Save remirobert/318ee7c4479fc8c384ab to your computer and use it in GitHub Desktop.
Save remirobert/318ee7c4479fc8c384ab to your computer and use it in GitHub Desktop.
badge label
import UIKit
class MIBadgeLabel: UILabel {
override func drawRect(rect: CGRect)
{
// Drawing code
let ctx: CGContextRef = UIGraphicsGetCurrentContext()
let borderPath: UIBezierPath = UIBezierPath(roundedRect: rect, byRoundingCorners:UIRectCorner.AllCorners, cornerRadii: CGSizeMake(10.0, 10.0))
CGContextSetFillColorWithColor(ctx, UIColor.whiteColor().CGColor)
CGContextSaveGState(ctx)
CGContextAddPath(ctx, borderPath.CGPath)
CGContextSetLineWidth(ctx, 4.0)
CGContextSetStrokeColorWithColor(ctx, UIColor.clearColor().CGColor)
CGContextDrawPath(ctx, kCGPathStroke)
CGContextRestoreGState(ctx)
CGContextSaveGState(ctx)
var textFrame: CGRect = rect
let labelString: NSString = self.text! as NSString
let textSize: CGSize = labelString.sizeWithAttributes([NSFontAttributeName : UIFont.systemFontOfSize(13.0)])
textFrame.size.height = textSize.height
textFrame.origin.y = rect.origin.y + ceil((rect.size.height - textFrame.size.height) / 2.0)
var paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle();
paragraphStyle.alignment = .Center
var attributes: NSMutableDictionary = [NSFontAttributeName: UIFont.systemFontOfSize(13.0), NSForegroundColorAttributeName : UIColor.whiteColor(), NSParagraphStyleAttributeName:paragraphStyle]
labelString.drawInRect(textFrame, withAttributes: attributes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment