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

Fix NavigationBar title too long #159

Open
wants to merge 2 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
8 changes: 7 additions & 1 deletion Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ class ViewController: UIViewController {

// "Old" version
// menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, containerView: self.navigationController!.view, title: "Dropdown Menu", items: items)



menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, containerView: self.navigationController!.view, title: BTTitle.index(2), items: items)

// Another way to initialize:
// menuView = BTNavigationDropdownMenu(navigationController: self.navigationController, containerView: self.navigationController!.view, title: BTTitle.title("Dropdown Menu"), items: items)


let maxString = String(repeating: "一", count: 11)
let maxTitleSize = (maxString as NSString).size(withAttributes: [NSAttributedString.Key.font:menuView.navigationBarTitleFont])
menuView.menuTitleMaxWidth = maxTitleSize.width
menuView.menuTitleBreakModel = .byTruncatingMiddle
menuView.cellHeight = 50
menuView.cellBackgroundColor = self.navigationController?.navigationBar.barTintColor
menuView.cellSelectionColor = UIColor(red: 0.0/255.0, green:160.0/255.0, blue:195.0/255.0, alpha: 1.0)
Expand Down
31 changes: 30 additions & 1 deletion Source/BTNavigationDropdownMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ open class BTNavigationDropdownMenu: UIView {
self.configuration.menuTitleColor = value
}
}

open var menuTitleMaxWidth: CGFloat? {
get {
return self.configuration.menuTitleMaxWidth
}
set(value) {
self.configuration.menuTitleMaxWidth = value
}
}

open var menuTitleBreakModel: NSLineBreakMode {
get {
return self.configuration.menuTitleBreakModel
}
set(value) {
self.configuration.menuTitleBreakModel = value
}
}


// The height of the cell. Default is 50
open var cellHeight: NSNumber {
Expand Down Expand Up @@ -314,6 +333,7 @@ open class BTNavigationDropdownMenu: UIView {
self.menuTitle.textColor = self.menuTitleColor
self.menuTitle.font = self.configuration.navigationBarTitleFont
self.menuTitle.textAlignment = self.configuration.cellTextLabelAlignment
self.menuTitle.lineBreakMode = self.configuration.menuTitleBreakModel
self.menuButton.addSubview(self.menuTitle)

self.menuArrow = UIImageView(image: self.configuration.arrowImage.withRenderingMode(.alwaysTemplate))
Expand Down Expand Up @@ -377,7 +397,16 @@ open class BTNavigationDropdownMenu: UIView {
}

override open func layoutSubviews() {
self.menuTitle.sizeToFit()
let fitsSize = self.menuTitle.sizeThatFits(CGSize.zero)
var menuWidth = fitsSize.width
if let menuTitleMaxWidth = self.menuTitleMaxWidth {
menuWidth = CGFloat.minimum(fitsSize.width, menuTitleMaxWidth)
let menuSize = CGSize(width: menuWidth, height: fitsSize.height)
self.menuTitle.frame.size.height = menuSize.height
self.menuTitle.frame.size.width = menuSize.width
} else {
self.menuTitle.sizeToFit()
}
self.menuTitle.center = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
self.menuTitle.textColor = self.configuration.menuTitleColor
self.menuArrow.sizeToFit()
Expand Down
3 changes: 3 additions & 0 deletions Source/Internal/BTConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import UIKit

final class BTConfiguration {
var menuTitleColor: UIColor?
var menuTitleMaxWidth: CGFloat?
var menuTitleBreakModel : NSLineBreakMode!
var cellHeight: CGFloat!
var cellBackgroundColor: UIColor?
var cellSeparatorColor: UIColor?
Expand Down Expand Up @@ -58,6 +60,7 @@ final class BTConfiguration {

// Default values
self.menuTitleColor = UIColor.darkGray
self.menuTitleBreakModel = .byTruncatingMiddle
self.cellHeight = 50
self.cellBackgroundColor = UIColor.white
self.arrowTintColor = UIColor.white
Expand Down