Skip to content

Commit

Permalink
UIAlertView-based implementation of UI7AlertView
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone committed Jun 15, 2013
1 parent acea418 commit 9ae12af
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 120 deletions.
2 changes: 0 additions & 2 deletions UI7Kit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
3801741617682DDA0049D26C /* UI7TableViewCellDisclosureIndicator.png in Resources */ = {isa = PBXBuildFile; fileRef = 3801741517682DDA0049D26C /* UI7TableViewCellDisclosureIndicator.png */; };
38017419176849680049D26C /* UI7NavigationBarPortrait@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 38017417176849680049D26C /* UI7NavigationBarPortrait@2x.png */; };
3801741A176849680049D26C /* UI7NavigationBarPortraitLandscape@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 38017418176849680049D26C /* UI7NavigationBarPortraitLandscape@2x.png */; };
38017424176989650049D26C /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 383764D713373154007072FD /* Foundation.framework */; };
38017432176989660049D26C /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 38C7A9891495181800381910 /* SenTestingKit.framework */; };
38017433176989660049D26C /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 383764E213373155007072FD /* UIKit.framework */; };
38017434176989660049D26C /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 383764D713373154007072FD /* Foundation.framework */; };
Expand Down Expand Up @@ -164,7 +163,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
38017424176989650049D26C /* Foundation.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
36 changes: 1 addition & 35 deletions UI7Kit/UI7AlertView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,6 @@
//


@interface UI7AlertView : UIView

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

@property(nonatomic,readonly) UILabel *titleLabel;
@property(nonatomic,readonly) UILabel *messageLabel;

@property(nonatomic,assign) id /*<UIAlertViewDelegate>*/ delegate; // weak reference
@property(nonatomic,copy) NSString *title;
@property(nonatomic,copy) NSString *message; // secondary explanation text


// adds a button with the title. returns the index (0 based) of where it was added. buttons are displayed in the order added except for the
// cancel button which will be positioned based on HI requirements. buttons cannot be customized.
- (NSInteger)addButtonWithTitle:(NSString *)title; // returns index of button. 0 based.
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
@property(nonatomic,readonly) NSInteger numberOfButtons;
@property(nonatomic) NSInteger cancelButtonIndex; // if the delegate does not implement -alertViewCancel:, we pretend this button was clicked on. default is -1

@property(nonatomic,readonly) NSInteger firstOtherButtonIndex; // -1 if no otherButtonTitles or initWithTitle:... not used
@property(nonatomic,readonly,getter=isVisible) BOOL visible;

// shows popup alert animated.
- (void)show;

// hides alert sheet or popup. use this method when you need to explicitly dismiss the alert.
// it does not need to be called if the user presses on a button
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

// Alert view style - defaults to UIAlertViewStyleDefault
@property(nonatomic,assign) UIAlertViewStyle alertViewStyle NS_AVAILABLE_IOS(5_0);

/* Retrieve a text field at an index - raises NSRangeException when textFieldIndex is out-of-bounds.
The field at index 0 will be the first text field (the single field or the login field), the field at index 1 will be the password field. */
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);
@interface UI7AlertView : UIAlertView

@end
189 changes: 108 additions & 81 deletions UI7Kit/UI7AlertView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,51 @@

#import "UI7AlertView.h"

@interface UIAlertView (Private)

@property(nonatomic,readonly) UILabel *titleLabel;
@property(nonatomic,readonly) UILabel *bodyTextLabel;
@property(nonatomic,readonly) NSArray *buttons;

@end


@interface UIAlertView (Accessor)

@property(nonatomic,retain) UIView *backgroundImageView;

@end


@implementation UIAlertView (Accessor)

- (void)setBackgroundImageView:(UIView *)backgroundImageView {
id view;
[backgroundImageView retain];
object_getInstanceVariable(self, "_backgroundImageView", (void **)&view);
[view release];
object_setInstanceVariable(self, "_backgroundImageView", backgroundImageView);
}

- (UIView *)backgroundImageView {
void *view;
object_getInstanceVariable(self, "_backgroundImageView", &view);
return view;
}

@end


@interface UIAlertButton: UIButton

@property(nonatomic,copy) NSString *title;

@end


@interface UI7AlertView ()

@property(nonatomic,assign) UIView *frameView;
@property(nonatomic,retain) NSArray *buttons;

@property(nonatomic,assign) UILabel *titleLabel;
@property(nonatomic,assign) UILabel *messageLabel;

@property(nonatomic,assign) UIView *strokeView;

