Advertisement
andoird213

@IBDesignable Help

Jul 26th, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.76 KB | None | 0 0
  1. //CircleView.swift
  2.  
  3. import UIKit
  4.  
  5. @IBDesignable
  6. class CircleView: UIView {
  7.  
  8.     override func drawRect(rect: CGRect) {
  9.         CustomStyleKit.drawCircleView(circleViewColor: UIColor.blueColor(), tLCImage: UIImage(named: "no-photo.png")!, tRCImage: UIImage(named: "no-photo.png")!, bLCImage: UIImage(named: "no-photo.png")!, bRCImage: UIImage(named: "no-photo.png")!, tag: "tag", scale: 1.5)
  10.     }
  11.    
  12.     override func prepareForInterfaceBuilder() {
  13.         CustomStyleKit.drawCircleView(circleViewColor: UIColor.blueColor(), tLCImage: UIImage(named: "no-photo.png")!, tRCImage: UIImage(named: "no-photo.png")!, bLCImage: UIImage(named: "no-photo.png")!, bRCImage: UIImage(named: "no-photo.png")!, tag: "tag", scale: 1.5)
  14.     }
  15.  
  16. }
  17.  
  18.  
  19. //CustomStyleKit.swift
  20.  
  21. import UIKit
  22.  
  23. public class CustomStyleKit : NSObject {
  24.  
  25.     //// Drawing Methods
  26.  
  27.     public class func drawCircleView(#circleViewColor: UIColor, tLCImage: UIImage, tRCImage: UIImage, bLCImage: UIImage, bRCImage: UIImage, tag: String, scale: CGFloat) {
  28.         //// General Declarations
  29.         let context = UIGraphicsGetCurrentContext()
  30.  
  31.         //// Group
  32.         CGContextSaveGState(context)
  33.         CGContextTranslateCTM(context, 101, 100.5)
  34.         CGContextScaleCTM(context, scale, scale)
  35.  
  36.  
  37.  
  38.         //// OuterCircle Drawing
  39.         CGContextSaveGState(context)
  40.         CGContextTranslateCTM(context, -50, -50)
  41.         CGContextScaleCTM(context, 2, 2)
  42.  
  43.         var outerCirclePath = UIBezierPath(ovalInRect: CGRectMake(0, 0, 50, 50))
  44.         circleViewColor.setStroke()
  45.         outerCirclePath.lineWidth = 2
  46.         outerCirclePath.stroke()
  47.  
  48.         CGContextRestoreGState(context)
  49.  
  50.  
  51.         //// TextCircle Drawing
  52.         let textCircleRect = CGRectMake(-51, -12, 100, 24)
  53.         var textCirclePath = UIBezierPath(ovalInRect: textCircleRect)
  54.         circleViewColor.setFill()
  55.         textCirclePath.fill()
  56.         let textCircleStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
  57.         textCircleStyle.alignment = NSTextAlignment.Center
  58.  
  59.         let textCircleFontAttributes = [NSFontAttributeName: UIFont(name: "Helvetica", size: 9)!, NSForegroundColorAttributeName: UIColor.whiteColor(), NSParagraphStyleAttributeName: textCircleStyle]
  60.  
  61.         let textCircleTextHeight: CGFloat = NSString(string: tag).boundingRectWithSize(CGSizeMake(textCircleRect.width, CGFloat.infinity), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: textCircleFontAttributes, context: nil).size.height
  62.         CGContextSaveGState(context)
  63.         CGContextClipToRect(context, textCircleRect);
  64.         NSString(string: tag).drawInRect(CGRectMake(textCircleRect.minX, textCircleRect.minY + (textCircleRect.height - textCircleTextHeight) / 2, textCircleRect.width, textCircleTextHeight), withAttributes: textCircleFontAttributes)
  65.         CGContextRestoreGState(context)
  66.  
  67.  
  68.         //// TopDivider Drawing
  69.         var topDividerPath = UIBezierPath()
  70.         topDividerPath.moveToPoint(CGPointMake(-0.5, -50.5))
  71.         topDividerPath.addLineToPoint(CGPointMake(-0.5, -11.5))
  72.         topDividerPath.addLineToPoint(CGPointMake(-0.5, -50.5))
  73.         topDividerPath.closePath()
  74.         circleViewColor.setStroke()
  75.         topDividerPath.lineWidth = 3
  76.         topDividerPath.stroke()
  77.  
  78.  
  79.         //// BottomDivider Drawing
  80.         var bottomDividerPath = UIBezierPath()
  81.         bottomDividerPath.moveToPoint(CGPointMake(-0.5, 11.5))
  82.         bottomDividerPath.addLineToPoint(CGPointMake(-0.5, 50.5))
  83.         bottomDividerPath.addLineToPoint(CGPointMake(-0.5, 11.5))
  84.         bottomDividerPath.closePath()
  85.         circleViewColor.setStroke()
  86.         bottomDividerPath.lineWidth = 3
  87.         bottomDividerPath.stroke()
  88.  
  89.  
  90.  
  91.         CGContextRestoreGState(context)
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement