iOS開源:FFToast - iOS消息通知的簡單擴展

fantastic 7年前發布 | 15K 次閱讀 iOS開發 移動開發 Github

FFToast

This is a simple extension of an iOS message notifications that can be used to pop up a prompt at the top or bottom of the screen. You can customize it according to their own display.

English instructions

Requirements

  • Requires iOS 8 or later
  • Requires Automatic Reference Counting (ARC)

Features

  • Easy to use
  • Can be easily customized

Installation

CocoaPods

To install FFToast using CocoaPods, please integrate it in your existing Podfile, or create a new Podfile:

target 'MyApp' do
  pod 'FFToast'
end

Then run pod install .

Manual

Add FFToast folder to your project

Usage

#import <FFToast/FFToast.h>

You can create a message notification that shows the default effect at the top by calling the following method:

/**
 Create and display one Toast

@param title Message title @param message Message content @param iconImage Message icon, when toastType is not FFToastTypeDefault iconImage is empty will still have the corresponding icon @param duration Show duration */

  • (void)showToastWithTitle:(NSString )title message:(NSString )message iconImage:(UIImage*)iconImage duration:(NSTimeInterval)duration toastType:(FFToastType)toastType;</code></pre>

    Where toastType:

    typedef NS_ENUM(NSInteger, FFToastType) {
      //Gray background, no icon
      FFToastTypeDefault = 0,
      //Green background + success icon
      FFToastTypeSuccess = 1,
      //Red background + error icon
      FFToastTypeError = 2,
      //Orange background + warning icon
      FFToastTypeWarning = 3,
      //Gray blue background + info icon
      FFToastTypeInfo = 4,
    };

    for example:

    [FFToast showToastWithTitle:@"This is the title" message:@"Message content......." iconImage:[UIImage imageNamed:@"test"] duration:3 toastType:FFToastTypeDefault];

    Title (title), message (message), icon (iconImage) can be empty, FFToast will be based on specific content to adapt.

    If you want to show a message notification below the status bar or below the screen, you can set some properties. Set the display position:

    typedef NS_ENUM(NSInteger, FFToastPosition) {

    //Displayed at the top of the screen FFToastPositionDefault = 0, //Displayed below the status bar FFToastPositionBelowStatusBar = 1, //Displayed below the status bar + rounded corners + left and right margins FFToastPositionBelowStatusBarWithFillet = 2, //Displayed in the bottom of the screen FFToastPositionBottom = 3, //Displayed in the bottom of the screen + rounded FFToastPositionBottomWithFillet = 4

};</code></pre>

Some other attributes:

//background color
@property (strong, nonatomic) UIColor toastBackgroundColor;
//Toast title text color
@property (strong, nonatomic) UIColor titleTextColor;
//Toast content text color
@property (strong, nonatomic) UIColor* messageTextColor;

//Toast title text font @property (strong, nonatomic) UIFont titleFont; //Toast text font @property (strong, nonatomic) UIFont messageFont;

//Toast View corner radius @property(assign,nonatomic)CGFloat toastCornerRadius; //Toast View transparency @property(assign,nonatomic)CGFloat toastAlpha;

//Toast shows the length of time @property(assign,nonatomic)NSTimeInterval duration; //Toast disappear animation is enabled @property(assign,nonatomic)BOOL dismissToastAnimated;

//Toast display position @property (assign, nonatomic) FFToastPosition toastPosition;</code></pre>

After setting the properties, you can call the following method to display it:

