Skip to content

Commit

Permalink
Merge pull request #2 from Legoless/master
Browse files Browse the repository at this point in the history
Swift support, improved styling support
  • Loading branch information
kubatruhlar committed Oct 21, 2016
2 parents 6e098c3 + 6eb75c6 commit a9fe458
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 47 deletions.
Binary file added .DS_Store
Binary file not shown.
Expand Up @@ -177,7 +177,7 @@
3BD1B19E1B2CBE7600621F68 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0630;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = "Jakub Truhlar";
TargetAttributes = {
3BD1B1A51B2CBE7600621F68 = {
Expand Down Expand Up @@ -291,14 +291,17 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -334,8 +337,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand Down Expand Up @@ -364,6 +369,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = "JTAlertView Example/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.jakubtruhlar.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -374,6 +380,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = "JTAlertView Example/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.jakubtruhlar.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand All @@ -392,6 +399,7 @@
);
INFOPLIST_FILE = "JTAlertView ExampleTests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.jakubtruhlar.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/JTAlertView Example.app/JTAlertView Example";
};
Expand All @@ -407,6 +415,7 @@
);
INFOPLIST_FILE = "JTAlertView ExampleTests/Info.plist";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.jakubtruhlar.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/JTAlertView Example.app/JTAlertView Example";
};
Expand Down
2 changes: 1 addition & 1 deletion JTAlertView Example/JTAlertView Example/Info.plist
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.jakubtruhlar.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
2 changes: 1 addition & 1 deletion JTAlertView Example/JTAlertView ExampleTests/Info.plist
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.jakubtruhlar.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
22 changes: 17 additions & 5 deletions JTAlertView/JTAlertView.h
Expand Up @@ -7,6 +7,8 @@

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface JTAlertView : UIView

/** In Cancel and Destructive case JTAlertView will try to use bold version of your font (Must be in format FontName-Bold) */
Expand All @@ -16,6 +18,8 @@ typedef NS_ENUM(NSInteger, JTAlertViewStyle) {
JTAlertViewStyleDestructive
};

typedef void (^JTAlertViewStyling)(UIButton *btn);

