Compare commits

...

8 Commits

Author SHA1 Message Date
Cezary Wojcik 9dd9bcecb2 2.3.0 2015-07-11 14:27:26 -07:00
Cezary Wojcik c5875f1311 Merge pull request #56 from benguild/master
Quick fix for lack of NSAttributedString support. Could be improved.
2015-07-11 02:04:52 -07:00
Ben Guild 6d2a139fa0 Changing function and variable names to match UILabel's. 2015-06-14 15:41:36 +09:00
Ben Guild 1a1d1c5d38 Quick fix for lack of NSAttributedString support. Could be improved. 2015-06-14 15:33:04 +09:00
Cezary Wojcik b87af9f288 2.2.6 2015-05-12 12:17:28 -07:00
Cezary Wojcik 975ca58291 2.2.5 2015-05-04 07:08:59 -07:00
Cezary Wojcik d9d8c1d342 Merge pull request #50 from somethingkindawierd/master
Fix calculation of navbar height
2015-05-01 15:39:11 -07:00
Jon Beebe e700d781cc Fix calculation of navbar height 2015-04-30 11:41:45 -05:00
5 changed files with 324 additions and 113 deletions
@@ -311,6 +311,7 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
@@ -350,6 +351,7 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
+1 -1
View File
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "CWStatusBarNotification"
s.version = "2.2.4"
s.version = "2.3.0"
s.summary = "A library that creates status bar notifications."
s.description = "CWStatusBarNotification is a library allows you to present a beautiful text-based notification in the status bar."
s.homepage = "https://github.com/cezarywojcik/CWStatusBarNotification"
+211 -24
View File
@@ -3,75 +3,262 @@
// CWNotificationDemo
//
// Created by Cezary Wojcik on 11/15/13.
// Copyright (c) 2013 Cezary Wojcik. All rights reserved.
// Copyright (c) 2015 Cezary Wojcik. All rights reserved.
//
#import <Foundation/Foundation.h>
/**
* @brief A simple completion used for handling tapping the notification.
*/
typedef void(^CWCompletionBlock)(void);
# pragma mark - ScrollLabel
/**
* A subclass of @c UILabel that scrolls the text if it is too long for the
* label.
*/
@interface ScrollLabel : UILabel
- (CGFloat)scrollTime;
@end
# pragma mark - CWWindowContainer
/**
* A subclass of @c UIWindow that overrides the @c hitTest method in order to
* allow tap events to pass through the window.
*/
@interface CWWindowContainer : UIWindow
/// The height of the notification that is being displayed in the window.
@property (assign, nonatomic) CGFloat notificationHeight;
@end
# pragma mark - CWViewController
/**
* A subclass of @c UIViewController that allows handing over
* @c supportedInterfaceOrientations if needed.
*/
@interface CWViewController : UIViewController
/// Indicates the preferred status bar style.
@property (nonatomic) UIStatusBarStyle preferredStatusBarStyle;
@property (nonatomic, setter=setSupportedInterfaceOrientations:) NSInteger supportedInterfaceOrientations;
/// Indicats the supported interface orientations.
@property (nonatomic, setter=setSupportedInterfaceOrientations:)
UIInterfaceOrientationMask supportedInterfaceOrientations;
@end
# pragma mark - CWStatusBarNotification
/**
* A subclass of @c NSObject that is responsible for managing status bar
* notifications.
*/
@interface CWStatusBarNotification : NSObject
# pragma mark - enums
/**
* @typedef CWNotificationStyle
* @brief Determines the notification style.
*/
typedef NS_ENUM(NSInteger, CWNotificationStyle) {
/// Covers the status bar portion of the screen.
CWNotificationStyleStatusBarNotification,
/// Covers the status bar and navigation bar portions of the screen.
CWNotificationStyleNavigationBarNotification
};
/**
* @typedef CWNotificationAnimationStyle
* @brief Determines the direction of animation for the notification.
*/
typedef NS_ENUM(NSInteger, CWNotificationAnimationStyle) {
/// Animate in from the top or animate out to the top.
CWNotificationAnimationStyleTop,
/// Animate in from the bottom or animate out to the bottom.
CWNotificationAnimationStyleBottom,
/// Animate in from the left or animate out to the left.
CWNotificationAnimationStyleLeft,
/// Animate in from the right or animate out to the right.
CWNotificationAnimationStyleRight
};
/**
* @typedef CWNotificationAnimationType
* @brief Determines whether the notification moves the existing content out of
* the way or simply overlays it.
*/
typedef NS_ENUM(NSInteger, CWNotificationAnimationType) {
/// Moves existing content out of the way.
CWNotificationAnimationTypeReplace,
/// Overlays existing content.
CWNotificationAnimationTypeOverlay
};
# pragma mark - properties
/// The label that holds the notification text.
@property (strong, nonatomic) ScrollLabel *notificationLabel;
@property (strong, nonatomic) UIColor *notificationLabelBackgroundColor;
@property (strong, nonatomic) UIColor *notificationLabelTextColor;
@property (strong, nonatomic) UIFont *notificationLabelFont;
@property (assign, nonatomic) CGFloat notificationLabelHeight;
@property (strong, nonatomic) UIView *customView;
@property (assign, nonatomic) BOOL multiline;
/// The @c UIView that holds a screenshot of the status bar view.
@property (strong, nonatomic) UIView *statusBarView;
/// The block that gets triggered when the notification is tapped.
@property (copy, nonatomic) CWCompletionBlock notificationTappedBlock;
@property (nonatomic) CWNotificationStyle notificationStyle;
@property (nonatomic) NSInteger supportedInterfaceOrientations;
@property (nonatomic) CWNotificationAnimationStyle notificationAnimationInStyle;
@property (nonatomic) CWNotificationAnimationStyle notificationAnimationOutStyle;
@property (nonatomic) CWNotificationAnimationType notificationAnimationType;
/// Indicates whether the notification is currently being shown.
@property (nonatomic) BOOL notificationIsShowing;
/// Indicates whether the notification is currently dismissing.
@property (nonatomic) BOOL notificationIsDismissing;
/// The window that holds the notification.
@property (strong, nonatomic) CWWindowContainer *notificationWindow;
/**
* The background color of the notification label. Default value is the tint
* color of the application's main window.
*/
@property (strong, nonatomic) UIColor *notificationLabelBackgroundColor;
/**
* The text color of the notification label. Default value is white.
*/
@property (strong, nonatomic) UIColor *notificationLabelTextColor;
/**
* The font of the notification label. Default value is system font.
*/
@property (strong, nonatomic) UIFont *notificationLabelFont;
/**
* Allows setting a custom height for the notification label. If this value is
* 0, the height will be determined by the @c notificationStyle. Default value
* is 0.
*/
@property (assign, nonatomic) CGFloat notificationLabelHeight;
/**
* The custom view to present if using @c displayNotificationWithView. Default
* value is @c nil.
*/
@property (strong, nonatomic) UIView *customView;
/**
* Determines whether the notification text has multiple lines. Default value is
* @c NO.
*/
@property (assign, nonatomic) BOOL multiline;
/**
* The supported interface orientations. Default value is
* @c UIInterfaceOrientationMaskAll.
*/
@property (nonatomic) UIInterfaceOrientationMask supportedInterfaceOrientations;
/**
* Determines whether the notification covers the status bar or both the status
* bar and the navigation bar. Default value is
* @c CWNotificationStyleStatusBarNotification.
*/
@property (nonatomic) CWNotificationStyle notificationStyle;
/**
* Determines the direction from which the notification animates in. Default
* value is @c CWNotificationAnimationStyleBottom.
*/
@property (nonatomic) CWNotificationAnimationStyle notificationAnimationInStyle;
/**
* Determines the direction from which the notification animates out. Default
* value is @c CWNotificationAnimationStyleBottom.
*/
@property (nonatomic) CWNotificationAnimationStyle
notificationAnimationOutStyle;
/**
* Determines whether the the notification's animation replaces the existing
* content or overlays it. Default value is
* @c CWNotificationAnimationTypeReplace.
*/
@property (nonatomic) CWNotificationAnimationType notificationAnimationType;
/**
* The preferred status bar style. Default value is @c UIStatusBarStyleDefault.
*/
@property (nonatomic) UIStatusBarStyle preferredStatusBarStyle;
- (void)displayNotificationWithMessage:(NSString *)message forDuration:(CGFloat)duration;
- (void)displayNotificationWithMessage:(NSString *)message completion:(void (^)(void))completion;
- (void)displayNotificationWithView:(UIView *)view forDuration:(CGFloat)duration;
- (void)displayNotificationWithView:(UIView *)view completion:(void (^)(void))completion;
- (void)dismissNotification;
#pragma mark - methods
/**
* Displays a notification with the indicated message and then performs the
* completion block once the notification animates in.
* @param message
* The content of the message to be displayed.
* @param completion
* The block to be invoked once the notification is displayed.
*/
- (void)displayNotificationWithMessage:(NSString *)message
completion:(void (^)(void))completion;
/**
* Displays a notification with the indicated message for the indicated
* duration.
* @param message
* The content of the message to be displayed.
* @param duration
* The amount of seconds for which the notification should be displayed,
* not including the animate in and out times.
*/
- (void)displayNotificationWithMessage:(NSString *)message
forDuration:(CGFloat)duration;
/**
* Displays a notification with the indicated attributed string and then
* performs the completion block once the notification animates in.
* @param attributedString
* The content of the message to be displayed.
* @param completion
* The block to be invoked once the notification is displayed.
*/
- (void)displayNotificationWithAttributedString:(NSAttributedString *)
attributedString
completion:(void (^)(void))completion;
/**
* Displays a notification with the indicated message for the indicated
* duration.
* @param attributedString
* The content of the message to be displayed.
* @param duration
* The amount of seconds for which the notification should be displayed,
* not including the animate in and out times.
*/
- (void)displayNotificationWithAttributedString:(NSAttributedString *)
attributedString
forDuration:(CGFloat)duration;
/**
* Displays a notification with the indicated custom view and then performs the
* completion block once the notification animates in.
* @param view
* The custom @c UIView that you wish to present.
* @param completion
* The block to be invoked once the notification is displayed.
*/
- (void)displayNotificationWithView:(UIView *)view
completion:(void (^)(void))completion;
/**
* Displays a notification with the indicated custom view for the indicated
* duration.
* @param view
* The custom @c UIView that you wish to present.
* @param duration
* The amount of seconds for which the notification should be displayed,
* not including the animate in and out times.
*/
- (void)displayNotificationWithView:(UIView *)view
forDuration:(CGFloat)duration;
/**
* Dismisses the currently presented notification and then performs the
* completion block.
* @param completion
* The block to be invoked after the notification is dismissed.
*/
- (void)dismissNotificationWithCompletion:(void(^)(void))completion;
/**
* Dismisses the currently presented notification.
*/
- (void)dismissNotification;
@end
+109 -87
View File
@@ -3,7 +3,7 @@
// CWNotificationDemo
//
// Created by Cezary Wojcik on 11/15/13.
// Copyright (c) 2013 Cezary Wojcik. All rights reserved.
// Copyright (c) 2015 Cezary Wojcik. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
@@ -17,6 +17,68 @@
#define SCROLL_SPEED 40.0f
#define SCROLL_DELAY 1.0f
# pragma mark - ScrollLabel
@implementation ScrollLabel
{
UIImageView *textImage;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
textImage = [[UIImageView alloc] init];
[self addSubview:textImage];
}
return self;
}
- (CGFloat)fullWidth
{
return [self.text sizeWithAttributes:@{NSFontAttributeName: self.font}].width;
}
- (CGFloat)scrollOffset
{
if (self.numberOfLines != 1) return 0;
CGRect insetRect = CGRectInset(self.bounds, PADDING, 0);
return MAX(0, [self fullWidth] - insetRect.size.width);
}
- (CGFloat)scrollTime
{
return ([self scrollOffset] > 0) ? [self scrollOffset] / SCROLL_SPEED + SCROLL_DELAY : 0;
}
- (void)drawTextInRect:(CGRect)rect
{
if ([self scrollOffset] > 0) {
rect.size.width = [self fullWidth] + PADDING * 2;
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
[super drawTextInRect:rect];
textImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[textImage sizeToFit];
[UIView animateWithDuration:[self scrollTime] - SCROLL_DELAY
delay:SCROLL_DELAY
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut
animations:^{
textImage.transform = CGAffineTransformMakeTranslation(-[self scrollOffset], 0);
} completion:^(BOOL finished) {
}];
} else {
textImage.image = nil;
[super drawTextInRect:CGRectInset(rect, PADDING, 0)];
}
}
@end
# pragma mark - CWWindowContainer
@implementation CWWindowContainer
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
@@ -24,11 +86,9 @@
CGFloat height;
if (SYSTEM_VERSION_LESS_THAN(@"8.0") && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
height = [UIApplication sharedApplication].statusBarFrame.size.width;
}
else {
} else {
height = [UIApplication sharedApplication].statusBarFrame.size.height;
}
if (point.y > 0 && point.y < (self.notificationHeight != 0.0 ? self.notificationHeight : height)) {
return [super hitTest:point withEvent:event];
}
@@ -38,6 +98,8 @@
@end
# pragma mark - CWViewController
@interface CWViewController()
@property (nonatomic, assign) NSInteger _cwViewControllerSupportedInterfaceOrientation;
@@ -51,16 +113,22 @@
return _preferredStatusBarStyle;
}
- (void)setSupportedInterfaceOrientations:(NSInteger)supportedInterfaceOrientations
- (void)setSupportedInterfaceOrientations:(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
self._cwViewControllerSupportedInterfaceOrientation = supportedInterfaceOrientations;
}
- (NSInteger)supportedInterfaceOrientations
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return self._cwViewControllerSupportedInterfaceOrientation;
}
- (BOOL)prefersStatusBarHidden
{
CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
return !(statusBarHeight > 0);
}
@end
# pragma mark - dispatch after with cancellation
@@ -105,66 +173,6 @@ static void cancel_delayed_block(CWDelayedBlockHandle delayedHandle)
delayedHandle(YES);
}
# pragma mark - ScrollLabel
@implementation ScrollLabel
{
UIImageView *textImage;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
textImage = [[UIImageView alloc] init];
[self addSubview:textImage];
}
return self;
}
- (CGFloat)fullWidth
{
return [self.text sizeWithAttributes:@{NSFontAttributeName: self.font}].width;
}
- (CGFloat)scrollOffset
{
if (self.numberOfLines != 1) return 0;
CGRect insetRect = CGRectInset(self.bounds, PADDING, 0);
return MAX(0, [self fullWidth] - insetRect.size.width);
}
- (CGFloat)scrollTime
{
return ([self scrollOffset] > 0) ? [self scrollOffset] / SCROLL_SPEED + SCROLL_DELAY : 0;
}
- (void)drawTextInRect:(CGRect)rect
{
if ([self scrollOffset] > 0) {
rect.size.width = [self fullWidth] + PADDING * 2;
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
[super drawTextInRect:rect];
textImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[textImage sizeToFit];
[UIView animateWithDuration:[self scrollTime] - SCROLL_DELAY
delay:SCROLL_DELAY
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut
animations:^{
textImage.transform = CGAffineTransformMakeTranslation(-[self scrollOffset], 0);
} completion:^(BOOL finished) {
}];
} else {
textImage.image = nil;
[super drawTextInRect:CGRectInset(rect, PADDING, 0)];
}
}
@end
# pragma mark - CWStatusBarNotification
@interface CWStatusBarNotification()
@@ -187,10 +195,14 @@ static void cancel_delayed_block(CWDelayedBlockHandle delayedHandle)
{
self = [super init];
if (self) {
// set defaults
// set default
self.notificationLabelBackgroundColor = [[UIApplication sharedApplication] delegate].window.tintColor;
self.notificationLabelTextColor = [UIColor whiteColor];
self.notificationLabelFont = [UIFont systemFontOfSize:FONT_SIZE];
self.notificationLabelHeight = 0.0;
self.customView = nil;
self.multiline = NO;
self.supportedInterfaceOrientations = UIInterfaceOrientationMaskAll;
self.notificationStyle = CWNotificationStyleStatusBarNotification;
self.notificationAnimationInStyle = CWNotificationAnimationStyleBottom;
self.notificationAnimationOutStyle = CWNotificationAnimationStyleBottom;
@@ -198,7 +210,6 @@ static void cancel_delayed_block(CWDelayedBlockHandle delayedHandle)
self.notificationIsDismissing = NO;
self.isCustomView = NO;
self.preferredStatusBarStyle = UIStatusBarStyleDefault;
self.supportedInterfaceOrientations = UIInterfaceOrientationMaskAll;
// create tap recognizer
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(notificationTapped:)];
@@ -272,7 +283,7 @@ static void cancel_delayed_block(CWDelayedBlockHandle delayedHandle)
- (CGFloat)getNavigationBarHeight
{
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) ||
if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ||
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return 44.0f;
}
@@ -489,6 +500,27 @@ static void cancel_delayed_block(CWDelayedBlockHandle delayedHandle)
}
}
- (void)displayNotificationWithMessage:(NSString *)message forDuration:(CGFloat)duration
{
[self displayNotificationWithMessage:message completion:^{
self.dismissHandle = perform_block_after_delay(duration, ^{
[self dismissNotification];
});
}];
}
- (void)displayNotificationWithAttributedString:(NSAttributedString *)attributedString completion:(void (^)(void))completion
{
[self displayNotificationWithMessage:[attributedString string] completion:completion];
[[self notificationLabel] setAttributedText:attributedString];
}
- (void)displayNotificationWithAttributedString:(NSAttributedString *)attributedString forDuration:(CGFloat)duration
{
[self displayNotificationWithMessage:[attributedString string] forDuration:duration];
[[self notificationLabel] setAttributedText:attributedString];
}
- (void)displayNotificationWithView:(UIView *)view completion:(void (^)(void))completion
{
if (!self.notificationIsShowing) {
@@ -525,9 +557,13 @@ static void cancel_delayed_block(CWDelayedBlockHandle delayedHandle)
}
}
- (void)dismissNotification
- (void)displayNotificationWithView:(UIView *)view forDuration:(CGFloat)duration
{
[self dismissNotificationWithCompletion:nil];
[self displayNotificationWithView:view completion:^{
self.dismissHandle = perform_block_after_delay(duration, ^{
[self dismissNotification];
});
}];
}
- (void)dismissNotificationWithCompletion:(void (^)(void))completion
@@ -560,22 +596,8 @@ static void cancel_delayed_block(CWDelayedBlockHandle delayedHandle)
}
}
- (void)displayNotificationWithMessage:(NSString *)message forDuration:(CGFloat)duration
- (void)dismissNotification
{
[self displayNotificationWithMessage:message completion:^{
self.dismissHandle = perform_block_after_delay(duration, ^{
[self dismissNotification];
});
}];
[self dismissNotificationWithCompletion:nil];
}
- (void)displayNotificationWithView:(UIView *)view forDuration:(CGFloat)duration
{
[self displayNotificationWithView:view completion:^{
self.dismissHandle = perform_block_after_delay(duration, ^{
[self dismissNotification];
});
}];
}
@end
+1 -1
View File
@@ -18,7 +18,7 @@ Works for iPhone and iPad.
### CocoaPods
`pod 'CWStatusBarNotification', '~> 2.2.4'`
`pod 'CWStatusBarNotification', '~> 2.3.0'`
### Manual