Expand All @@ -28,38 +66,19 @@ - (void)_buttonDidSelected:(id)sender;
@implementation UI7AlertView

@synthesize frameView=_frameView;
@synthesize buttons=_buttons;
@synthesize titleLabel=_titleLabel, messageLabel=_messageLabel;
@synthesize strokeView=_strokeView;

@synthesize cancelButtonIndex;

- (void)dealloc {
self.buttons = nil;
[super dealloc];
}

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
self = [super initWithFrame:CGRectMake(.0, .0, 320.0, 480.0)]; // rough
self = [super init]; // rough
if (self != nil) {
self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1.0];
self.backgroundImageView = [UIImage blankImage].view;

UIView *frameView = self.frameView = [[[UIView alloc] initWithFrame:CGRectMake(.0, .0, 270.0, 100.0)] autorelease];
UIView *frameView = self.frameView = [[[UIView alloc] initWithFrame:CGRectMake(.0, .0, 284.0, 141.0)] autorelease];
frameView.backgroundColor = [UIColor iOS7BackgroundColor]; // temp

self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(13.0, 20.0, frameView.frame.size.width - 26.0, .0)];
self.titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel.textAlignment = UITextAlignmentCenter;
self.titleLabel.font = [UIFont iOS7SystemFontOfSize:16.0 weight:UI7FontWeightMedium];
self.titleLabel.numberOfLines = 3;
[frameView addSubview:self.titleLabel];

self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(13.0, 20.0, frameView.frame.size.width - 26.0, .0)];
self.messageLabel.backgroundColor = [UIColor clearColor];
self.messageLabel.textAlignment = UITextAlignmentCenter;
self.messageLabel.font = [UIFont iOS7SystemFontOfSize:16.0 weight:UI7FontWeightLight];
self.messageLabel.numberOfLines = 5;
[frameView addSubview:self.messageLabel];

self.strokeView = [[UIView alloc] initWithFrame:CGRectMake(.0, .0, frameView.frame.size.width, 0.5)];
self.strokeView.backgroundColor = [UIColor colorWith8BitWhite:182 alpha:255];
Expand All @@ -70,7 +89,7 @@ - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)d
self.title = title;
self.message = message;
self.delegate = delegate;
self.buttons = [NSArray array];
// self.buttons = [NSArray array];

va_list titlep;
va_start(titlep, otherButtonTitles);
Expand All @@ -87,68 +106,76 @@ - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)d
}

- (void)show {
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
self.frame = window.bounds;
{
CGRect frame = self.titleLabel.frame;
self.titleLabel.text = self.title;
CGSize size = [self.title sizeWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(frame.size.width, INFINITY)];
frame.size.height = size.height;
self.titleLabel.frame = frame;
}
{
CGRect frame = self.messageLabel.frame;
frame.origin.y = self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height + 2.0f;
self.messageLabel.text = self.message;
CGSize size = [self.message sizeWithFont:self.messageLabel.font constrainedToSize:CGSizeMake(frame.size.width, INFINITY)];
frame.size.height = size.height;
self.messageLabel.frame = frame;
// [self.buttons applyProcedureWithIndex:^(id obj, NSUInteger index) {
// UI7Button *button = obj;
// CGFloat width = self.frameView.frame.size.width / self.buttons.count;
// CGRect frame = CGRectMake(width * index, self.strokeView.frame.origin.y + 1.0f, self.frameView.frame.size.width / self.buttons.count, 45.0);
// button.frame = frame;
// }];

// {
// self.frameView.center = self.center;
//
// [window addSubview:self];
// }
self.backgroundImageView.alpha = .0;
for (UIView *view in self.backgroundImageView.subviews) {
NSLog(@" %@ %@", view.className, view);
}
[super show];

self.titleLabel.textColor = self.bodyTextLabel.textColor = [UIColor blackColor];
self.titleLabel.shadowOffset = self.bodyTextLabel.shadowOffset = CGSizeZero;

self.titleLabel.font = [UIFont iOS7SystemFontOfSize:16.0 weight:UI7FontWeightMedium];
self.bodyTextLabel.font = [UIFont iOS7SystemFontOfSize:16.0 weight:UI7FontWeightLight];

{
CGRect frame = self.strokeView.frame;
frame.origin.y = self.messageLabel.frame.origin.y + self.messageLabel.frame.size.height + 29.5f;
frame.origin.y = self.bodyTextLabel.frame.origin.y + self.bodyTextLabel.frame.size.height + 29.5f;
self.strokeView.frame = frame;
}
[self.buttons applyProcedureWithIndex:^(id obj, NSUInteger index) {
UI7Button *button = obj;
CGFloat width = self.frameView.frame.size.width / self.buttons.count;
CGRect frame = CGRectMake(width * index, self.strokeView.frame.origin.y + 1.0f, self.frameView.frame.size.width / self.buttons.count, 45.0);
button.frame = frame;
}];
{
CGRect frame = self.frameView.frame;
frame.size.height = self.strokeView.frame.origin.y + 46;
self.frameView.frame = frame;
}
{
self.frameView.center = self.center;

[window addSubview:self];
}
}

- (NSInteger)addButtonWithTitle:(NSString *)title {
UI7Button *button = [UI7Button buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(.0, .0, self.frameView.frame.size.width, 45.0);
self.frameView.frame = self.bounds;

button.frame = frame;
[self.frameView addSubview:button];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor iOS7ButtonTitleColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor iOS7ButtonTitleHighlightedColor] forState:UIControlStateHighlighted];
button.tag = self.buttons.count;
[button addTarget:self action:@selector(_buttonDidSelected:) forControlEvents:UIControlEventTouchUpInside];
for (UIAlertButton *button in self.buttons) {
button.titleLabel.font = [UIFont iOS7SystemFontOfSize:16.0 weight:UI7FontWeightLight];
[button setTitleColor:[UIColor iOS7ButtonTitleColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor iOS7ButtonTitleHighlightedColor] forState:UIControlStateHighlighted];
button.titleLabel.shadowOffset = CGSizeZero;
[button setBackgroundImage:nil forState:UIControlStateNormal];
[button setBackgroundImage:nil forState:UIControlStateHighlighted];

self.buttons = [self.buttons arrayByAddingObject:button];
return button.tag;
CGRect frame = button.frame;
frame.size.height = 45.0;
frame.origin.y = self.strokeView.frame.origin.y + 0.5f;
button.frame = frame;
}
}

