RMessage
简介
欢迎来到RMessage!RMessage是对Felix Krause (@KrauseFx) 开发的原始TSMessages库的重构。它将大量旧代码现代化/重构,使其更干净、更容易理解,同时也更符合Objective-C规范。以下是其中的一些更改:
- 使用Autolayout代替易出错的spring/struts frame设置。
- 解决了与通知视图位置相关的许多故障,例如:视图现在能够在状态栏下面正确地调整大小,从而允许状态栏显示。
- 大量代码清理。
- 通过删除混淆的模式和重复,代码库更干净、更容易理解。
- 尽管LOC有限,但方法已被保持较小,仍有改进的空间。
- 允许添加自定义设计而无需通过子类化类,只需在自定义设计的文件中添加新的json "主题"即可 - 自定义设计是累积的,因此您还可以访问默认设计。
- 还添加了各种新的定制选项,例如可以更改:
- 自定义设计中个体通知消息的不透明度,通过“alpha”键。
- 标题和小标题标签不再需要应用相同的阴影。
- 自定义设计图标图像不再需要固定大小。通知会相应地调整大小。
- 无需担心自定义文件中缺失键。如果没有指定,则默认设计会接管。
- 替换了iOS 7推出时损坏的旧模糊代码。
- 使用PPTopMostController增强了默认视图控制器的检测功能。TSMessages会假设窗口根视图控制器是默认视图控制器,这可能导致在模态中使用时出现问题。
- 移除了强制特定的设计 - TSMessages强制使用iOS7或iOS6的设计。在这里没有差异 - 尽管库默认为iOS7设计。
- 还有很多其他的功能:
描述
通知从屏幕顶部移动到导航条下方,并保持几秒钟,取决于显示文本的长度。要在时间耗尽之前关闭通知,用户可以将其向顶部滑动或简单地点击。
已经为你设置了4种不同的类型:成功、错误、警告、正常(查看截图)
添加新的通知类型并使用不同的设计非常简单。
- 将新类型添加到messageType枚举中。
- 创建一个新的配置文件并将其添加到RMessage中,通过调用[RMessage addDesignsFromFileWithName:inBundle:]
- 通过调用RMessageView上的任何类方法并使用类型为RMessageTypeCustom和CustomTypeString等于配置文件中对应您主题的键来显示具有自定义设计的通知。
查看示例项目以了解如何使用这个库。 确保打开工作区,而不是项目文件,因为示例项目使用 Cocoapods。
通过Twitter联系开发者:donileo (Adonis Peralta)
安装
从 CocoaPods 获得
RMessage 可以通过 CocoaPods 获得安装。只需将以下行添加到您的 Podfile 中即可安装:
pod "RMessage"
手动操作
将源文件 RMessageView 和 RMessage 复制到您的项目中。同时复制 RMessageDefaultDesign.json 文件。
使用方法
要显示通知,请使用以下代码:
[RMessage showNotificationWithTitle:@"Your Title"
subtitle:@"A description"
type:RMessageTypeError
customTypeName:nil
callback:nil];
// Add an icon image inside the message, appears on the left
[RMessage showNotificationInViewController:self
title:@"Update available"
subtitle:@"Please update the app"
iconImage:iconUIImageHere
type:RMessageTypeNormal
customTypeName:nil
duration:RMessageDurationAutomatic
callback:nil
buttonTitle:@"Update"
buttonCallback:^{
NSLog(@"User tapped the button");
}
atPosition:RMessagePositionTop
canBeDismissedByUser:YES];
// Use a custom design file must be a json file though no need to include the json extension in the argument
[RMessage addDesignsFromFileWithName:@"AlternativeDesigns" inBundle:[NSBundle mainBundle]]; // has an @"alternate-error" key specified with custom design properties
[RMessage showNotificationWithTitle:@"Your Title"
subtitle:@"A description"
type:RMessageTypeCustom
customTypeName:@"alternate-error"
callback:nil];
您可以在一个默认视图中定义显示通知的地方。
[RMessage setDefaultViewController:myNavController];
您可以在消息视图显示之前进行自定义,例如设置透明度值或添加自定义子视图。
[RMessage setDelegate:self];
...
- (void)customizeMessageView:(RMessageView *)messageView
{
messageView.alpha = 0.4;
[messageView addSubview:...];
}
您可以使用UIAppearance对消息视图元素进行自定义。
#import "RMessageView.h"
@implementation AppDelegate
....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//If you want you can override some properties using UIAppearance
[[RMessageView appearance] setTitleFont:[UIFont boldSystemFontOfSize:6]];
[[RMessageView appearance] setTitleTextColor:[UIColor redColor]];
[[RMessageView appearance] setSubtitleFont:[UIFont boldSystemFontOfSize:10]];
[[RMessageView appearance] setSubtitleTextColor:[UIColor greenColor]];
[[RMessageView appearance] setErrorIcon:[UIImage imageNamed:@"NotificationButtonBackground"]];
[[RMessageView appearance] setSuccessIcon:[UIImage imageNamed:@"NotificationButtonBackground"]];
[[RMessageView appearance] setMessageIcon:[UIImage imageNamed:@"NotificationButtonBackground"]];
[[RMessageView appearance] setWarningIcon:[UIImage imageNamed:@"NotificationButtonBackground"]];
//End of override
return YES;
}
自定义设计文件属性
有关自定义设计文件属性的列表/描述,请参见: CustomDesignFilePropertySpecs.md
有关自定义设计文件属性的版本兼容性,请参见: CustomDesignFilePropertyCompatibility.md
截图
设计示例
许可
RMessage 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。
最近更改
可以在本仓库的发行版部分找到。