Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new property "tokenCornerRadius" #76

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion KSTokenView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Pod::Spec.new do |s|


# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
s.ios.deployment_target = '9.0'
s.ios.deployment_target = '8.0'


# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
310 changes: 155 additions & 155 deletions KSTokenView/KSToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,164 +29,164 @@ import UIKit
//__________________________________________________________________________________
//
open class KSToken : UIControl {

//MARK: - Public Properties
//__________________________________________________________________________________
//

/// retuns title as description
override open var description : String {
get {
return title

//MARK: - Public Properties
//__________________________________________________________________________________
//

/// retuns title as description
override open var description : String {
get {
return title
}
}

/// default is ""
open var title = ""

/// default is nil. Any Custom object.
open var object: AnyObject?

/// default is false. If set to true, token can not be deleted
open var sticky = false

/// default is 15
open var tokenCornerRadius:CGFloat = 15.0

/// Token Title color
open var tokenTextColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1)

/// Token background color
open var tokenBackgroundColor = UIColor(red: 50/255, green: 50/255, blue: 255/255, alpha: 1)

/// Token title color in selected state
open var tokenTextHighlightedColor: UIColor?

/// Token backgrould color in selected state
open var tokenBackgroundHighlightedColor: UIColor?

/// Token background color in selected state. It doesn't have effect if 'tokenBackgroundHighlightedColor' is set
open var darkRatio: CGFloat = 0.75

/// Token border width
open var borderWidth: CGFloat = 0.0

///Token border color
open var borderColor: UIColor = UIColor.black

/// default is 200. Maximum width of token. After maximum limit is reached title is truncated at end with '...'
fileprivate var _maxWidth: CGFloat? = 200
open var maxWidth: CGFloat {
get{
return _maxWidth!
}
set (newWidth) {
if (_maxWidth != newWidth) {
_maxWidth = newWidth
sizeToFit()
setNeedsDisplay()
}
}

/// default is ""
open var title = ""

/// default is nil. Any Custom object.
open var object: AnyObject?

/// default is false. If set to true, token can not be deleted
open var sticky = false

/// Token Title color
open var tokenTextColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1)

/// Token background color
open var tokenBackgroundColor = UIColor(red: 50/255, green: 50/255, blue: 255/255, alpha: 1)

/// Token title color in selected state
open var tokenTextHighlightedColor: UIColor?

/// Token backgrould color in selected state
open var tokenBackgroundHighlightedColor: UIColor?

/// Token background color in selected state. It doesn't have effect if 'tokenBackgroundHighlightedColor' is set
open var darkRatio: CGFloat = 0.75

/// Token border width
open var borderWidth: CGFloat = 0.0

///Token border color
open var borderColor: UIColor = UIColor.black

/// default is 200. Maximum width of token. After maximum limit is reached title is truncated at end with '...'
fileprivate var _maxWidth: CGFloat? = 200
open var maxWidth: CGFloat {
get{
return _maxWidth!
}
set (newWidth) {
if (_maxWidth != newWidth) {
_maxWidth = newWidth
sizeToFit()
setNeedsDisplay()
}
}
}

/// returns true if token is selected
override open var isSelected: Bool {
didSet (newValue) {
setNeedsDisplay()
}
}

//MARK: - Constructors
//__________________________________________________________________________________
//
convenience required public init(coder aDecoder: NSCoder) {
self.init(title: "")
}

convenience public init(title: String) {
self.init(title: title, object: title as AnyObject?);
}

public init(title: String, object: AnyObject?) {
self.title = title
self.object = object
super.init(frame: CGRect.zero)
backgroundColor = UIColor.clear
}

//MARK: - Drawing code
//__________________________________________________________________________________
//
override open func draw(_ rect: CGRect) {
//// General Declarations
let context = UIGraphicsGetCurrentContext()

//// Rectangle Drawing

// fill background
let rectanglePath = UIBezierPath(roundedRect: rect, cornerRadius: 15)

var textColor: UIColor
var backgroundColor: UIColor

if (isSelected) {
if (tokenBackgroundHighlightedColor != nil) {
backgroundColor = tokenBackgroundHighlightedColor!
} else {
backgroundColor = tokenBackgroundColor.darkendColor(darkRatio)
}

if (tokenTextHighlightedColor != nil) {
textColor = tokenTextHighlightedColor!
} else {
textColor = tokenTextColor
}

}
}

/// returns true if token is selected
override open var isSelected: Bool {
didSet (newValue) {
setNeedsDisplay()
}
}

//MARK: - Constructors
//__________________________________________________________________________________
//
convenience required public init(coder aDecoder: NSCoder) {
self.init(title: "")
}

convenience public init(title: String) {
self.init(title: title, object: title as AnyObject?);
}

public init(title: String, object: AnyObject?) {
self.title = title
self.object = object
super.init(frame: CGRect.zero)
backgroundColor = UIColor.clear
}

//MARK: - Drawing code
//__________________________________________________________________________________
//
override open func draw(_ rect: CGRect) {
//// General Declarations
let context = UIGraphicsGetCurrentContext()

//// Rectangle Drawing

// fill background
let rectanglePath = UIBezierPath(roundedRect: rect, cornerRadius: tokenCornerRadius)

var textColor: UIColor = tokenTextColor
var backgroundColor: UIColor

if (isSelected) {
if (tokenBackgroundHighlightedColor != nil) {
backgroundColor = tokenBackgroundHighlightedColor!
} else {
backgroundColor = tokenBackgroundColor
textColor = tokenTextColor
}

backgroundColor.setFill()
rectanglePath.fill()

var paddingX: CGFloat = 0.0
var font = UIFont.systemFont(ofSize: 14)
var tokenField: KSTokenField? {
return superview! as? KSTokenField
}
if ((tokenField) != nil) {
paddingX = tokenField!.paddingX()!
font = tokenField!.tokenFont()!
backgroundColor = tokenBackgroundColor.darkendColor(darkRatio)
}

// Text
let rectangleTextContent = title
let rectangleStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
rectangleStyle.lineBreakMode = NSLineBreakMode.byTruncatingTail
rectangleStyle.alignment = NSTextAlignment.center
let rectangleFontAttributes = [NSFontAttributeName: font, NSForegroundColorAttributeName: textColor, NSParagraphStyleAttributeName: rectangleStyle] as [String : Any]

let maxDrawableHeight = max(rect.height , font.lineHeight)
let textHeight: CGFloat = KSUtils.getRect(rectangleTextContent as NSString, width: rect.width, height: maxDrawableHeight , font: font).size.height


let textRect = CGRect(x: rect.minX + paddingX, y: rect.minY + (maxDrawableHeight - textHeight) / 2, width: min(maxWidth, rect.width) - (paddingX*2), height: maxDrawableHeight)

rectangleTextContent.draw(in: textRect, withAttributes: rectangleFontAttributes)

#if swift(>=2.3)
context!.saveGState()
context!.clip(to: rect)
context!.restoreGState()
#else
context.saveGState()
context.clip(to: rect)
context.restoreGState()
#endif

// Border
if (borderWidth > 0.0 && borderColor != UIColor.clear) {
borderColor.setStroke()
rectanglePath.lineWidth = borderWidth
rectanglePath.stroke()
if (tokenTextHighlightedColor != nil) {
textColor = tokenTextHighlightedColor!
} else {
textColor = tokenTextColor
}
}
} else {
backgroundColor = tokenBackgroundColor.darkendColor(darkRatio)
}
backgroundColor.setFill()
rectanglePath.fill()

var paddingX: CGFloat = 0.0
var font = UIFont.systemFont(ofSize: 14)
var tokenField: KSTokenField? {
return superview! as? KSTokenField
}
if ((tokenField) != nil) {
paddingX = tokenField!.paddingX()!
font = tokenField!.tokenFont()!
}

// Text
let rectangleTextContent = title
let rectangleStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
rectangleStyle.lineBreakMode = NSLineBreakMode.byTruncatingTail
rectangleStyle.alignment = NSTextAlignment.center
let rectangleFontAttributes = [NSFontAttributeName: font, NSForegroundColorAttributeName: textColor, NSParagraphStyleAttributeName: rectangleStyle] as [String : Any]

let maxDrawableHeight = max(rect.height , font.lineHeight)
let textHeight: CGFloat = KSUtils.getRect(rectangleTextContent as NSString, width: rect.width, height: maxDrawableHeight , font: font).size.height


let textRect = CGRect(x: rect.minX + paddingX, y: rect.minY + (maxDrawableHeight - textHeight) / 2, width: min(maxWidth, rect.width) - (paddingX*2), height: maxDrawableHeight)

rectangleTextContent.draw(in: textRect, withAttributes: rectangleFontAttributes)

#if swift(>=2.3)
context!.saveGState()
context!.clip(to: rect)
context!.restoreGState()
#else
context.saveGState()
context.clip(to: rect)
context.restoreGState()
#endif

// Border
if (borderWidth > 0.0 && borderColor != UIColor.clear) {
borderColor.setStroke()
rectanglePath.lineWidth = borderWidth
rectanglePath.stroke()
}
}
}
Loading