Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1.  
  2. func genSkillImageWithSkills(skills: [Skill]) -> UIImage {
  3.  
  4. let maxWidth:CGFloat = 170
  5.  
  6. let marginTop:CGFloat = 3.0
  7.  
  8. let marginLeft: CGFloat = 6.0
  9.  
  10. let lineSpacing: CGFloat = 5.0
  11.  
  12. let labelMargin: CGFloat = 5.0
  13.  
  14. var skillLabels = [CGRect]()
  15.  
  16. //let context = UIGraphicsGetCurrentContext()
  17. UIGraphicsBeginImageContextWithOptions(CGSize(width: maxWidth, height: 50), false, UIScreen.mainScreen().scale)
  18.  
  19. for (index, skill) in skills.enumerate() {
  20.  
  21. var skillLocal = skill.localName
  22.  
  23. if index == 5 {
  24. break
  25. }
  26.  
  27. if index == 4 {
  28. if skills.count > 4 && skills.count != 5 {
  29. skillLocal = NSLocalizedString("\(skills.count-4) More...", comment: "")
  30. }
  31. }
  32.  
  33. //// Text Drawing
  34. let textRect = CGRectMake(0, 0, 0, 14)
  35. let textTextContent = NSString(string: skillLocal)
  36. let textStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
  37. textStyle.alignment = .Center
  38.  
  39. let textFontAttributes: [String: AnyObject] = {
  40. if index == 4 && skills.count != 5 {
  41. return [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.yepTintColor(), NSParagraphStyleAttributeName: textStyle]
  42. } else {
  43. return [NSFontAttributeName: UIFont.systemFontOfSize(12), NSForegroundColorAttributeName: UIColor.whiteColor(), NSParagraphStyleAttributeName: textStyle]
  44. }
  45. }()
  46.  
  47. let textTextWidth: CGFloat = textTextContent.boundingRectWithSize(CGSizeMake(CGFloat.infinity, 12), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: textFontAttributes, context: nil).size.width
  48.  
  49. var rect = CGRectMake(0, marginTop, textTextWidth, textRect.height)
  50.  
  51. var lastLabel: CGRect = rect
  52.  
  53. if index > 0 {
  54. lastLabel = skillLabels[index - 1]
  55. }
  56.  
  57. var x = lastLabel.origin.x + lastLabel.width + labelMargin * 2
  58.  
  59. var y = lastLabel.origin.y
  60.  
  61. if x + rect.width + marginLeft*2 > maxWidth {
  62. x = 0
  63. y = lastLabel.origin.y + lastLabel.height + lineSpacing + marginTop*2
  64. }
  65.  
  66. if index == 0 {
  67. x = 0
  68. y = lastLabel.origin.y
  69. }
  70.  
  71. rect = CGRectMake(x + marginLeft, y , rect.width, rect.height)
  72.  
  73. let rectanglePath = UIBezierPath(roundedRect: CGRectMake(rect.origin.x - marginLeft, rect.origin.y - marginTop , textTextWidth + marginLeft * 2, textRect.height + marginTop*2), cornerRadius: (textRect.height + marginTop*2)*0.5)
  74.  
  75. let fillColor: UIColor = {
  76. if index == 4 && skills.count != 5 {
  77. return UIColor(red: 234/255.0, green: 246/255.0, blue: 255/255.0, alpha: 1.0)
  78. } else {
  79. return UIColor.yepTintColor()
  80. }
  81. }()
  82.  
  83. fillColor.setFill()
  84.  
  85. rectanglePath.fill()
  86.  
  87. skillLabels.append(rect)
  88.  
  89. textTextContent.drawInRect(rect, withAttributes: textFontAttributes)
  90. }
  91.  
  92. //CGContextSaveGState(context)
  93. //CGContextClipToRect(context, CGRectMake(0, 0, maxWidth, 50))
  94. //CGContextRestoreGState(context)
  95.  
  96. let backgroundImage = UIGraphicsGetImageFromCurrentImageContext()
  97. UIGraphicsEndImageContext()
  98.  
  99. return backgroundImage
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement