Skip to content

Commit

Permalink
fix last toolbar item shrink issue
Browse files Browse the repository at this point in the history
- cleanup code
- remove bounce to remove background toolbar
  • Loading branch information
cbess committed Oct 31, 2020
1 parent 9502828 commit 4cc1e0d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 39 deletions.
4 changes: 2 additions & 2 deletions RichEditorView.podspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Pod::Spec.new do |s|
s.name = "RichEditorView"
s.version = "4.2"
s.version = "4.2.1"
s.summary = "Rich Text Editor for iOS written in Swift"
s.homepage = "https://github.com/cjwirth/RichEditorView"
s.license = 'BSD 3-clause'
s.author = { "Caesar Wirth" => "[email protected]" }
s.authors = { "C. Bess" => "[email protected]", "Caesar Wirth" => "[email protected]" }
s.source = { :git => "https://github.com/cbess/RichEditorView.git", :tag => s.version.to_s }

s.platform = :ios, '12.0'
Expand Down
62 changes: 25 additions & 37 deletions RichEditorView/Classes/RichEditorToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,18 @@ import UIKit
open var actionHandler: (() -> Void)?

public convenience init(image: UIImage? = nil, handler: (() -> Void)? = nil) {
self.init(image: image, style: .plain, target: nil, action: nil)
self.init(image: image, style: .plain, target: nil, action: #selector(buttonTapped))
target = self
action = #selector(RichBarButtonItem.buttonWasTapped)
actionHandler = handler
}

public convenience init(title: String = "", handler: (() -> Void)? = nil) {
self.init(title: title, style: .plain, target: nil, action: nil)
self.init(title: title, style: .plain, target: nil, action: #selector(buttonTapped))
target = self
action = #selector(RichBarButtonItem.buttonWasTapped)
actionHandler = handler
}

@objc func buttonWasTapped() {
@objc func buttonTapped() {
actionHandler?()
}
}
Expand All @@ -65,92 +63,82 @@ import UIKit

/// The tint color to apply to the toolbar background.
open var barTintColor: UIColor? {
get { return backgroundToolbar.barTintColor }
set { backgroundToolbar.barTintColor = newValue }
get { return toolbar.barTintColor }
set { toolbar.barTintColor = newValue }
}

private var toolbarScroll: UIScrollView
private var toolbar: UIToolbar
private var backgroundToolbar: UIToolbar

public override init(frame: CGRect) {
toolbarScroll = UIScrollView()
toolbar = UIToolbar()
backgroundToolbar = UIToolbar()
super.init(frame: frame)
setup()
}

public required init?(coder aDecoder: NSCoder) {
toolbarScroll = UIScrollView()
toolbar = UIToolbar()
backgroundToolbar = UIToolbar()
super.init(coder: aDecoder)
setup()
}

private func setup() {
autoresizingMask = .flexibleWidth
backgroundColor = .clear

backgroundToolbar.frame = bounds
backgroundToolbar.autoresizingMask = [.flexibleHeight, .flexibleWidth]

toolbar.autoresizingMask = .flexibleWidth
toolbar.backgroundColor = .clear
toolbar.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)

toolbarScroll.frame = bounds
toolbarScroll.bounces = false
toolbarScroll.autoresizingMask = [.flexibleHeight, .flexibleWidth]
toolbarScroll.showsHorizontalScrollIndicator = false
toolbarScroll.showsVerticalScrollIndicator = false
toolbarScroll.backgroundColor = .clear

toolbarScroll.addSubview(toolbar)

addSubview(backgroundToolbar)
addSubview(toolbarScroll)
updateToolbar()
}

private func updateToolbar() {
var buttons = [UIBarButtonItem]()

// build collection of richbar buttons
for option in options {
let handler = { [weak self] in
if let strongSelf = self {
option.action(strongSelf)
}
}

var button: RichBarButtonItem!
if let image = option.image {
let button = RichBarButtonItem(image: image, handler: handler)
buttons.append(button)
button = RichBarButtonItem(image: image, handler: handler)
} else {
let title = option.title
let button = RichBarButtonItem(title: title, handler: handler)
buttons.append(button)
button = RichBarButtonItem(title: option.title, handler: handler)
}

buttons.append(button)
}
toolbar.items = buttons

// calculate new toolbar width
let defaultIconWidth: CGFloat = 28
let barButtonItemMargin: CGFloat = 12
let width: CGFloat = buttons.reduce(0) {sofar, new in
if let view = new.value(forKey: "view") as? UIView {
return sofar + view.frame.size.width + barButtonItemMargin
} else {
return sofar + (defaultIconWidth + barButtonItemMargin)
let width: CGFloat = buttons.reduce(0) { result, button in
var width = defaultIconWidth

if let view = button.customView {
width = view.bounds.width
}

return result + width + barButtonItemMargin
}

if width < frame.size.width {
toolbar.frame.size.width = frame.size.width + barButtonItemMargin
} else {
toolbar.frame.size.width = width + barButtonItemMargin
}
toolbar.frame.size.height = 44
toolbarScroll.contentSize.width = width
toolbar.items = buttons
toolbar.frame.size.width = (width < frame.size.width ? frame.size.width : width + defaultIconWidth)
toolbar.frame.size.height = bounds.height
toolbarScroll.contentSize.width = width + barButtonItemMargin
}

}

0 comments on commit 4cc1e0d

Please sign in to comment.