// Public properties
/** Size of the alertView. Default is 240.0, 290.0. */
@property (nonatomic, assign) CGSize size;
Expand All @@ -29,6 +33,9 @@ typedef NS_ENUM(NSInteger, JTAlertViewStyle) {
/** Image overlay. Default overlay is gray with half alpha. */
@property (nonatomic, strong) UIColor *overlayColor;

/** Button separator color. Default is 97% white. */
@property (nonatomic, strong) UIColor *separatorColor;

/** Font applied on title and alertView buttons. AlertView buttons will ignore this font's size but not the style (In case you want to setup custom font for your buttons, use font parameter in method instead). Default is Helvetica Neue Medium with 21.0 size. */
@property (nonatomic, strong) UIFont *font;

Expand All @@ -42,20 +49,23 @@ typedef NS_ENUM(NSInteger, JTAlertViewStyle) {
@property (nonatomic, assign, getter=isBackgroundShadow) bool backgroundShadow;

// Initializers
+ (instancetype)alertWithTitle:(NSString *)titleText andImage:(UIImage *)image;
- (instancetype)initWithTitle:(NSString *)titleText andImage:(UIImage *)image;
+ (instancetype)alertWithTitle:(NSString * _Nullable)titleText andImage:(UIImage * _Nullable)image;
- (instancetype)initWithTitle:(NSString * _Nullable)titleText andImage:(UIImage * _Nullable)image;

// Buttons
- (void)addButtonWithTitle:(NSString *)titleText action:(void (^)(JTAlertView *alertView))action;
- (void)addButtonWithTitle:(NSString *)titleText style:(JTAlertViewStyle)style action:(void (^)(JTAlertView *alertView))action;
- (void)addButtonWithTitle:(NSString *)titleText style:(JTAlertViewStyle)style forControlEvents:(UIControlEvents)controlEvents action:(void (^)(JTAlertView *alertView))action;
- (void)addButtonWithTitle:(NSString *)titleText font:(UIFont *)font style:(JTAlertViewStyle)style forControlEvents:(UIControlEvents)controlEvents action:(void (^)(JTAlertView *alertView))action;
- (void)addButtonWithTitle:(NSString *)titleText font:(UIFont * _Nullable)font style:(JTAlertViewStyle)style forControlEvents:(UIControlEvents)controlEvents action:(void (^)(JTAlertView *alertView))action;

/** Designated method to adda a button. Provide a styling block to update custom styling to provided button, overriding default JTAlertViewStyle parameter and font. */
- (void)addButtonWithTitle:(NSString *)titleText styling:(JTAlertViewStyling _Nullable)styling forControlEvents:(UIControlEvents)controlEvents action:(void (^)(JTAlertView *alertView))action;

// Displaying
- (void)show;
- (void)showInSuperview:(UIView *)superView withCompletion:(void (^)())completion animated:(bool)animated;
- (void)showInSuperview:(UIView *)superView withCompletion:(void (^ _Nullable)())completion animated:(bool)animated;
- (void)hide;
- (void)hideWithCompletion:(void (^)())completion animated:(bool)animated;
- (void)hideWithCompletion:( void (^ _Nullable )())completion animated:(bool)animated;
- (void)hideWithDelay:(NSTimeInterval)delay animated:(bool)animated;

@end
Expand All @@ -69,3 +79,5 @@ typedef void (^ActionBlock)(JTAlertView *alertView);
- (void)handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock)action;

@end

NS_ASSUME_NONNULL_END
88 changes: 49 additions & 39 deletions JTAlertView/JTAlertView.m
Expand Up @@ -76,12 +76,10 @@ + (instancetype)alertWithTitle:(NSString *)titleText andImage:(UIImage *)image {

#pragma mark - Buttons handle
- (void)addButtonWithTitle:(NSString *)titleText action:(void (^)(JTAlertView *alertView))action {

[self addButtonWithTitle:titleText style:JTAlertViewStyleDefault action:action];
}

- (void)addButtonWithTitle:(NSString *)titleText style:(JTAlertViewStyle)style action:(void (^)(JTAlertView *alertView))action {

[self addButtonWithTitle:titleText style:style forControlEvents:UIControlEventTouchUpInside action:action];
}

Expand All @@ -90,45 +88,57 @@ - (void)addButtonWithTitle:(NSString *)titleText style:(JTAlertViewStyle)style f
}

- (void)addButtonWithTitle:(NSString *)titleText font:(UIFont *)font style:(JTAlertViewStyle)style forControlEvents:(UIControlEvents)controlEvents action:(void (^)(JTAlertView *alertView))action {
JTAlertViewStyling styling = ^(UIButton *btn) {
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor whiteColor]];
if (!font) {
switch (style) {
case JTAlertViewStyleDefault:
btn.titleLabel.font = [_font fontWithSize:kBtnFontSize];
break;
case JTAlertViewStyleCancel:
btn.titleLabel.font = [self boldForFont:_font withSize:kBtnFontSize] ? [self boldForFont:_font withSize:kBtnFontSize] : [_font fontWithSize:kBtnFontSize];
break;
case JTAlertViewStyleDestructive:
btn.titleLabel.font = [self boldForFont:_font withSize:kBtnFontSize] ? [self boldForFont:_font withSize:kBtnFontSize] : [_font fontWithSize:kBtnFontSize];

[btn setTitleColor:[UIColor colorWithRed:0.906 green:0.298 blue:0.235 alpha:1] forState:UIControlStateNormal];
break;
default:
break;
}
} else {
switch (style) {
case JTAlertViewStyleDefault:
btn.titleLabel.font = font;
break;
case JTAlertViewStyleCancel:
btn.titleLabel.font = [self boldForFont:font withSize:font.pointSize] ? [self boldForFont:font withSize:font.pointSize] : font;
break;
case JTAlertViewStyleDestructive:
btn.titleLabel.font = [self boldForFont:font withSize:font.pointSize] ? [self boldForFont:font withSize:font.pointSize] : font;

[btn setTitleColor:[UIColor colorWithRed:0.906 green:0.298 blue:0.235 alpha:1] forState:UIControlStateNormal];
break;
default:
break;
}
}
};

