Skip to content
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
7 changes: 4 additions & 3 deletions BTNavigationDropdownMenu.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "BTNavigationDropdownMenu"
s.version = "0.4.1"
s.version = "1.0.2"
s.summary = "The elegent navigation dropdown menu"
s.description = <<-DESC
The elegant **dropdown menu**, written in **Swift**, appears underneath **navigation bar** to display a list of related items when a user click on the navigation title.
Expand All @@ -10,9 +10,10 @@ Pod::Spec.new do |s|
s.license = "MIT"
s.author = { "Pham Ba Tho" => "phambatho@gmail.com" }
s.social_media_url = "https://www.facebook.com/phambatho"
s.platform = :ios, '8.0'
s.platform = :ios, '9.3'
s.source = { :git => "https://github.com/PhamBaTho/BTNavigationDropdownMenu.git", :tag => s.version.to_s }
s.source_files = "Source/*.swift"
s.resources = "Source/*.bundle"
s.requires_arc = true
s.requires_arc = true
s.dependency 'DYBadge'
end
19 changes: 19 additions & 0 deletions Demo/BTNavigationDropdownMenu/BTNavigationDropdownMenu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// BTNavigationDropdownMenu.h
// BTNavigationDropdownMenu
//
// Created by Dominik Butz on 10.04.18.
// Copyright © 2018 PHAM BA THO. All rights reserved.
//

#import <UIKit/UIKit.h>

//! Project version number for BTNavigationDropdownMenu.
FOUNDATION_EXPORT double BTNavigationDropdownMenuVersionNumber;

//! Project version string for BTNavigationDropdownMenu.
FOUNDATION_EXPORT const unsigned char BTNavigationDropdownMenuVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <BTNavigationDropdownMenu/PublicHeader.h>


