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

Added support for optional NSAttributedString, for cell text. #161

Open
wants to merge 1 commit 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
42 changes: 42 additions & 0 deletions Source/BTItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// BTItem.swift
//
// Copyright (c) 2017 PHAM BA THO ([email protected]). All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation

/**

An struct to define both a String and optional NSAttributedString, for each item

- parameters:
- string: Define item String.
- attributeString: Define optional item NSAttributedString, used in UITableViewCell only.
*/
public struct BTItem {
let string: String
let attributedString: NSAttributedString?

public init(string: String, attributedString: NSAttributedString? = nil) {
self.string = string
self.attributedString = attributedString
}
}
14 changes: 7 additions & 7 deletions Source/BTNavigationDropdownMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ open class BTNavigationDropdownMenu: UIView {
fileprivate var menuArrow: UIImageView!
fileprivate var backgroundView: UIView!
fileprivate var tableView: BTTableView!
fileprivate var items: [String]!
fileprivate var items: [BTItem]!
fileprivate var menuWrapper: UIView!

required public init?(coder aDecoder: NSCoder) {
Expand All @@ -248,7 +248,7 @@ open class BTNavigationDropdownMenu: UIView {
- title: A string to define title to be displayed.
- items: The array of items to select
*/
public convenience init(navigationController: UINavigationController? = nil, containerView: UIView = UIApplication.shared.keyWindow!, title: String, items: [String]) {
public convenience init(navigationController: UINavigationController? = nil, containerView: UIView = UIApplication.shared.keyWindow!, title: String, items: [BTItem]) {

self.init(navigationController: navigationController, containerView: containerView, title: BTTitle.title(title), items: items)
}
Expand All @@ -265,7 +265,7 @@ open class BTNavigationDropdownMenu: UIView {
- title: An enum to define title to be displayed, can be a string or index of items.
- items: The array of items to select
*/
public init(navigationController: UINavigationController? = nil, containerView: UIView = UIApplication.shared.keyWindow!, title: BTTitle, items: [String]) {
public init(navigationController: UINavigationController? = nil, containerView: UIView = UIApplication.shared.keyWindow!, title: BTTitle, items: [BTItem]) {
// Key window
guard let window = UIApplication.shared.keyWindow else {
super.init(frame: CGRect.zero)
Expand All @@ -286,7 +286,7 @@ open class BTNavigationDropdownMenu: UIView {
switch title{
case .index(let index):
if index < items.count{
titleToDisplay = items[index]
titleToDisplay = items[index].string
} else {
titleToDisplay = ""
}
Expand Down Expand Up @@ -349,7 +349,7 @@ open class BTNavigationDropdownMenu: UIView {
}
selfie.didSelectItemAtIndexHandler!(indexPath)
if selfie.shouldChangeTitleText! {
selfie.setMenuTitle("\(selfie.tableView.items[indexPath])")
selfie.setMenuTitle("\(selfie.tableView.items[indexPath].string)")
}
self?.hideMenu()
self?.layoutSubviews()
Expand Down Expand Up @@ -406,7 +406,7 @@ open class BTNavigationDropdownMenu: UIView {
}
}

open func updateItems(_ items: [String]) {
open func updateItems(_ items: [BTItem]) {
if !items.isEmpty {
self.tableView.items = items
self.tableView.reloadData()
Expand All @@ -418,7 +418,7 @@ open class BTNavigationDropdownMenu: UIView {
self.tableView.reloadData()

if self.shouldChangeTitleText! {
self.setMenuTitle("\(self.tableView.items[index])")
self.setMenuTitle("\(self.tableView.items[index].string)")
}
}

Expand Down
12 changes: 8 additions & 4 deletions Source/Internal/BTTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ class BTTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
var selectRowAtIndexPathHandler: ((_ indexPath: Int) -> ())?

// Private properties
var items: [String] = []
var items: [BTItem] = []
var selectedIndexPath: Int?

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

init(frame: CGRect, items: [String], title: String, configuration: BTConfiguration) {
init(frame: CGRect, items: [BTItem], title: String, configuration: BTConfiguration) {
super.init(frame: frame, style: UITableView.Style.plain)

self.items = items
self.selectedIndexPath = items.index(of: title)
self.selectedIndexPath = items.firstIndex(where: { $0.string == title })
self.configuration = configuration

// Setup table view
Expand Down Expand Up @@ -76,7 +76,11 @@ class BTTableView: UITableView, UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = BTTableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "Cell", configuration: self.configuration)
cell.textLabel?.text = self.items[(indexPath as NSIndexPath).row]
if let attributedString = self.items[(indexPath as NSIndexPath).row].attributedString {
cell.textLabel?.attributedText = attributedString
} else {
cell.textLabel?.text = self.items[(indexPath as NSIndexPath).row].string
}
cell.checkmarkIcon.isHidden = ((indexPath as NSIndexPath).row == selectedIndexPath) ? false : true
return cell
}
Expand Down