Skip to content

Commit

Permalink
Renamed some properties for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardopereira committed Apr 9, 2018
1 parent 9812920 commit 0af94be
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Example/WSTagsFieldExample/TableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class TagsViewCell: UITableViewCell {
tagsField.addTag("alksjlkasd4")
tagsField.addTag("alksjlkasdsds5")

tagsField.placeholderAlwayVisible = true
tagsField.placeholderAlwaysVisible = true
tagsField.backgroundColor = .lightGray

tagsField.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
Expand Down
14 changes: 7 additions & 7 deletions Example/WSTagsFieldExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class ViewController: UIViewController {
//tagsField.translatesAutoresizingMaskIntoConstraints = false
//tagsField.heightAnchor.constraint(equalToConstant: 150).isActive = true

tagsField.tagCornerRadius = 3.0
tagsField.lineSpace = 10
tagsField.cornerRadius = 3.0
tagsField.spaceBetweenLines = 10
tagsField.spaceBetweenTags = 10

//tagsField.numberOfLines = 3
Expand All @@ -35,7 +35,7 @@ class ViewController: UIViewController {

tagsField.placeholder = "Enter a tag"
tagsField.placeholderColor = .red
tagsField.placeholderAlwayVisible = true
tagsField.placeholderAlwaysVisible = true
tagsField.backgroundColor = .lightGray
tagsField.frame = tagsView.bounds
tagsField.returnKeyType = .next
Expand All @@ -62,20 +62,20 @@ class ViewController: UIViewController {
@IBAction func touchChangeAppearance(_ sender: UIButton) {
tagsField.layoutMargins = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
tagsField.contentInset = UIEdgeInsets(top: 2, left: 2, bottom: 2, right: 2) //old padding
tagsField.tagCornerRadius = 10.0
tagsField.lineSpace = 2
tagsField.cornerRadius = 10.0
tagsField.spaceBetweenLines = 2
tagsField.spaceBetweenTags = 2
tagsField.tintColor = .red
tagsField.textColor = .blue
tagsField.selectedColor = .yellow
tagsField.selectedTextColor = .black
tagsField.delimiter = ","
tagsField.displayDelimiter = true
tagsField.isDelimiterVisible = true
tagsField.borderWidth = 2
tagsField.borderColor = .blue
tagsField.fieldTextColor = .green
tagsField.placeholderColor = .green
tagsField.placeholderAlwayVisible = false
tagsField.placeholderAlwaysVisible = false
}

@IBAction func touchAddRandomTags(_ sender: UIButton) {
Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ An iOS text field that represents different Tags.
## Usage

``` swift

let tagsField = WSTagsField()
tagsField.backgroundColor = .white
tagsField.padding = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
tagsField.layoutMargins = UIEdgeInsets(top: 2, left: 6, bottom: 2, right: 6)
tagsField.contentInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
tagsField.spaceBetweenLines = 5.0
tagsField.spaceBetweenTags = 10.0
tagsField.font = .systemFont(ofSize: 12.0)
tagsField.backgroundColor = .white
tagsField.tintColor = .green
tagsField.textColor = .black
tagsField.fieldTextColor = .blue
tagsField.selectedColor = .black
tagsField.selectedTextColor = .red
tagsField.delimiter = ","
tagsField.isDelimiterVisible = true
tagsField.placeholderColor = .green
tagsField.placeholderAlwaysVisible = true
tagsField.returnKeyType = .next

// Events
tagsField.onDidAddTag = { (_,_) in
Expand Down Expand Up @@ -109,7 +114,7 @@ let package = Package(
)
```

(**Note** that the Swift Package Manager is still in early design and development, for more infomation checkout its repository)
(**Note** that the Swift Package Manager is still in early design and development, for more information checkout its repository)

#### Manually

Expand All @@ -118,7 +123,7 @@ Download all the source files and drop them into your project.
## Requirements

* iOS 8.0+
* Xcode 8 (Swift 3.0)
* Xcode 9 (Swift 4.0)

# Contributing

Expand Down
69 changes: 42 additions & 27 deletions Source/WSTagsField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import UIKit
open class WSTagsField: UIScrollView {
fileprivate let textField = BackspaceDetectingTextField()

/// Max number of lines of tags can display in WSTagsField before its contents become scrollable. Default value is 0, which means WSTagsField always resize to fit all tags.
open var numberOfLines: Int = 0 {
didSet { repositionViews() }
}

open override var isFirstResponder: Bool {
guard super.isFirstResponder == false,
textField.isFirstResponder == false else { return true }
Expand All @@ -27,25 +22,28 @@ open class WSTagsField: UIScrollView {
return false
}

/// Background color for tag view in normal(non-selected) state.
/// Background color for tag view in normal (non-selected) state.
open override var tintColor: UIColor! {
didSet {
tagViews.forEach { $0.tintColor = self.tintColor }
}
}

/// Text color for tag view in normal (non-selected) state.
open var textColor: UIColor? {
didSet {
tagViews.forEach { $0.textColor = self.textColor }
}
}

/// Background color for tag view in normal (selected) state.
open var selectedColor: UIColor? {
didSet {
tagViews.forEach { $0.selectedColor = self.selectedColor }
}
}

/// Text color for tag view in normal (selected) state.
open var selectedTextColor: UIColor? {
didSet {
tagViews.forEach { $0.selectedTextColor = self.selectedTextColor }
Expand All @@ -54,25 +52,38 @@ open class WSTagsField: UIScrollView {

open var delimiter: String = "" {
didSet {
tagViews.forEach { $0.displayDelimiter = self.displayDelimiter ? self.delimiter : "" }
tagViews.forEach { $0.displayDelimiter = self.isDelimiterVisible ? self.delimiter : "" }
}
}

open var displayDelimiter: Bool = false {
@available(*, unavailable, message: "Use 'isDelimiterVisible' instead.")
open var displayDelimiter: Bool = false

open var isDelimiterVisible: Bool = false {
didSet {
tagViews.forEach { $0.displayDelimiter = self.displayDelimiter ? self.delimiter : "" }
tagViews.forEach { $0.displayDelimiter = self.isDelimiterVisible ? self.delimiter : "" }
}
}

open var maxHeight: CGFloat = CGFloat.infinity {
didSet {
tagViews.forEach { $0.displayDelimiter = self.displayDelimiter ? self.delimiter : "" }
tagViews.forEach { $0.displayDelimiter = self.isDelimiterVisible ? self.delimiter : "" }
}
}

/// Max number of lines of tags can display in WSTagsField before its contents become scrollable. Default value is 0, which means WSTagsField always resize to fit all tags.
open var numberOfLines: Int = 0 {
didSet {
repositionViews()
}
}

open var tagCornerRadius: CGFloat = 3.0 {
@available(*, unavailable, message: "Use 'cornerRadius' instead.")
open var tagCornerRadius: CGFloat = 3.0

open var cornerRadius: CGFloat = 3.0 {
didSet {
tagViews.forEach { $0.cornerRadius = self.tagCornerRadius }
tagViews.forEach { $0.cornerRadius = self.cornerRadius }
}
}

Expand Down Expand Up @@ -112,7 +123,10 @@ open class WSTagsField: UIScrollView {
}
}

open var placeholderAlwayVisible: Bool = false {
@available(*, unavailable, message: "Use 'placeholderAlwaysVisible' instead.")
open var placeholderAlwayVisible: Bool = false

open var placeholderAlwaysVisible: Bool = false {
didSet {
updatePlaceholderTextVisibility()
}
Expand All @@ -134,11 +148,7 @@ open class WSTagsField: UIScrollView {
}

@available(*, unavailable, message: "Use 'contentInset' instead.")
open var padding: UIEdgeInsets = UIEdgeInsets(top: 10.0, left: 8.0, bottom: 10.0, right: 8.0) {
didSet {
repositionViews()
}
}
open var padding: UIEdgeInsets = UIEdgeInsets.zero

open override var contentInset: UIEdgeInsets {
didSet {
Expand All @@ -152,7 +162,7 @@ open class WSTagsField: UIScrollView {
}
}

open var lineSpace: CGFloat = 2.0 {
open var spaceBetweenLines: CGFloat = 2.0 {
didSet {
repositionViews()
}
Expand Down Expand Up @@ -204,7 +214,7 @@ open class WSTagsField: UIScrollView {

fileprivate var oldIntrinsicContentHeight: CGFloat = 0

open var estimatedInitialMaxLayoutWidth: CGFloat {
fileprivate var estimatedInitialMaxLayoutWidth: CGFloat {
// Workaround: https://stackoverflow.com/questions/42342402/how-can-i-create-a-view-has-intrinsiccontentsize-just-like-uilabel
// "So how the system knows the label's width so that it can calculate the height before layoutSubviews"
// Re: "It calculates it. It asks “around” first by checking the last constraint (if there is one) for width. It asks it subviews (your custom class) for its constrains and then makes the calculations."
Expand Down Expand Up @@ -309,8 +319,8 @@ open class WSTagsField: UIScrollView {
tagView.textColor = self.textColor
tagView.selectedColor = self.selectedColor
tagView.selectedTextColor = self.selectedTextColor
tagView.displayDelimiter = self.displayDelimiter ? self.delimiter : ""
tagView.cornerRadius = self.tagCornerRadius
tagView.displayDelimiter = self.isDelimiterVisible ? self.delimiter : ""
tagView.cornerRadius = self.cornerRadius
tagView.borderWidth = self.borderWidth
tagView.borderColor = self.borderColor
tagView.layoutMargins = self.layoutMargins
Expand Down Expand Up @@ -463,7 +473,9 @@ open class WSTagsField: UIScrollView {
}

// MARK: TextField Properties

extension WSTagsField {

public var keyboardType: UIKeyboardType {
get { return textField.keyboardType }
set { textField.keyboardType = newValue }
Expand Down Expand Up @@ -500,12 +512,15 @@ extension WSTagsField {
}

@available(iOS, unavailable)
override open var inputAccessoryView: UIView? { return super.inputAccessoryView }
override open var inputAccessoryView: UIView? {
return super.inputAccessoryView
}

open var inputFieldAccessoryView: UIView? {
get { return textField.inputAccessoryView }
set { textField.inputAccessoryView = newValue }
}

}

// MARK: Private functions
Expand Down Expand Up @@ -583,7 +598,7 @@ extension WSTagsField {
if curX + tagRect.width > maxWidth {
// Need a new line
curX = 0
curY += Constants.STANDARD_ROW_HEIGHT + lineSpace
curY += Constants.STANDARD_ROW_HEIGHT + spaceBetweenLines
totalHeight += Constants.STANDARD_ROW_HEIGHT
}

Expand All @@ -609,7 +624,7 @@ extension WSTagsField {
// isOnFirstLine will be useful, and this calculation is important.
// So leaving it set here, and marking the warning to ignore it
curX = 0 + Constants.TEXT_FIELD_HSPACE
curY += Constants.STANDARD_ROW_HEIGHT + lineSpace
curY += Constants.STANDARD_ROW_HEIGHT + spaceBetweenLines
totalHeight += Constants.STANDARD_ROW_HEIGHT
// Adjust the width
availableWidthForTextField = maxWidth - curX
Expand Down Expand Up @@ -667,7 +682,7 @@ extension WSTagsField {
}

fileprivate func updatePlaceholderTextVisibility() {
textField.attributedPlaceholder = (placeholderAlwayVisible || tags.count == 0) ? attributedPlaceholder() : nil
textField.attributedPlaceholder = (placeholderAlwaysVisible || tags.count == 0) ? attributedPlaceholder() : nil
}

private func attributedPlaceholder() -> NSAttributedString {
Expand All @@ -682,7 +697,7 @@ extension WSTagsField {
guard self.numberOfLines > 0 else {
return CGFloat.infinity
}
return contentInset.top + contentInset.bottom + Constants.STANDARD_ROW_HEIGHT * CGFloat(numberOfLines) + lineSpace * CGFloat(numberOfLines - 1)
return contentInset.top + contentInset.bottom + Constants.STANDARD_ROW_HEIGHT * CGFloat(numberOfLines) + spaceBetweenLines * CGFloat(numberOfLines - 1)
}

}
Expand Down

0 comments on commit 0af94be

Please sign in to comment.