Permalink
Browse files

New properties for title, message and visibility.

This allows you, for example, to have an instance of OLGhostAlertView set as a property on your View Controller and set its contents, hide it and show it in a more flexible way.
  • Loading branch information...
1 parent 76d0973 commit 1a5f7c8c43601662f685063371a3d4e18a52c98e @radutzan radutzan committed Apr 24, 2013
Showing with 63 additions and 22 deletions.
  1. +3 −0 OLGhostAlertView.h
  2. +40 −20 OLGhostAlertView.m
  3. +2 −2 OLGhostAlertView.podspec
  4. +18 −0 README.md
View
3 OLGhostAlertView.h
@@ -23,5 +23,8 @@ typedef NS_ENUM(NSUInteger, OLGhostAlertViewPosition) {
@property (nonatomic) OLGhostAlertViewPosition position;
@property (nonatomic, copy) void (^completionBlock)(void);
+@property (nonatomic) NSString *title;
+@property (nonatomic) NSString *message;
+@property (nonatomic, readonly, getter=isVisible) BOOL visible;
@end
View
60 OLGhostAlertView.m
@@ -26,8 +26,9 @@ @interface OLGhostAlertView ()
@property NSTimeInterval timeout;
@property UIInterfaceOrientation interfaceOrientation;
@property CGFloat bottomMargin;
-@property BOOL isShowingKeyboard;
+@property BOOL keyboardIsVisible;
@property CGFloat keyboardHeight;
+@property (nonatomic, readwrite) BOOL visible;
@end
@@ -103,19 +104,13 @@ - (id)initWithFrame:(CGRect)frame
- (id)initWithTitle:(NSString *)title message:(NSString *)message timeout:(NSTimeInterval)timeout dismissible:(BOOL)dismissible
{
- // title cannot be nil. message can be nil.
- if (!title) {
- NSLog(@"OLGhostAlertView: title cannot be nil. Your app will now crash.");
- return self;
- }
-
self = [self initWithFrame:CGRectZero];
if (self) {
- _titleLabel.text = title;
+ self.title = title;
+
+ self.message = message;
if (message) {
- _messageLabel.text = message;
-
[self addSubview:_messageLabel];
}
@@ -149,6 +144,12 @@ - (id)initWithTitle:(NSString *)title
- (void)show
{
+ if (!self.title && !self.message) {
+ NSLog(@"OLGhostAlertView: Your alert doesn't seem to have any content.");
+ }
+
+ if (self.isVisible) return;
+
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
UIView *parentView;
@@ -167,6 +168,8 @@ - (void)show
[parentView addSubview:self];
+ self.visible = YES;
+
[UIView animateWithDuration:0.5 animations:^{
self.alpha = 1;
} completion:^(BOOL finished) {
@@ -181,6 +184,8 @@ - (void)hide
[UIView animateWithDuration:0.5 animations:^{
self.alpha = 0;
} completion:^(BOOL finished){
+ self.visible = NO;
+
[self removeFromSuperview];
if (self.completionBlock) {
@@ -213,11 +218,11 @@ - (void)layoutSubviews
constrainedSize.width = maxWidth;
constrainedSize.height = MAXFLOAT;
- CGSize titleSize = [self.titleLabel.text sizeWithFont:[UIFont boldSystemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:constrainedSize];
+ CGSize titleSize = [self.title sizeWithFont:[UIFont boldSystemFontOfSize:TITLE_FONT_SIZE] constrainedToSize:constrainedSize];
CGSize messageSize = CGSizeZero;
- if (self.messageLabel.text) {
- messageSize = [self.messageLabel.text sizeWithFont:[UIFont systemFontOfSize:MESSAGE_FONT_SIZE] constrainedToSize:constrainedSize];
+ if (self.message) {
+ messageSize = [self.message sizeWithFont:[UIFont systemFontOfSize:MESSAGE_FONT_SIZE] constrainedToSize:constrainedSize];
totalHeight = titleSize.height + messageSize.height + floorf(VERTICAL_PADDING * 2.5);
@@ -258,7 +263,7 @@ - (void)layoutSubviews
self.frame = CGRectMake(xPosition, yPosition, totalWidth, totalHeight);
- if (self.isShowingKeyboard && self.position == OLGhostAlertViewPositionBottom) {
+ if (self.keyboardIsVisible && self.position == OLGhostAlertViewPositionBottom) {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y - self.keyboardHeight, self.frame.size.width, self.frame.size.height);
}
@@ -274,15 +279,15 @@ - (void)keyboardWillShow:(NSNotification*)notification
NSDictionary *keyboardInfo = [notification userInfo];
CGSize keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
- self.isShowingKeyboard = YES;
+ self.keyboardIsVisible = YES;
self.keyboardHeight = keyboardSize.height;
[self setNeedsLayout];
}
- (void)keyboardWillHide:(NSNotification*)notification
{
- self.isShowingKeyboard = NO;
+ self.keyboardIsVisible = NO;
[self setNeedsLayout];
}
@@ -298,14 +303,29 @@ - (void)didChangeOrientation:(NSNotification *)notification
- (CGRect)superviewBoundsForCurrentOrientation
{
- return [self screenBoundsForOrientation:self.interfaceOrientation];
+ CGRect screenRect = self.superview.bounds;
+
+ return screenRect;
}
-- (CGRect)screenBoundsForOrientation:(UIInterfaceOrientation)orientation
+#pragma mark - Title and message setters
+
+- (void)setTitle:(NSString *)title
{
- CGRect screenRect = self.superview.bounds;
+ _title = title;
- return screenRect;
+ self.titleLabel.text = title;
+
+ [self setNeedsLayout];
+}
+
+- (void)setMessage:(NSString *)message
+{
+ _message = message;
+
+ self.messageLabel.text = message;
+
+ [self setNeedsLayout];
}
#pragma mark - dealloc
View
4 OLGhostAlertView.podspec
@@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = 'OLGhostAlertView'
- s.version = '1.3'
+ s.version = '1.4'
s.license = 'Public Domain'
s.platform = :ios
s.summary = 'Temporary and unobtrusive translucent alert view for iPhone and iPad.'
s.homepage = 'https://github.com/ondalabs/OLGhostAlertView'
s.authors = { 'Radu Dutzan' => 'radu@ondalabs.com'}
- s.source = { :git => 'https://github.com/ondalabs/OLGhostAlertView.git', :tag => '1.3' }
+ s.source = { :git => 'https://github.com/ondalabs/OLGhostAlertView.git', :tag => '1.4' }
s.source_files = 'OLGhostAlertView.{h,m}'
s.requires_arc = true
View
18 README.md
@@ -79,6 +79,24 @@ A block to execute after the instance has been dismissed.
@property (nonatomic, copy) void (^completionBlock)(void);
+#### title
+The string that appears in the title of the alert.
+
+ @property (nonatomic) NSString *title;
+
+Setting this property after initialization recalculates the view's metrics.
+
+#### message
+Descriptive text that provides more details than the title.
+
+ @property (nonatomic) NSString *message;
+
+Setting this property after initialization recalculates the view's metrics.
+
+#### visible
+A Boolean value that indicates whether the view is currently visible on the screen.
+
+ @property (nonatomic, readonly, getter=isVisible) BOOL visible;
Known Issues
---------------

0 comments on commit 1a5f7c8

Please sign in to comment.