Git Product home page Git Product logo

toast-swift's Introduction

Toast-Swift

Codacy Badge

A Swift Toast view - iOS 14 style - built with UIKit. ๐Ÿž

Installation

Swift Package Manager

You can use The Swift Package Manager to install Toast-Swift by adding the description to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/BastiaanJansen/toast-swift", from: "2.1.2")
]

CocoaPods

pod "ToastViewSwift"

Usage

To create a simple text based toast:

let toast = Toast.text("Safari pasted from Notes")
toast.show()

Or add a subtitle:

let toast = Toast.text("Safari pasted from Notes", subtitle: "A few seconds ago")
toast.show()

And if you want to use your own font(NSAttributedString is supported):

let attributes = [
    NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 17)!, 
    NSAttributedStringKey.foregroundColor: UIColor.black
]
let attributedString  = NSMutableAttributedString(string: "Safari pasted from Notes" , attributes: attributes)
let toast = Toast.text(attributedString)
toast.show()

If you want to add an icon, use the default method to construct a toast:

let toast = Toast.default(
    image: UIImage(systemName: "airpodspro")!,
    title: "Airpods Pro",
    subtitle: "Connected"
)
toast.show()

Want to use a different layout, but still use the Apple style? Create your own view and inject it into the AppleToastView class when creating a custom toast:

let customView: UIView = // Custom view

let appleToastView = AppleToastView(child: customView)

let toast = Toast.custom(view: appleToastView)
toast.show()

The show method accepts several optional parameters. haptic of type UINotificationFeedbackGenerator.FeedbackType to use haptics and after of type TimeInterval to show the toast after a certain amount of time:

toast.show(haptic: .success, after: 1)

Configuration options

The text, default and custom methods support custom configuration options. The following options are available:

Name Description Type Default
direction Where the toast will be shown. .bottom or .up .up
dismissBy Choose when the toast dismisses. Dismissable [.time, .swipe]
animationTime Duration of the show and close animation in seconds. TimeInterval 0.2
enteringAnimation The type of animation that will be used when toast is showing .slide, .fade, .scaleAndSlide, .scale and .custom .default
exitingAnimation The type of animation that will be used when toast is exiting .slide, .fade, .scaleAndSlide, .scale and .custom .default
attachTo The view which the toast view will be attached to. UIView nil
let config = ToastConfiguration(
    direction: .top,
    dismissBy: [.time(time: 4.0), .swipe(direction: .natural), .longPress],
    animationTime: 0.2
)

let toast = toast.text("Safari pasted from Notes", config: config)

Custom entering/exiting animations

self.toast = Toast.text(
            "Safari pasted from Noted",
            config: .init(
                direction: .bottom,
                enteringAnimation: .fade(alphaValue: 0.5),
                exitingAnimation: .slide(x: 0, y: 100))
            ).show()

The above configuration will show a toast that will appear on screen with an animation of fade-in. And then when exiting will go down and disapear.