/*
 Show a Toast
 /

  • (void)show;</code></pre>

    or:

    /**
    Show a Toast

    @param handler Toast click callback */

  • (void)show:(handler)handler;</code></pre>

    for example:

    FFToast *toast = [[FFToast alloc]initToastWithTitle:@"This is the title" message:@"Message content......." iconImage:[UIImage imageNamed:@"fftoast_info"]];
    toast.toastPosition = FFToastPositionBelowStatusBarWithFillet;
    toast.toastBackgroundColor = [UIColor colorWithRed:75.f/255.f green:107.f/255.f blue:122.f/255.f alpha:1.f];
    [toast show:^{
      //Called when you click message notifications
    }];//[toast show];

    Hide message notifications: The default 3 seconds after the automatic disappear, slide up the pop-up message to inform it will disappear

    About

    Author: imlifengfeng

    WeiBo:@imlifengfeng

    License

    Usage is provided under the MIT License. See LICENSE for full details.

    中文使用說明

    要求

    • 支持iOS 8或更高版本
    • 支持ARC

    特點

    • 簡單易用
    • 可以很容易自定義

    安裝

    CocoaPods

    要使用CocoaPods安裝FFToast,請將其集成到您現有的Podfile中,或創建一個新的Podfile:

    target 'MyApp' do
    pod 'FFToast'
    end

    然后 pod install .

    手動

    將FFToast文件夾添加到項目中

    使用方法

    #import <FFToast/FFToast.h>

    你可以通過調用下面的方法創建一個顯示在頂部的默認效果的消息通知:

    /**
    創建并顯示一個Toast

    @param title 標題 @param message 消息內容 @param iconImage 消息icon,toastType不為FFToastTypeDefault時iconImage為空仍然會有相應icon @param duration 顯示時長 */

  • (void)showToastWithTitle:(NSString )title message:(NSString )message iconImage:(UIImage*)iconImage duration:(NSTimeInterval)duration toastType:(FFToastType)toastType;</code></pre>

    其中的toastType:

    typedef NS_ENUM(NSInteger, FFToastType) {

    //灰色背景、無圖標 FFToastTypeDefault = 0, //綠色背景+成功圖標 FFToastTypeSuccess = 1, //紅色背景+錯誤圖標 FFToastTypeError = 2, //橙色背景+警告圖標 FFToastTypeWarning = 3, //灰藍色背景+信息圖標 FFToastTypeInfo = 4,

};</code></pre>

例如:

[FFToast showToastWithTitle:@"標題" message:@"消息內容......." iconImage:[UIImage imageNamed:@"test"] duration:3 toastType:FFToastTypeDefault];

標題(title)、消息內容(message)、圖標(iconImage)均可以為空,FFToast會根據具體的內容進行自適應。

如果想在狀態欄下方或者屏幕中下方顯示消息通知,可以通過設置一些屬性實現。 設置顯示位置:

typedef NS_ENUM(NSInteger, FFToastPosition) {

//顯示在屏幕頂部
FFToastPositionDefault = 0,
//顯示在狀態欄下方
FFToastPositionBelowStatusBar = 1,
//顯示在狀態欄下方+圓角+左右邊距
FFToastPositionBelowStatusBarWithFillet = 2,
//顯示在屏幕中下方
FFToastPositionBottom = 3,
//顯示在屏幕中下方+圓角
FFToastPositionBottomWithFillet = 4

};</code></pre>

其他的一些屬性:

//背景顏色
@property (strong, nonatomic) UIColor toastBackgroundColor;
//Toast標題文字顏色
@property (strong, nonatomic) UIColor titleTextColor;
//Toast內容文字顏色
@property (strong, nonatomic) UIColor* messageTextColor;

//Toast標題文字字體 @property (strong, nonatomic) UIFont titleFont; //Toast文字字體 @property (strong, nonatomic) UIFont messageFont;

//Toast View圓角 @property(assign,nonatomic)CGFloat toastCornerRadius; //Toast View透明度 @property(assign,nonatomic)CGFloat toastAlpha;

//Toast顯示時長 @property(assign,nonatomic)NSTimeInterval duration; //Toast消失動畫是否啟用 @property(assign,nonatomic)BOOL dismissToastAnimated;

//Toast顯示位置 @property (assign, nonatomic) FFToastPosition toastPosition;</code></pre>

設置完屬性后,就可以調用下面方法將其顯示出來:

/*
 顯示一個Toast
 /

  • (void)show;</code></pre>

    或者:

    /**
    顯示一個Toast

    @param handler Toast點擊回調 */

  • (void)show:(handler)handler;</code></pre>

    例如:

    FFToast *toast = [[FFToast alloc]initToastWithTitle:@"標題" message:@"消息內容......." iconImage:[UIImage imageNamed:@"fftoast_info"]];
    toast.toastPosition = FFToastPositionBelowStatusBarWithFillet;
    toast.toastBackgroundColor = [UIColor colorWithRed:75.f/255.f green:107.f/255.f blue:122.f/255.f alpha:1.f];
    [toast show:^{
      //點擊消息通知時調用
    }];//[toast show];

    隱藏消息通知: 默認3秒后自動消失,向上滑動彈出的消息通知它也會消失。

    關于

    作者: imlifengfeng

    微博:@imlifengfeng

    許可

    該項目在 MIT 許可協議下使用. 有關詳細信息,請參閱 LICENSE .

     

     

     

 本文由用戶 fantastic 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!