Skip to content

Commit

Permalink
Allow to change placeholder at any time.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartłomiej Semańczyk committed Aug 26, 2016
1 parent 9639aa6 commit a0c61e7
Show file tree
Hide file tree
Showing 47 changed files with 1,780 additions and 153 deletions.
2 changes: 1 addition & 1 deletion BSSelectableView.podspec
Expand Up @@ -9,7 +9,7 @@
Pod::Spec.new do |s|

s.name = 'BSSelectableView'
s.version = '1.0.9'
s.version = '1.1'
s.summary = 'Easily manage your token along with your single or multiply select view.'
s.description = "Looking for simple Swift library to manage multiply or single selection? This one is for you:)"

Expand Down
46 changes: 16 additions & 30 deletions BSSelectableView/Classes/BSMultiSelectableView.swift
Expand Up @@ -6,7 +6,10 @@
// Copyright (c) 2016 Bartłomiej Semańczyk. All rights reserved.
//

@IBDesignable public class BSMultiSelectableView: BSSelectableView, UITableViewDataSource, UITableViewDelegate, BSTokenViewDataSource {
@IBDesignable public class BSMultiSelectableView: BSSelectableView, UITableViewDataSource, UITableViewDelegate {

@IBInspectable public var lineHeight: CGFloat = 30
@IBInspectable public var margin: CGFloat = 0

@IBOutlet public var tokenView: BSTokenView?
@IBOutlet public var scrollTokenView: BSScrollTokenView?
Expand Down Expand Up @@ -43,8 +46,12 @@

tableView.delegate = self
tableView.dataSource = self
tokenView?.dataSource = self
scrollTokenView?.dataSource = self

tokenView?.multiselectableView = self
scrollTokenView?.multiselectableView = self

tokenView?.reloadData()
scrollTokenView?.reloadData()
}

//MARK: - Deinitialization
Expand All @@ -59,6 +66,10 @@
expanded = !expanded
}

func viewForTokenAtIndex(index: Int) -> UIView? {
return delegate?.multiSelectableView?(self, tokenViewForOption: selectedOptions[index], atIndex: index)
}

//MARK: - Private

//MARK: - Overridden
Expand All @@ -75,10 +86,11 @@
let option = options[indexPath.row]

cell.titleLabel.text = option.title
cell.accessoryType = .None
cell.titleLabel.font = BSSelectableView.fontForOption
cell.titleLabel.textColor = BSSelectableView.titleColorForOption
cell.leftPaddingConstraint.constant = CGFloat(BSSelectableView.leftPaddingForOption)

cell.accessoryType = .None
cell.layoutMargins = UIEdgeInsetsZero
cell.selectionStyle = .None

Expand All @@ -98,30 +110,4 @@

delegate?.multiSelectableView?(self, didSelectOption: selectedOption)
}

//MARK: - BSTokenViewDataSource

func lineHeightForToken() -> CGFloat {
return delegate?.lineHeightForTokenInMultiSelectableView?() ?? 30
}

func numberOfTokens() -> Int {
return selectedOptions.count
}

func viewForTokenAtIndex(index: Int) -> UIView? {
return delegate?.multiSelectableView?(self, tokenViewForOption: selectedOptions[index], atIndex: index)
}

func tokenViewDidRefreshWithHeight(height: CGFloat) {
tokenViewHeightConstraint?.constant = height
}

func textForPlaceholder() -> String {
return placeholderText
}

func marginForToken() -> CGFloat {
return delegate?.marginForTokenInMultiSelectableView?() ?? 0
}
}
47 changes: 17 additions & 30 deletions BSSelectableView/Classes/BSScrollTokenView.swift
Expand Up @@ -8,32 +8,15 @@

public class BSScrollTokenView: UIScrollView {

weak var dataSource: BSTokenViewDataSource?
var multiselectableView: BSMultiSelectableView?

private var tokenViews = [UIView]()
private var placeholderLabel = UILabel()

//MARK: - Class Methods

//MARK: - Initialization

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

reloadData()
}

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

reloadData()
}

override public func awakeFromNib() {
super.awakeFromNib()

reloadData()
}

//MARK: - Deinitialization

