diff --git a/Demo/Demo/ViewController.swift b/Demo/Demo/ViewController.swift index d5c66704..591a5506 100755 --- a/Demo/Demo/ViewController.swift +++ b/Demo/Demo/ViewController.swift @@ -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) diff --git a/Source/BTNavigationDropdownMenu.swift b/Source/BTNavigationDropdownMenu.swift index 3e098428..df384691 100755 --- a/Source/BTNavigationDropdownMenu.swift +++ b/Source/BTNavigationDropdownMenu.swift @@ -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 { @@ -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)) @@ -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() diff --git a/Source/Internal/BTConfiguration.swift b/Source/Internal/BTConfiguration.swift index f25c5eb8..96c748c3 100644 --- a/Source/Internal/BTConfiguration.swift +++ b/Source/Internal/BTConfiguration.swift @@ -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? @@ -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