24 changes: 24 additions & 0 deletions Demo/BTNavigationDropdownMenu/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
1 change: 1 addition & 0 deletions Demo/Cartfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github "DominikButz/DYBadge" ~> 2.0
1 change: 1 addition & 0 deletions Demo/Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github "DominikButz/DYBadge" "2.0.1"
41 changes: 41 additions & 0 deletions Demo/Carthage/Checkouts/DYBadge/DYBadge.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Be sure to run `pod lib lint DYBadgeButton.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
s.name = 'DYBadge'
s.version = '2.0.1'
s.summary = 'Pop up badge, written in Swift. Add as subview to any UIView object.'
s.swift_version = '4.0'

# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!

s.description = <<-DESC
Pop up badge which can be added to any UIView or UIView subclass (written in Swift 4.0).
DESC

s.homepage = 'https://github.com/DominikButz/DYBadge'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'dominikbutz' => 'dominikbutz@gmail.com' }
s.source = { :git => 'https://github.com/DominikButz/DYBadge.git', :tag => s.version.to_s }


s.ios.deployment_target = '9.3'

s.source_files = 'DYBadgeExample/DYBadge/**/*'

# s.resource_bundles = {
# 'DYBadge' => ['DYBadge/Assets/*.png']
# }

s.public_header_files = 'DYBadge/**/*.h'

end
19 changes: 19 additions & 0 deletions Demo/Carthage/Checkouts/DYBadge/DYBadge/DYBadge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// DYBadge.h
// DYBadge
//
// Created by Dominik Butz on 09.04.18.
// Copyright © 2018 Duoyun. All rights reserved.
//

#import <UIKit/UIKit.h>

//! Project version number for DYBadge.
FOUNDATION_EXPORT double DYBadgeVersionNumber;

//! Project version string for DYBadge.
FOUNDATION_EXPORT const unsigned char DYBadgeVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <DYBadge/PublicHeader.h>


262 changes: 262 additions & 0 deletions Demo/Carthage/Checkouts/DYBadge/DYBadge/DYBadge.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
//
// DYBadge.swift
// Flat Chat
//
// Created by Dominik Butz on 08.04.18.
// Copyright © 2018 Duoyun. All rights reserved.
//

import UIKit

@IBDesignable public class DYBadge: UILabel {

/// The text of the badge
@IBInspectable open var badgeString:String? {
didSet{

let wasNullishBefore = self.isNullish(self.text)

self.text = badgeString

self.setBadgeSizeAndFrame(animated: !wasNullishBefore)

var transform: CGAffineTransform?
var shouldHide: Bool?

if self.isHidden && self.isNullish(badgeString) == false {
// is hidden but should appear
self.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
transform = CGAffineTransform.identity
shouldHide = false

}

else if self.isHidden == false && self.isNullish(badgeString) {
// should disappear
transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
shouldHide = true

} else {

return
}

self.animateBadgeTransform(shouldHide: shouldHide!,transform: transform!)

}
}


/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
public override func awakeFromNib() {
super.awakeFromNib()

self.setupBadge()
}


public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupBadge()
}

public override init(frame: CGRect) {
super.init(frame: frame)
self.setupBadge()
}

private func setupBadge() {

self.textAlignment = .center

self.setBadgeSizeAndFrame(animated: false)

self.isHidden = self.isNullish(badgeString)


}

private func setBadgeSizeAndFrame(animated: Bool) {

if self.isNullish(badgeString) {
return
}

let sizeLabel = UILabel()
sizeLabel.text = self.badgeString!
sizeLabel.font = self.font
sizeLabel.sizeToFit()
let badgeSize = sizeLabel.frame.size

self.transform = CGAffineTransform.identity

let frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: badgeSize.width + 8.0, height: badgeSize.height + 2.0)

if animated {
UIView.animate(withDuration: 0.1) {
self.set(frame: frame)
}
} else {

self.set(frame: frame)

}

self.clipsToBounds = true

}

private func set(frame: CGRect) {

self.frame = frame
self.layer.cornerRadius = self.bounds.height / 2.0

}


private func animateBadgeTransform(shouldHide: Bool, transform: CGAffineTransform) {

let springValue:CGFloat = shouldHide ? 0 : 0.4

if shouldHide == false {

self.isHidden = false

}

UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: springValue, initialSpringVelocity: 0, options: [], animations: {
self.transform = transform

}) { (completed) in

if shouldHide {
self.isHidden = true
}
}
}



/// Checks if the badge string is "nullish" (nil, empty string or Int value == 0). overridable
///
/// - Parameters:
/// - stringValue: a String
open func isNullish(_ stringValue: String?)-> Bool {

if stringValue == nil {
return true
}

let stringWithoutWhitespaces = stringValue!.components(separatedBy: .whitespaces).joined()

if stringWithoutWhitespaces == "" {
return true
}

let intValue = Int(stringWithoutWhitespaces)

if intValue == 0 {
return true
}

return false

}

/// Finds the first occurence of a DYBadge if it exists among the superview's subviews
///
/// - Parameters:
/// - superview: the superview that hopefully has a DYBadge as one of its subviews
/// - Returns: a DYBadge object
class func findBadge(in superview: UIView)->DYBadge? {

let subviews = superview.subviews

for view in subviews {
if let badge = view as? DYBadge {
return badge
}
}
return nil
}

}


public extension UIView {

/// Finds the first occurence of a DYBadge if it exists among the view's subviews
/// - Returns: a DYBadge object
public func getBadge()->DYBadge? {

for view in self.subviews {
if let badge = view as? DYBadge {
return badge
}
}
return nil

}

}

public extension UIButton {

private func alignImageAndTitleVertically(padding: CGFloat = 6.0) {

let imageSize = self.imageView!.frame.size
self.titleLabel?.sizeToFit()
self.titleLabel?.textAlignment = .center

let titleSize = self.titleLabel!.frame.size
let totalHeight = imageSize.height + titleSize.height + padding
// print("image height: \(imageSize.height)")
// print("total height: \(totalHeight)")

self.imageEdgeInsets = UIEdgeInsets(
top: -(totalHeight - imageSize.height),
left: 0,
bottom: 0,
right: -titleSize.width
)

self.titleEdgeInsets = UIEdgeInsets(
top: 0,
left: -imageSize.width,
bottom: -(totalHeight - titleSize.height),
right: 0
)
}


/// Create a UIButton with an image and title - the title is placed underneath the image
///
/// - Parameters:
/// - image: a UIImage object
/// - title: the title, placed underneath the image.
/// - font: the font of the title
/// - frame: the frame of the button's image view. the tint color
/// - Returns: a UIButton
public class func createImageTitleButton(image: UIImage, title: String, font: UIFont, frame: CGRect, tintColor: UIColor)->UIButton {

let button = UIButton(frame: frame)
button.contentMode = .scaleAspectFit
button.setImage(image, for: .normal)
button.imageView!.contentMode = .scaleAspectFit
button.setTitle(title, for: .normal)
button.titleLabel?.font = font
button.tintColor = tintColor
button.setTitleColor(tintColor, for: .normal)
button.alignImageAndTitleVertically(padding: 0)
return button

}


}
Loading