- (void)_buttonDidSelected:(id)sender {
id<UIAlertViewDelegate> delegate = [self delegate];
[delegate alertView:(id)self willDismissWithButtonIndex:[sender tag]];
[delegate alertView:(id)self didDismissWithButtonIndex:[sender tag]];
[delegate alertView:(id)self didDismissWithButtonIndex:[sender tag]];
[self removeFromSuperview];
}
//- (NSInteger)addButtonWithTitle:(NSString *)title {
// [super addButtonWithTitle:title];
// UI7Button *button = [UI7Button buttonWithType:UIButtonTypeCustom];
// CGRect frame = CGRectMake(.0, .0, self.frameView.frame.size.width, 45.0);
//
// button.frame = frame;
// [self.frameView addSubview:button];
// [button setTitle:title forState:UIControlStateNormal];
// [button setTitleColor:[UIColor iOS7ButtonTitleColor] forState:UIControlStateNormal];
// [button setTitleColor:[UIColor iOS7ButtonTitleHighlightedColor] forState:UIControlStateHighlighted];
// button.tag = self.buttons.count;
// [button addTarget:self action:@selector(_buttonDidSelected:) forControlEvents:UIControlEventTouchUpInside];
//
// self.buttons = [self.buttons arrayByAddingObject:button];
// return button.tag;
//}

//- (void)_buttonDidSelected:(id)sender {
// id<UIAlertViewDelegate> delegate = [self delegate];
// [delegate alertView:(id)self willDismissWithButtonIndex:[sender tag]];
// [delegate alertView:(id)self didDismissWithButtonIndex:[sender tag]];
// [delegate alertView:(id)self didDismissWithButtonIndex:[sender tag]];
// [self removeFromSuperview];
//}

@end
2 changes: 1 addition & 1 deletion UI7Kit/UI7Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ FOUNDATION_EXTERN NSString *UI7FontWeightBold;

@interface UIImage (Images)

+ (id)blankImage;
+ (UIImage *)blankImage;
- (UIImageView *)view;

@end
Expand Down
2 changes: 1 addition & 1 deletion UI7Kit/UI7Utilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ + (UIFont *)iOS7SystemFontOfSize:(CGFloat)fontSize weight:(NSString *)weight {

@implementation UIImage (Images)

+ (id)blankImage {
+ (UIImage *)blankImage {
static UIImage *image = nil;
if (image == nil) {
image = [[UIImage imageNamed:@"UI7Blank"] retain];
Expand Down

0 comments on commit 9ae12af

Please sign in to comment.