Created
April 30, 2015 12:24
-
-
Save remirobert/318ee7c4479fc8c384ab to your computer and use it in GitHub Desktop.
badge label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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