├── README └── KBButton.swift /README: -------------------------------------------------------------------------------- 1 | KBButton Class (Swift) 2 | Custom Button class with following features 3 | 4 | IBInspectable UI views to change 5 | a) Border Color 6 | b) Border width 7 | c) Corner Radius 8 | d) Underline title 9 | e) Badge Count 10 | f) Background color for normal and selected state of button 11 | 12 | How to use : 13 | Add KBButton to your project 14 | Replace name of class from UIButton to KBButton -------------------------------------------------------------------------------- /KBButton.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KBButton.swift 3 | // TURNSTR 4 | // 5 | // Created by Apple on 22/02/16. 6 | // Copyright © 2016 Neophyte. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @IBDesignable 12 | 13 | class KBButton: UIButton { 14 | 15 | 16 | @IBInspectable var borderColor : UIColor?{ 17 | 18 | didSet{ 19 | layer.borderColor = borderColor?.CGColor; 20 | } 21 | } 22 | @IBInspectable var borderWidth : CGFloat = 1.0{ 23 | didSet{ 24 | layer.borderWidth = borderWidth; 25 | } 26 | } 27 | @IBInspectable var cornerRadius : CGFloat = 1.0{ 28 | didSet{ 29 | layer.cornerRadius = cornerRadius; 30 | layer.masksToBounds = cornerRadius > 0 31 | } 32 | } 33 | @IBInspectable var selectedColor : UIColor = UIColor.clearColor(){ 34 | didSet{ 35 | setBackgroundImage(imageFromColor(selectedColor), forState: .Selected) 36 | } 37 | } 38 | @IBInspectable var normalColor : UIColor = UIColor.clearColor(){ 39 | didSet{ 40 | setBackgroundImage(imageFromColor(normalColor), forState: .Normal) 41 | } 42 | } 43 | @IBInspectable var underline : Bool = false{ 44 | didSet{ 45 | if (underline){ 46 | let btnTitle : NSMutableAttributedString = NSMutableAttributedString(attributedString: NSAttributedString(string: self.currentTitle!)) 47 | let range : NSRange = NSMakeRange(0, (self.currentTitle?.characters.count)!) 48 | btnTitle.addAttributes([NSUnderlineStyleAttributeName : NSUnderlineStyle.StyleSingle.rawValue, NSForegroundColorAttributeName : (self.titleLabel?.textColor)!], range: range) 49 | self.setAttributedTitle(btnTitle, forState: self.state) 50 | } 51 | else{ 52 | self.setTitle(self.currentTitle, forState: self.state) 53 | } 54 | } 55 | } 56 | 57 | @IBInspectable var badgeValue: String? = "0"{ 58 | didSet{ 59 | let lblBadge = UILabel(frame: CGRectMake(frame.width - 13.0, 0.0, 12.0, 12.0)) 60 | lblBadge.text = badgeValue 61 | lblBadge.textAlignment = .Center 62 | lblBadge.adjustsFontSizeToFitWidth = true 63 | lblBadge.backgroundColor = UIColor(red: 249.0/255.0, green: 142.0/255.0, blue: 32.0/255.0, alpha: 1.0) 64 | lblBadge.textColor = UIColor.whiteColor() 65 | lblBadge.font = UIFont.systemFontOfSize(8.0) 66 | lblBadge.sizeToFit() 67 | lblBadge.layer.cornerRadius = lblBadge.frame.size.height/2 68 | lblBadge.clipsToBounds = true 69 | addSubview(lblBadge) 70 | } 71 | } 72 | 73 | func imageFromColor(color: UIColor) -> UIImage{ 74 | let rect = CGRectMake(0.0, 0.0, 1.0, 1.0); 75 | UIGraphicsBeginImageContext(rect.size); 76 | let context = UIGraphicsGetCurrentContext(); 77 | CGContextSetFillColorWithColor(context, color.CGColor); 78 | CGContextFillRect(context, rect); 79 | let image = UIGraphicsGetImageFromCurrentImageContext(); 80 | UIGraphicsEndImageContext(); 81 | 82 | return image 83 | } 84 | 85 | } 86 | --------------------------------------------------------------------------------