[self addButtonWithTitle:titleText styling:styling forControlEvents:controlEvents action:action];
}

- (void)addButtonWithTitle:(NSString *)titleText styling:(JTAlertViewStyling)styling forControlEvents:(UIControlEvents)controlEvents action:(void (^)(JTAlertView *alertView))action {
UIBlockButton *btn = [UIBlockButton buttonWithType:UIButtonTypeSystem];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor whiteColor]];
[btn setTitle:titleText forState:UIControlStateNormal];
[btn handleControlEvent:controlEvents withBlock:action];

if (!font) {
switch (style) {
case JTAlertViewStyleDefault:
btn.titleLabel.font = [_font fontWithSize:kBtnFontSize];
break;
case JTAlertViewStyleCancel:
btn.titleLabel.font = [self boldForFont:_font withSize:kBtnFontSize] ? [self boldForFont:_font withSize:kBtnFontSize] : [_font fontWithSize:kBtnFontSize];
break;
case JTAlertViewStyleDestructive:
btn.titleLabel.font = [self boldForFont:_font withSize:kBtnFontSize] ? [self boldForFont:_font withSize:kBtnFontSize] : [_font fontWithSize:kBtnFontSize];
[btn setTitleColor:[UIColor colorWithRed:0.906 green:0.298 blue:0.235 alpha:1] forState:UIControlStateNormal];
break;
default:
break;
}
} else {
switch (style) {
case JTAlertViewStyleDefault:
btn.titleLabel.font = font;
break;
case JTAlertViewStyleCancel:
btn.titleLabel.font = [self boldForFont:font withSize:font.pointSize] ? [self boldForFont:font withSize:font.pointSize] : font;
break;
case JTAlertViewStyleDestructive:
btn.titleLabel.font = [self boldForFont:font withSize:font.pointSize] ? [self boldForFont:font withSize:font.pointSize] : font;
[btn setTitleColor:[UIColor colorWithRed:0.906 green:0.298 blue:0.235 alpha:1] forState:UIControlStateNormal];
break;
default:
break;
}
if (styling != nil) {
styling(btn);
}

[btn setTitle:titleText forState:UIControlStateNormal];
[btn handleControlEvent:controlEvents withBlock:action];

[self.btns addObject:btn];

[self layoutBtns];
Expand Down Expand Up @@ -165,7 +175,7 @@ - (void)layoutBtns {

- (void)addSeparatorInView:(UIView *)superview withTag:(NSInteger)tag andFrame:(CGRect)frame {
UIView *separator = [[UIView alloc] initWithFrame:frame];
separator.backgroundColor = [UIColor colorWithWhite:kSeparatorColorValue alpha:1.0];
separator.backgroundColor = self.separatorColor;
separator.tag = tag;
[superview insertSubview:separator atIndex:2];
}
Expand Down Expand Up @@ -243,7 +253,6 @@ - (void)show {
}

- (void)showInSuperview:(UIView *)superView withCompletion:(void (^)())completion animated:(bool)animated {

if (self.btns.count == 0) {
[self applyAppearanceConsideringButtons:false];
} else {
Expand Down Expand Up @@ -359,6 +368,7 @@ - (void)defaultSetup {
_titleColor = [UIColor whiteColor];
_titleShadow = true;
_backgroundShadow = false;
_separatorColor = [UIColor colorWithWhite:kSeparatorColorValue alpha:1.0];
}

- (void)noAlpha {
Expand Down Expand Up @@ -433,4 +443,4 @@ - (void)callActionBlock {
_actionBlock((JTAlertView *)self.superview);
}

@end
@end

0 comments on commit a9fe458

Please sign in to comment.