self.toast = Toast.text(
            "Safari pasted from Noted",
            config: .init(
                direction: .bottom,
                enteringAnimation: .scale(scaleX: 0.6, scaleY: 0.6),
                exitingAnimation: .default
            ).show()

The above configuration will show a toast that will appear on screen with scaling up animation from 0.6 to 1.0. And then when exiting will use our default animation (which is scaleAndSlide)

For more on animation see the Toast.AnimationType enum.

Custom toast view

Don't like the default Apple'ish style? No problem, it is also possible to use a custom toast view with the custom method. Firstly, create a class that confirms to the ToastView protocol:

class CustomToastView : UIView, ToastView {
    private let text: String

    public init(text: String) {
        self.text = text
    }

    func createView(for toast: Toast) {
        // View is added to superview, create and style layout and add constraints
    }
}

Use your custom view with the custom construct method on Toast:

let customToastView: ToastView = CustomToastView(text: "Safari pasted from Notes")

let toast = Toast.custom(view: customToastView)
toast.show()

Queues

To show toasts after each other, use the ToastQueue class:

let toast1 = Toast.text("Notification 1")
let toast2 = Toast.text("Notification 2")
let toast3 = Toast.text("Notification 3")

let queue = ToastQueue([toast1, toast2, toast3])

queue.show()

Delegates

Below delegate functions are optional to implement when implementing ToastDelegate.

extension MyViewController: ToastDelegate {
    func willShowToast(_ toast: Toast) {
        print("Toast will be shown after this")
    }

    func didShowToast(_ toast: Toast) {
        print("Toast was shown")
    }

    func willCloseToast(_ toast: Toast) {
        print("Toast will be closed after this")
    }

    func didCloseToast(_ toast: Toast) {
        print("Toast was closed (either automatically, dismissed by user or programmatically)")
    }
}

Licence

Toast-Swift is available under the MIT licence. See the LICENCE for more info.

Stargazers repo roster for @BastiaanJansen/Toast-Swift

toast-swift's People

Contributors

5kylar avatar allen870619 avatar bastiaanjansen avatar cederache avatar fromatom avatar honcon avatar jisu15-kim avatar lukszar avatar mmmcheese avatar nikoloutsos avatar nmtitov avatar swapnanildhol avatar tornikegomareli avatar twomedia avatar zandor300 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

toast-swift's Issues

Add snapshot tests

I think it is worth adding some snapshot tests on the views.
That way we can be more confident merging PRs.
Also, we can add a Github action workflow for running the snapshot tests in every pull request.
IIRC github actions are free for open source project โค๏ธ

What do you think? @BastiaanJansen

Attach to view

I got problems attaching the toast to the top of the tabBar
without attach argument it show in the top of the view controller, I want it to show to the bottom of it
when trying
utilsUi.showToast(title: "tet", message: "test", attachTo: self.tabBarController?.tabBar)
I got crash

Dismiss callback

Is there a way to know when the toast is dismissed? Something like a delegate or a callback?

Custom font

Can I change font for Toast title?
No any information about this

I have a problem. A really, really, big problem.

After the first popover pops up, click the button to pop up the second popover and repeat to N pop-ups. It's going to add up, the shadow looks heavy, and what I need is the first one to close when the second one pops up, so how do I do that?

Toast bottom direction broke swipe to remove

Great library. The problem is when enablePanToClose = true and direction = .bottom, when try to swipe from top to bottom - the whole app is trying to collapse (default for ios). Can you, please, add direction of swiping too?

Custom styles

Is it possible to customize the font family, text & background colors, etc?

Thanks for the awesome library!

Running resolver because the following dependencies were added: 'toast-swift'

Hello, I have a problem with compiling:

a resolved file is required when automatic dependency resolution is disabled and should be placed at /Volumes/workspace/repository/UOnLoad.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved. Running resolver because the following dependencies were added: 'toast-swift' (https://github.com/BastiaanJansen/toast-swift)2022-12-09 11:43:56.738 xcodebuild[4147:18228] Writing error result bundle to /var/folders/r0/yrl1s4f524n1lpryy_y16t2r0000gn/T/ResultBundle_2022-09-12_11-43-0056.xcresult

Dark style

I don't see where I can apply the dark style toast, there is no parameters ?

Tap to dismiss

Hello - Thanks for this, really like it.

A little feature request. Please could you implement an option in the config for a 'tap to dismiss' option?

Cheers :)

Support animation type

Thinking of making a PR that will give the ability to choose the toast entering/exiting animation.

let config = ToastConfiguration(
    direction: .up,
    autoHide: true,
    enablePanToClose: true,
    displayTime: 5,
    animationTime: 0.2
+   animationType: .slide 
)

also fade animation type can be an option.

What do you think ๐Ÿค”

Number of lines option

It truncates the texts when they are too long. It would be good if we can set the number of lines in the config. Also, minimum scale factor can be useful too.

Especially needed when you don't use a subtitle but a long title.

Screenshot 2023-08-11 at 21 30 01

Add visionOS support

This lib could be useful for visionOS apps. Haven't done a lot yet with visionOS apps but since some people already made visionOS precompile conditional statements, someone else could join in on this discussion.

Currently visionOS isn't listed as a supported platform in SwiftPM Package.swift or Cocoapods podspec. (They were out of sync so I created PR #54 to fix that)

If the library works out of the box we could add visionOS platform support, but maybe other changes are needed to get it to work or to polish it.

Just wanted to start the discussion.

(I live in EU, no Apple Vision Pro for me ๐Ÿ˜ญ)

Timer invalidation breaking pan gesture

With the current version the closeTimer?.invalidate() in the pan gesture handler is breaking the gesture with the following error: [SystemGestureGate] <0x143508050> Gesture: System gesture gate timed out.. The gesture handler is then not called anymore and the toast is then frozen and is not disappearing anymore.
Does someone else has a similar behavior?
It tested it on Xcode 14.0.1, 14.1 - iOS 16 Simulator and device with SwiftUI and UIKit in a plain project.

If I remove the invalidation everything works well, but I cannot figure out why this happens.

Custom toast view

On your code page you have an example for CustomToastView, could you add some sample code of what would go into the func createView(for toast: Toast).

Image doesn't show up

Our app is a hybrid SwiftUI / UIKit app that forces Dark Mode at all times.

I've tried everything to get the image to appear, but it is always just a blank spot. Even the sample code doesn't work:

let toast = Toast.default(
image: UIImage(systemName: "airpodspro")!,
title: "Airpods Pro",
subtitle: "Connected"
)
toast.show()

The toast shows up as expected, but with a blank area instead of an image.

Need toast message position ?

/* Its not a issue its suggestion */
โ€“> it would be great if ya provide where to show toast like top, bottom, center .....
โ€“> toast need a dynamic height like what if your title and subtile have more then 3 lines ?

thank you... its suggestion not a complain ๐Ÿ™‚

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.