Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andriadze committed Sep 28, 2017
0 parents commit 29ba715
Show file tree
Hide file tree
Showing 59 changed files with 3,819 additions and 0 deletions.
40 changes: 40 additions & 0 deletions CircleLabel.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Be sure to run `pod lib lint CircleLabel.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
s.name = 'CircleLabel'
s.version = '1.0.0'
s.summary = 'Labels with circle backgrounds'



s.description = <<-DESC
Genererate labels with circle backgrounds.
Use text based color generator with custom schemes or choose your own color.
Library has full Storyboard integration.
DESC

s.homepage = 'https://github.com/hoomazoid/CircleLabel'
s.screenshots = 'https://i.imgur.com/OX0ZniH.jpg'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'Giorgi Andriadze' => 'g.andriadze2@gmail.com' }
s.source = { :git => 'https://github.com/hoomazoid/CircleLabel.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

s.ios.deployment_target = '8.0'

s.source_files = 'CircleLabel/Classes/**/*'

# s.resource_bundles = {
# 'CircleLabel' => ['CircleLabel/Assets/*.png']
# }

# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end
Empty file added CircleLabel/Assets/.gitkeep
Empty file.
Empty file added CircleLabel/Classes/.gitkeep
Empty file.
172 changes: 172 additions & 0 deletions CircleLabel/Classes/CircleLabel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
//
// CircularImageView.swift
// CircularText
//
// Created by Gio Andriadze on 9/27/17.
// Copyright © 2017 Casatrade Ltd. All rights reserved.
//

import UIKit

@IBDesignable
class CircleLabel : UIImageView
{
static let DEFAULT_COLOR_SCHEME:[UInt32] = [
0xfff16364,
0xfff58559,
0xfff9a43e,
0xffe4c62e,
0xff67bf74,
0xff59a2be,
0xff2093cd,
0xffad62a7,
0xff805781
]

var COLOR_SCHEME:[UInt32] = CircleLabel.DEFAULT_COLOR_SCHEME{
didSet{
setCircleAndText();
}
}

@IBInspectable var textColor:UIColor = UIColor.white{
didSet{
setCircleAndText()
}
}

@IBInspectable var text:String = "" {
didSet{
setCircleAndText()
}
}
@IBInspectable var colorFromText:String = ""{
didSet{
setCircleAndText()
}
}
@IBInspectable var padding:Float = 0.3{
didSet{
setCircleAndText()
}
}

@IBInspectable var useTextColor:Bool = true{
didSet{
setCircleAndText()
}
}

@IBInspectable var circleColor:UIColor = UIColor.gray{
didSet{
if(!useTextColor){
setCircleAndText()
}
}
}

@IBInspectable var amountLines:Int = 1{
didSet{
setCircleAndText()
}
}


override init(frame:CGRect)
{
super.init(frame: frame)

setCircleAndText();
}

init(frame:CGRect, text:String, colorText:String)
{

super.init(frame: frame)
self.text = text;
self.colorFromText = colorText;

setCircleAndText();
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setCircleAndText();

}

private func setCircleAndText() {
self.contentMode = .scaleAspectFit

let color = (useTextColor ? randomColorFrom(text: colorFromText) : circleColor)

self.image = circle(diameter: self.frame.size.height, color: color)
self.superview?.layoutIfNeeded()

}


private func circle(diameter: CGFloat, color: UIColor) -> UIImage? {
//temp view to position label
if(padding < 0.3)
{
padding = 0.3
}


let tempView = UIView(frame: CGRect(x: 0, y: 0, width:diameter, height:diameter));
//main label
let label = UILabel(frame: CGRect(x:0, y: 0, width:diameter/(CGFloat)(1 + padding), height:diameter/(CGFloat)(1 + padding)))
label.backgroundColor = UIColor.clear
label.textColor = self.textColor
label.numberOfLines = amountLines;
label.adjustsFontSizeToFitWidth = true;
label.minimumScaleFactor = 0.01
label.text = text
label.textAlignment = .center
label.baselineAdjustment = .alignCenters;
label.font = label.font.withSize(diameter)
label.center = tempView.center


tempView.addSubview(label)

UIGraphicsBeginImageContextWithOptions(CGSize(width: diameter, height: diameter), false, 0)
guard let ctx = UIGraphicsGetCurrentContext() else { return nil }
ctx.saveGState()
//Draw the circle

let rect = CGRect(x: 0, y: 0, width: diameter, height: diameter)
ctx.setFillColor(color.cgColor)
ctx.fillEllipse(in: rect)
ctx.restoreGState()
//render view with label
tempView.layer.render(in:ctx)

let img = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

return img
}

private func randomColorFrom(text: String) -> UIColor
{
let rgb:UInt32 = COLOR_SCHEME[abs(text.hashValue) % CircleLabel.DEFAULT_COLOR_SCHEME.count]
let red:CGFloat = CGFloat((rgb & 0xFF0000) >> 16)/255.0;
let green = CGFloat((rgb & 0xFF00) >> 8)/255.0;
let blue = CGFloat((rgb & 0xFF))/255.0

return UIColor(red: red, green: green, blue: blue, alpha: 1)

}

override func prepareForInterfaceBuilder()
{
setCircleAndText();
}





}
Loading

0 comments on commit 29ba715

Please sign in to comment.