iOS開源:FFToast - GitHub 中最強大的 Notification 和 AlertView 框架
FFToast
A very powerful iOS message notifications and AlertView extensions. It can be easily realized from the top of the screen, the bottom of the screen and the middle of the screen pops up a notification. You can easily customize the pop-up View.
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></pre>
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;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 the message below the status bar, the bottom of the screen or the middle of the screen, you can set some properties to achieve. Set the display position:
typedef NS_ENUM(NSInteger, FFToastPosition) { //Displayed at the top of the screen FFToastPositionDefault = 0, //Is displayed below the status bar FFToastPositionBelowStatusBar = 1, //Displayed below the status bar + rounded corners + left and right margins FFToastPositionBelowStatusBarWithFillet = 2, //Displayed at the bottom of the screen FFToastPositionBottom = 3, //Displayed at the bottom of the screen + fillet FFToastPositionBottomWithFillet = 4, //Displayed in the middle of the screen FFToastPositionCentre = 5, //Displayed in the middle of the screen + fillet FFToastPositionCentreWithFillet = 6 };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 fillet @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; //Toast display type @property (assign, nonatomic) FFToastType toastType; //Whether it is automatically hidden. AutoDismiss, enableDismissBtn, dismissBtnImage The three properties are only valid for Toast that pops up from the center of the screen @property(assign,nonatomic)BOOL autoDismiss; //Whether the hidden button is displayed in the upper right corner @property(assign,nonatomic)BOOL enableDismissBtn; //Hide the button's icon @property (strong, nonatomic) UIImage* dismissBtnImage;After setting the properties, you can call the following method to display it:
/** Show a Toast */ - (void)show;or:
/** Show a Toast @param handler Toast click callback */ - (void)show:(handler)handler;E.g:
FFToast *toast = [[FFToast alloc]initToastWithTitle:@"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:^{ //Toast is called when clicked }];//[toast show];If you want to customize a Toast from the middle, you can call the following method:
/** Show a custom Toast in the middle @param customToastView Customized ToastView @param autoDismiss Whether it is automatically hidden @param duration Display duration (autoDismiss = NO this parameter will be invalid) @param enableDismissBtn Whether to show the hidden button @param dismissBtnImage Hide button image (enableDismissBtn = NO this parameter will be invalid) @return Toast */ - (instancetype)initCentreToastWithView:(UIView *)customToastView autoDismiss:(BOOL)autoDismiss duration:(NSTimeInterval)duration enableDismissBtn:(BOOL)enableDismissBtn dismissBtnImage:(UIImage*)dismissBtnImage;When you customize Toast from the middle, you can set the above parameters autoDismiss and the parameter enableDismissBtn to NO. And then in your custom View in their own place in the appropriate place to add a close button. To close the Toast from the middle, you can call the following method:
/** Hide a Toast */ - (void)dismissCentreToast;Top, the bottom of the pop-up Toast can not customize View, but for iconImage, Title, message can be set according to need and can be nil, the final Toast will be based on specific content to adapt.
Hide message notifications: The default 3 seconds after the automatic disappear, slide up the pop-up message to inform it will disappear
中文使用說明
FFToast是一個非常強大的iOS message notifications和AlertView擴展。它可以很容易實現從屏幕頂部、屏幕底部和屏幕中間彈出一個通知。你可以很容易的自定義彈出的View.
要求
- 支持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;
其中的toastType:
typedef NS_ENUM(NSInteger, FFToastType) { //灰色背景、無圖標 FFToastTypeDefault = 0, //綠色背景+成功圖標 FFToastTypeSuccess = 1, //紅色背景+錯誤圖標 FFToastTypeError = 2, //橙色背景+警告圖標 FFToastTypeWarning = 3, //灰藍色背景+信息圖標 FFToastTypeInfo = 4, };
例如:
[FFToast showToastWithTitle:@"標題" message:@"消息內容......." iconImage:[UIImage imageNamed:@"test"] duration:3 toastType:FFToastTypeDefault];
標題(title)、消息內容(message)、圖標(iconImage)均可以為nil,FFToast會根據具體的內容進行自適應。
如果想在狀態欄下方、屏幕下方或者屏幕中間顯示消息通知,可以通過設置一些屬性實現。 設置顯示位置:
typedef NS_ENUM(NSInteger, FFToastPosition) { //顯示在屏幕頂部 FFToastPositionDefault = 0, //顯示在狀態欄下方 FFToastPositionBelowStatusBar = 1, //顯示在狀態欄下方+圓角+左右邊距 FFToastPositionBelowStatusBarWithFillet = 2, //顯示在屏幕底部 FFToastPositionBottom = 3, //顯示在屏幕底部+圓角 FFToastPositionBottomWithFillet = 4, //顯示在屏幕中間 FFToastPositionCentre = 5, //顯示在屏幕中間+圓角 FFToastPositionCentreWithFillet = 6 };
其他的一些屬性:
//背景顏色 @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; //Toast顯示類型 @property (assign, nonatomic) FFToastType toastType; //是否自動隱藏,autoDismiss、enableDismissBtn、dismissBtnImage三個屬性僅對從屏幕中間彈出的Toast有效 @property(assign,nonatomic)BOOL autoDismiss; //是否在右上角顯示隱藏按鈕 @property(assign,nonatomic)BOOL enableDismissBtn; //隱藏按鈕的圖標 @property (strong, nonatomic) UIImage* dismissBtnImage;
設置完屬性后,就可以調用下面方法將其顯示出來:
/** 顯示一個Toast */ - (void)show;
或者:
/** 顯示一個Toast @param handler Toast點擊回調 */ - (void)show:(handler)handler;
例如:
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];
如果你想自定義一個從中間彈出的Toast,可以調用下面的方法:
/** 在中間顯示一個自定義Toast @param customToastView 自定義的ToastView @param autoDismiss 是否自動隱藏 @param duration 顯示時長(autoDismiss = NO時該參數將無效) @param enableDismissBtn 是否顯示隱藏按鈕 @param dismissBtnImage 隱藏按鈕圖片(enableDismissBtn = NO時該參數將無效) @return Toast */ - (instancetype)initCentreToastWithView:(UIView *)customToastView autoDismiss:(BOOL)autoDismiss duration:(NSTimeInterval)duration enableDismissBtn:(BOOL)enableDismissBtn dismissBtnImage:(UIImage*)dismissBtnImage;
你在自定義從中間彈出的Toast時,你可以將上面的參數autoDismiss和參數enableDismissBtn設為NO。然后在你自定義的View中自己在合適的位置添加一個關閉按鈕。 關閉從中間彈出的Toast,可以調用下面的方法:
/** 隱藏一個Toast */ - (void)dismissCentreToast;
頂部、底部彈出的Toast不可自定義View,但是對于iconImage、Title、message均可以根據需要設置并且可以為nil,最終Toast會根據具體的內容進行自適應。
隱藏消息通知: 默認3秒后自動消失,向上滑動彈出的消息通知它也會消失。