//MARK: - Actions
Expand All @@ -50,11 +33,11 @@ public class BSScrollTokenView: UIScrollView {

tokenViews.removeAll()

let count = dataSource?.numberOfTokens() ?? 0
let count = multiselectableView?.selectedOptions.count ?? 0

for index in 0..<count {

if let tokenView = dataSource?.viewForTokenAtIndex(index) {
if let tokenView = multiselectableView?.viewForTokenAtIndex(index) {

tokenView.autoresizingMask = UIViewAutoresizing.None
addSubview(tokenView)
Expand All @@ -66,21 +49,25 @@ public class BSScrollTokenView: UIScrollView {

if count == 0 {

let placeholderLabel = UILabel(frame: CGRect(x: CGFloat(BSSelectableView.leftPaddingForPlaceholderText), y: 0, width: frame.size.width, height: dataSource?.lineHeightForToken() ?? 30))
placeholderLabel.text = dataSource?.textForPlaceholder()
placeholderLabel.textColor = BSSelectableView.textColorForPlaceholderText
placeholderLabel.font = BSSelectableView.fontForPlaceholderText

setupPlaceholderLabel()
addSubview(placeholderLabel)
}
}

func setupPlaceholderLabel() {

placeholderLabel.frame = CGRect(x: CGFloat(BSSelectableView.leftPaddingForPlaceholderText), y: 0, width: frame.size.width, height: multiselectableView?.lineHeight ?? 0)
placeholderLabel.text = multiselectableView?.placeholder
placeholderLabel.textColor = BSSelectableView.textColorForPlaceholderText
placeholderLabel.font = BSSelectableView.fontForPlaceholderText
}

//MARK: - Private

private func enumerateItemRectsUsingBlock(block: (CGRect) -> Void) {

var x: CGFloat = 0
let margin = dataSource?.marginForToken() ?? 0
let margin = multiselectableView?.margin ?? 0

for token in tokenViews {

Expand Down Expand Up @@ -111,11 +98,11 @@ public class BSScrollTokenView: UIScrollView {

override public func intrinsicContentSize() -> CGSize {

let lineHeight = dataSource?.lineHeightForToken() ?? 30
let lineHeight = multiselectableView?.lineHeight ?? 0

if tokenViews.isEmpty {

dataSource?.tokenViewDidRefreshWithHeight(lineHeight)
multiselectableView?.tokenViewHeightConstraint?.constant = lineHeight
return CGSizeZero
}

Expand All @@ -125,7 +112,7 @@ public class BSScrollTokenView: UIScrollView {
totalRect = CGRectUnion(itemRect, totalRect)
}

dataSource?.tokenViewDidRefreshWithHeight(max(totalRect.size.height, lineHeight))
multiselectableView?.tokenViewHeightConstraint?.constant = max(totalRect.size.height, lineHeight)
contentSize = CGSizeMake(totalRect.size.width, totalRect.size.height)

return totalRect.size
Expand Down
41 changes: 22 additions & 19 deletions BSSelectableView/Classes/BSSelectableView.swift
Expand Up @@ -23,34 +23,42 @@ public class BSSelectableOption: NSObject {

@objc public protocol BSSelectableViewDelegate {

optional func selectableOptionsForSelectableViewWithIdentifier(identifier: String) -> [BSSelectableOption]
optional func multiSelectableView(view: BSMultiSelectableView, tokenViewForOption option: BSSelectableOption, atIndex index: Int) -> UIView

optional func singleSelectableView(view: BSSingleSelectableView, didSelectOption option: BSSelectableOption)
optional func multiSelectableView(view: BSMultiSelectableView, didSelectOption option: BSSelectableOption)
optional func lineHeightForTokenInMultiSelectableView() -> CGFloat
optional func marginForTokenInMultiSelectableView() -> CGFloat
optional func selectableViewToggledOptionsWithButton(button: UIButton, expanded: Bool)
}

let BSSelectableTableViewCellIdentifier = "SelectableTableViewCellIdentifier"

public class BSSelectableView: UIView {

static public var tintColorForSelectedOption = UIColor.blueColor()
static public var titleColorForSelectedOption = UIColor.greenColor()
static public var titleColorForOption = UIColor.blackColor()
static public var fontForOption = UIFont.systemFontOfSize(16)
static public var fontForPlaceholderText = UIFont.systemFontOfSize(14)

static public var leftPaddingForPlaceholderText = 0
static public var leftPaddingForOption = 20
static public var heightForOption = 40
static public var leftPaddingForPlaceholderText = 0
static public var fontForPlaceholderText = UIFont.systemFontOfSize(14)

static public var tintColorForSelectedOption = UIColor.blueColor()
static public var titleColorForSelectedOption = UIColor.greenColor()
static public var titleColorForOption = UIColor.blackColor()
static public var textColorForPlaceholderText = UIColor.grayColor()

@IBInspectable public var identifier: String = ""
@IBInspectable public var tableViewAccessibilityIdentifier: String = ""
@IBInspectable public var maxNumberOfRows: Int = 6
@IBInspectable public var placeholderText: String = ""
@IBInspectable public var placeholder: String = "" {

didSet {

(self as? BSMultiSelectableView)?.tokenView?.setupPlaceholderLabel()
(self as? BSMultiSelectableView)?.scrollTokenView?.setupPlaceholderLabel()
(self as? BSSingleSelectableView)?.setupLabel()
}
}

@IBInspectable public var cornerRadius: CGFloat = 3 {

didSet {
Expand All @@ -64,16 +72,7 @@ public class BSSelectableView: UIView {
@IBOutlet public var contentOptionsHeightConstraint: NSLayoutConstraint?
@IBOutlet public var contentOptionsView: UIView?

weak public var delegate: BSSelectableViewDelegate? {

didSet {

options = delegate?.selectableOptionsForSelectableViewWithIdentifier?(identifier) ?? options
(self as? BSMultiSelectableView)?.tokenView?.reloadData()
(self as? BSMultiSelectableView)?.scrollTokenView?.reloadData()
(self as? BSSingleSelectableView)?.setupLabel()
}
}
weak public var delegate: BSSelectableViewDelegate?

public var options = [BSSelectableOption]() {

Expand Down Expand Up @@ -127,6 +126,10 @@ public class BSSelectableView: UIView {
contentOptionsView.addConstraints([topConstraint, trailingConstraint, bottomConstraint, leadingConstraint])
contentOptionsView.layoutIfNeeded()
}

(self as? BSMultiSelectableView)?.tokenView?.reloadData()
(self as? BSMultiSelectableView)?.scrollTokenView?.reloadData()
(self as? BSSingleSelectableView)?.setupLabel()
}

//MARK: - Deinitialization
Expand Down
10 changes: 5 additions & 5 deletions BSSelectableView/Classes/BSSingleSelectableView.swift
Expand Up @@ -28,7 +28,6 @@

tableView.delegate = self
tableView.dataSource = self
options = delegate?.selectableOptionsForSelectableViewWithIdentifier?(identifier) ?? options
}

//MARK: - Deinitialization
Expand All @@ -52,15 +51,15 @@

if selectedOption == nil {

selectedOptionLabel?.text = placeholderText
selectedOptionLabel?.text = placeholder
selectedOptionLabel?.font = BSSelectableView.fontForPlaceholderText
selectedOptionLabel?.textColor = BSSelectableView.textColorForPlaceholderText

} else {

selectedOptionLabel?.text = selectedOption?.title
selectedOptionLabel?.textColor = BSSelectableView.titleColorForOption
selectedOptionLabel?.font = BSSelectableView.fontForOption
selectedOptionLabel?.textColor = BSSelectableView.titleColorForOption
}
}

Expand All @@ -80,11 +79,12 @@
let option = options[indexPath.row]

cell.titleLabel.text = option.title
cell.accessoryType = option.identifier == selectedOption?.identifier ? .Checkmark : .None
cell.tintColor = BSSelectableView.tintColorForSelectedOption
cell.titleLabel.font = BSSelectableView.fontForOption
cell.titleLabel.textColor = option.identifier == selectedOption?.identifier ? BSSelectableView.titleColorForSelectedOption : BSSelectableView.titleColorForOption
cell.leftPaddingConstraint.constant = CGFloat(BSSelectableView.leftPaddingForOption)

cell.accessoryType = option.identifier == selectedOption?.identifier ? .Checkmark : .None
cell.tintColor = BSSelectableView.tintColorForSelectedOption
cell.layoutMargins = UIEdgeInsetsZero
cell.selectionStyle = .None

Expand Down

0 comments on commit a0c61e7

Please sign in to comment.