.h
//
// FDAlertView.h
// FDAlertViewDemo
//
// Created by fergusding on 15/5/26.
// Copyright (c) 2015年 fergusding. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol YYAlertViewDelegate;
static NSString *const ALERTVIEW_HIDE_NOTIFICATION = @"YYAlertViewHideNotification"; // 点击遮罩消失
@interface YYAlertView : UIView
@property (strong, nonatomic) UIView *contentView;
@property (strong, nonatomic) UIImage *icon;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *message;
@property (strong, nonatomic) NSString *detailMessage;
@property (strong, nonatomic) NSString *currentInterface;
@property (weak, nonatomic) id<YYAlertViewDelegate> delegate;
@property (nonatomic, assign) BOOL enableTouchBackgroundHide; // 点击背景关闭
- (instancetype)initWithIcon:(UIImage *)icon message:(NSString *)message detailMessage:(NSString *)detailMessage delegate:(id<YYAlertViewDelegate>)delegate buttonTitles:(NSString *)buttonTitles, ... NS_REQUIRES_NIL_TERMINATION;
// Show the alert view in current window
- (void)show;
// Hide the alert view
- (void)hide;
- (void)setMessageAttributeColor:(UIColor *)attributeColor withRange:(NSRange)range;
// Set the color and font size of title, if color is nil, default is black. if fontsize is 0, default is 14
- (void)setTitleColor:(UIColor *)color fontSize:(CGFloat)size;
// Set the color and font size of message, if color is nil, default is black. if fontsize is 0, default is 12
- (void)setMessageColor:(UIColor *)color fontSize:(CGFloat)size;
// Set the color and font size of button at the index, if color is nil, default is black. if fontsize is 0, default is 16
- (void)setButtonTitleColor:(UIColor *)color fontSize:(CGFloat)size atIndex:(NSInteger)index;
@end
@protocol YYAlertViewDelegate <NSObject>
- (void)alertView:(YYAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end
.m
//
// FDAlertView.m
// FDAlertViewDemo
//
// Created by fergusding on 15/5/26.
// Copyright (c) 2015年 fergusding. All rights reserved.
//
#import "YYAlertView.h"
#define TITLE_FONT_SIZE 14
#define MESSAGE_FONT_SIZE 14
#define DETAIL_MESSAGE_FONT_SIZE 12
#define BUTTON_FONT_SIZE 16
#define MARGIN_TOP 30
#define MARGIN_LEFT_LARGE 30
#define MARGIN_LEFT_SMALL 15
#define MARGIN_RIGHT_LARGE 30
#define MARGIN_RIGHT_SMALL 15
#define SPACE_LARGE 20
#define SPACE_SMALL 5
#define MESSAGE_LINE_SPACE 5
#define RGBA(R, G, B, A) [UIColor colorWithRed:R / 255.0 green:G / 255.0 blue:B / 255.0 alpha:A]
@interface YYAlertView ()
@property (strong, nonatomic) UIView *backgroundView;
@property (strong, nonatomic) UIView *titleView;
@property (strong, nonatomic) UIImageView *iconImageView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *messageLabel;
@property (strong, nonatomic) UILabel *detailMessageLabel;
@property (strong, nonatomic) NSMutableArray *buttonArray;
@property (strong, nonatomic) NSMutableArray *buttonTitleArray;
@end
CGFloat contentViewWidth;
CGFloat contentViewHeight;
@implementation YYAlertView
- (instancetype)init {
if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
self.backgroundColor = [UIColor clearColor];
_backgroundView = [[UIView alloc] initWithFrame:self.frame];
_backgroundView.backgroundColor = [UIColor blackColor];
[self addSubview:_backgroundView];
}
return self;
}
- (void)dismiss {
[self hide];
[[NSNotificationCenter defaultCenter] postNotificationName:ALERTVIEW_HIDE_NOTIFICATION object:nil];
}
- (instancetype)initWithIcon:(UIImage *)icon message:(NSString *)message detailMessage:(NSString *)detailMessage delegate:(id<YYAlertViewDelegate>)delegate buttonTitles:(NSString *)buttonTitles, ... {
if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
_icon = icon;
// _title = title;
_message = message;
_detailMessage = detailMessage;
_delegate = delegate;
_buttonArray = [NSMutableArray array];
_buttonTitleArray = [NSMutableArray array];
va_list args;
va_start(args, buttonTitles);
if (buttonTitles)
{
[_buttonTitleArray addObject:buttonTitles];
while (1)
{
NSString * otherButtonTitle = va_arg(args, NSString *);
if(otherButtonTitle == nil) {
break;
} else {
[_buttonTitleArray addObject:otherButtonTitle];
}
}
}
va_end(args);
self.backgroundColor = [UIColor clearColor];
_backgroundView = [[UIView alloc] initWithFrame:self.frame];
_backgroundView.backgroundColor = [UIColor blackColor];
[self addSubview:_backgroundView];
[self initContentView];
}
return self;
}
- (void)setEnableTouchBackgroundHide:(BOOL)enableTouchBackgroundHide {
_enableTouchBackgroundHide = enableTouchBackgroundHide;
if (enableTouchBackgroundHide) {
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
[_backgroundView addGestureRecognizer:gesture];
}
}
- (void)setMessageAttributeColor:(UIColor *)attributeColor withRange:(NSRange)range {
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithAttributedString:_messageLabel.attributedText];
[attributeStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
self.messageLabel.attributedText = attributeStr;
}
- (void)setContentView:(UIView *)contentView {
_contentView = contentView;
_contentView.center = self.center;
[self addSubview:_contentView];
}
- (void)setTitle:(NSString *)title {
_title = title;
[self initContentView];
}
- (void)setIcon:(UIImage *)icon {
_icon = icon;
[self initContentView];
}
- (void)setMessage:(NSString *)message {
_message = message;
[self initContentView];
}
- (void)setDetailMessage:(NSString *)detailMessage {
_detailMessage = detailMessage;
[self initContentView];
}
// Init the content of content view
- (void)initContentView {
contentViewWidth = 280 * self.frame.size.width / 320;
contentViewHeight = MARGIN_TOP;
_contentView = [[UIView alloc] init];
_contentView.backgroundColor = [UIColor whiteColor];
_contentView.layer.cornerRadius = 5.0;
_contentView.layer.masksToBounds = YES;
[self initTitleAndIcon];
[self initMessage];
[self initDetailMessage];
[self initAllButtons];
_contentView.frame = CGRectMake(0, 0, contentViewWidth, contentViewHeight);
_contentView.center = self.center;
[self addSubview:_contentView];
}
//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//{
// UITouch *touch = [touches anyObject];
// CGPoint touchPoint = [touch locationInView:self];
// if (touchPoint.x<self.x || touchPoint.x>self.w+self.x || touchPoint.y<self.y || touchPoint.y > self.h+self.y) {
// self.hidden = YES;
// [self removeFromSuperview];
// //(Screen_width*0.05, Screen_height*0.22, Screen_width*0.9, Screen_height*0.56)
// if ([self.currentInterface isEqualToString:@"首页"]) {
// [[NSNotificationCenter defaultCenter] postNotificationName:@"侧滑打开" object:self userInfo:nil];
// }
// }
//}
// Init the title and icon
- (void)initTitleAndIcon {
_titleView = [[UIView alloc] init];
if (_icon != nil) {
_iconImageView = [[UIImageView alloc] init];
_iconImageView.image = _icon;
_iconImageView.frame = CGRectMake(0, 0, 30, 30);
[_titleView addSubview:_iconImageView];
}
CGSize titleSize = [self getTitleSize];
if (_title != nil && ![_title isEqualToString:@""]) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.text = _title;
_titleLabel.textColor = RGBA(28, 28, 28, 1.0);
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.font = [UIFont systemFontOfSize:TITLE_FONT_SIZE];
_titleLabel.numberOfLines = 0;
_titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
_titleLabel.frame = CGRectMake(_iconImageView.frame.origin.x + _iconImageView.frame.size.width + SPACE_SMALL, 1, titleSize.width, titleSize.height);
[_titleView addSubview:_titleLabel];
}
_titleView.frame = CGRectMake(0, MARGIN_TOP, _iconImageView.frame.size.width + SPACE_SMALL + titleSize.width, MAX(_iconImageView.frame.size.height, titleSize.height));
_titleView.center = CGPointMake(contentViewWidth / 2, MARGIN_TOP + _titleView.frame.size.height / 2);
[_contentView addSubview:_titleView];
contentViewHeight += _titleView.frame.size.height;
}
// Init the message
- (void)initMessage {
if (_message != nil) {
_messageLabel = [[UILabel alloc] init];
_messageLabel.text = _message;
_messageLabel.textColor = RGBA(51, 51, 51, 1);
_messageLabel.numberOfLines = 0;
_messageLabel.font = [UIFont systemFontOfSize:MESSAGE_FONT_SIZE];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineSpacing = MESSAGE_LINE_SPACE;
NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraphStyle};
_messageLabel.attributedText = [[NSAttributedString alloc]initWithString:_message attributes:attributes];
_messageLabel.textAlignment = NSTextAlignmentCenter;
CGSize messageSize = [self getMessageSize];
_messageLabel.frame = CGRectMake(MARGIN_LEFT_LARGE, _titleView.frame.origin.y + _titleView.frame.size.height + SPACE_LARGE, MAX(contentViewWidth - MARGIN_LEFT_LARGE - MARGIN_RIGHT_LARGE, messageSize.width), messageSize.height);
[_contentView addSubview:_messageLabel];
contentViewHeight += SPACE_LARGE + _messageLabel.frame.size.height;
}
}
- (void)initDetailMessage {
if (_detailMessage != nil) {
_detailMessageLabel = [[UILabel alloc] init];
_detailMessageLabel.text = _detailMessage;
_detailMessageLabel.textColor = YYRedColor;
_detailMessageLabel.textAlignment = NSTextAlignmentCenter;
_detailMessageLabel.numberOfLines = 0;
_detailMessageLabel.font = [UIFont systemFontOfSize:DETAIL_MESSAGE_FONT_SIZE];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineSpacing = MESSAGE_LINE_SPACE;
NSDictionary *attributes = @{NSParagraphStyleAttributeName:paragraphStyle};
_messageLabel.attributedText = [[NSAttributedString alloc]initWithString:_message attributes:attributes];
_messageLabel.textAlignment = NSTextAlignmentCenter;
CGSize messageSize = [self getDetailMessageSize];
_detailMessageLabel.frame = CGRectMake(MARGIN_LEFT_LARGE, CGRectGetMaxY(_messageLabel.frame) + SPACE_SMALL, MAX(contentViewWidth - MARGIN_LEFT_LARGE - MARGIN_RIGHT_LARGE, messageSize.width), messageSize.height);
[_contentView addSubview:_detailMessageLabel];
contentViewHeight += SPACE_SMALL + _detailMessageLabel.frame.size.height;
}
}
// Init all the buttons according to button titles
- (void)initAllButtons {
if (_buttonTitleArray.count > 0) {
contentViewHeight += ( _detailMessage ? SPACE_SMALL : SPACE_LARGE) + 61;
UIView *horizonSperatorView = nil;
if (_detailMessage) {
horizonSperatorView = [[UIView alloc] initWithFrame:CGRectMake(0, _detailMessageLabel.frame.origin.y + _detailMessageLabel.frame.size.height + SPACE_SMALL, contentViewWidth, 1)];
}else {
horizonSperatorView = [[UIView alloc] initWithFrame:CGRectMake(0, _messageLabel.frame.origin.y + _messageLabel.frame.size.height + SPACE_LARGE, contentViewWidth, 1)];
}
horizonSperatorView.backgroundColor = RGBA(218, 218, 222, 1.0);
[_contentView addSubview:horizonSperatorView];
CGFloat buttonWidth = contentViewWidth / _buttonTitleArray.count;
for (NSString *buttonTitle in _buttonTitleArray) {
NSInteger index = [_buttonTitleArray indexOfObject:buttonTitle];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(index * buttonWidth, horizonSperatorView.frame.origin.y + horizonSperatorView.frame.size.height, buttonWidth, 60)];
button.titleLabel.font = [UIFont systemFontOfSize:BUTTON_FONT_SIZE];
[button setTitle:buttonTitle forState:UIControlStateNormal];
[button setTitleColor:RGBA(102, 102, 102, 1.0) forState:UIControlStateNormal];
if ([buttonTitle isEqualToString:@"删除"]) {
[button setTitleColor:YYRedColor forState:UIControlStateNormal];
}else if ([buttonTitle isEqualToString:@"充值"]) {
[button setTitleColor:YYOrangeColor forState:UIControlStateNormal];
}else if ([buttonTitle isEqualToString:@"邀请"]) {
[button setTitleColor:YYBlueColor forState:UIControlStateNormal];
}else if ([buttonTitle isEqualToString:@"确定"]) {
[button setTitleColor:YYBlueColor forState:UIControlStateNormal];
}
[button addTarget:self action:@selector(buttonWithPressed:) forControlEvents:UIControlEventTouchUpInside];
[_buttonArray addObject:button];
[_contentView addSubview:button];
if (index < _buttonTitleArray.count - 1) {
UIView *verticalSeperatorView = [[UIView alloc] initWithFrame:CGRectMake(button.frame.origin.x + button.frame.size.width, button.frame.origin.y, 1, button.frame.size.height)];
verticalSeperatorView.backgroundColor = RGBA(182, 182, 182, 1);
[_contentView addSubview:verticalSeperatorView];
}
}
}
}
// Get the size fo title
- (CGSize)getTitleSize {
UIFont *font = [UIFont systemFontOfSize:TITLE_FONT_SIZE];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize size = [_title boundingRectWithSize:CGSizeMake(contentViewWidth - (MARGIN_LEFT_SMALL + MARGIN_RIGHT_SMALL + _iconImageView.frame.size.width + SPACE_SMALL), 2000)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes context:nil].size;
size.width = ceil(size.width);
size.height = ceil(size.height);
return size;
}
// Get the size of message
- (CGSize)getMessageSize {
UIFont *font = [UIFont systemFontOfSize:MESSAGE_FONT_SIZE];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = MESSAGE_LINE_SPACE;
NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize size = [_message boundingRectWithSize:CGSizeMake(contentViewWidth - (MARGIN_LEFT_LARGE + MARGIN_RIGHT_LARGE), 2000)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes context:nil].size;
size.width = ceil(size.width);
size.height = ceil(size.height);
return size;
}
- (CGSize)getDetailMessageSize {
UIFont *font = [UIFont systemFontOfSize:DETAIL_MESSAGE_FONT_SIZE];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = MESSAGE_LINE_SPACE;
NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize size = [_detailMessage boundingRectWithSize:CGSizeMake(contentViewWidth - (MARGIN_LEFT_LARGE + MARGIN_RIGHT_LARGE), 2000)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes context:nil].size;
size.width = ceil(size.width);
size.height = ceil(size.height);
return size;
}
- (void)buttonWithPressed:(UIButton *)button {
if (_delegate && [_delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
NSInteger index = [_buttonTitleArray indexOfObject:button.titleLabel.text];
[_delegate alertView:self clickedButtonAtIndex:index];
}
[self hide];
}
- (void)show {
// UIWindow *window = [[UIApplication sharedApplication] keyWindow];
// NSArray *windowViews = [window subviews];
// if(windowViews && [windowViews count] > 0){
// // UIView *subView = [windowViews objectAtIndex:[windowViews count]-1];
// UIView *subView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
// for(UIView *aSubView in subView.subviews)
// {
// [aSubView.layer removeAllAnimations];
// }
// [subView addSubview:self];
// [self showBackground];
// [self showAlertAnimation];
// }
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self showBackground];
[self showAlertAnimation];
}
- (void)hide {
_contentView.hidden = YES;
[self hideAlertAnimation];
[self removeFromSuperview];
}
- (void)setTitleColor:(UIColor *)color fontSize:(CGFloat)size {
if (color != nil) {
_titleLabel.textColor = color;
}
if (size > 0) {
_titleLabel.font = [UIFont systemFontOfSize:size];
}
}
- (void)setMessageColor:(UIColor *)color fontSize:(CGFloat)size {
if (color != nil) {
_messageLabel.textColor = color;
}
if (size > 0) {
_messageLabel.font = [UIFont systemFontOfSize:size];
}
}
- (void)setButtonTitleColor:(UIColor *)color fontSize:(CGFloat)size atIndex:(NSInteger)index {
UIButton *button = _buttonArray[index];
if (color != nil) {
[button setTitleColor:color forState:UIControlStateNormal];
}
if (size > 0) {
button.titleLabel.font = [UIFont systemFontOfSize:size];
}
}
- (void)showBackground
{
_backgroundView.alpha = 0;
[UIView beginAnimations:@"fadeIn" context:nil];
[UIView setAnimationDuration:0.35];
_backgroundView.alpha = 0.6;
[UIView commitAnimations];
}
-(void)showAlertAnimation
{
CAKeyframeAnimation * animation;
animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.duration = 0.30;
animation.removedOnCompletion = YES;
animation.fillMode = kCAFillModeForwards;
NSMutableArray *values = [NSMutableArray array];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
animation.values = values;
[_contentView.layer addAnimation:animation forKey:nil];
}
- (void)hideAlertAnimation {
[UIView beginAnimations:@"fadeIn" context:nil];
[UIView setAnimationDuration:0.35];
_backgroundView.alpha = 0.0;
[UIView commitAnimations];
}
@end
网友评论