// // CustomAlertView.m // NoteLedge for Mac // // Created by LiuZhiXun on 16/4/27. // Copyright © 2016年 kdanmobile. All rights reserved. // #import "CustomAlertView.h" #import @implementation CustomAlertView - (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; // Drawing code here. } + (CustomAlertView *)alertViewWithMessage:(NSString *)message fromView:(NSView *)supverView withStyle:(KMCustomAlertStyle)alertStyle backgroundColor:(NSColor *)color { if (alertStyle != KMCustomAlertStyle_Black && alertStyle != KMCustomAlertStyle_Blue) { return nil; } CustomAlertView *view = [[CustomAlertView alloc] init]; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setLineBreakMode:NSLineBreakByWordWrapping]; CGFloat fontSize = 0; CGSize offsetSize = CGSizeZero; if (alertStyle == KMCustomAlertStyle_Black) { fontSize = 16; } else { fontSize = 12; } NSDictionary * attribute = @{NSFontAttributeName:[NSFont systemFontOfSize:fontSize], NSParagraphStyleAttributeName:style}; NSSize size = [message boundingRectWithSize:NSMakeSize(MIN(supverView.frame.size.width-80,500), supverView.frame.size.height-40) options:(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attribute].size; size.width = ceil(size.width); size.height = ceil(size.height); if (alertStyle == KMCustomAlertStyle_Black) { offsetSize = NSMakeSize(size.width+60, size.height+30); view.frame = NSMakeRect((supverView.frame.size.width-offsetSize.width)/2, (supverView.frame.size.height-offsetSize.height)/2, offsetSize.width,offsetSize.height); } else { offsetSize = NSMakeSize(size.width + size.height + 14 + 10, size.height+14); view.frame = NSMakeRect((supverView.frame.size.width-offsetSize.width)/2, (supverView.frame.size.height-offsetSize.height) - 10, offsetSize.width,offsetSize.height); } [supverView addSubview:view]; view.wantsLayer = YES; NSTextField *messageLabel = nil; if (alertStyle == KMCustomAlertStyle_Black) { view.layer.cornerRadius = 8; if (color) { view.layer.backgroundColor = color.CGColor; } else { view.layer.backgroundColor = [[NSColor blackColor] colorWithAlphaComponent:0.7].CGColor; } messageLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(15, 15, size.width+30., size.height)]; } else { view.layer.cornerRadius = view.frame.size.height/2; if (color) { view.layer.backgroundColor = color.CGColor; } else { view.layer.backgroundColor = [NSColor colorWithRed:78/255.0 green:163/255.0 blue:255/255.0 alpha:1].CGColor; } messageLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(view.frame.size.height/2, 7, size.width + 10, size.height)]; } messageLabel.font = [NSFont systemFontOfSize:fontSize]; messageLabel.textColor = [NSColor whiteColor]; messageLabel.backgroundColor = [NSColor clearColor]; messageLabel.bordered = NO; messageLabel.editable = NO; if ([messageLabel respondsToSelector:@selector(setLineBreakMode:)]) { messageLabel.lineBreakMode = NSLineBreakByWordWrapping; } messageLabel.alignment = NSTextAlignmentCenter; messageLabel.stringValue = message; [view addSubview:messageLabel]; [view alphaAnimationFrom:0 to:1. duration:0.3 finishedBlock:^ { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [view alphaAnimationFrom:1.0 to:0. duration:0.3 finishedBlock:^{ [view removeFromSuperview]; }]; }); }]; return view; } + (CustomAlertView *)alertViewWithMessage:(NSString *)message fromView:(NSView *)supverView withStyle:(KMCustomAlertStyle)alertStyle { return [CustomAlertView alertViewWithMessage:message fromView:supverView withStyle:alertStyle backgroundColor:nil]; } - (void)alphaAnimationFrom:(float)fromValue to:(float)toValue duration:(float)duration finishedBlock:(void (^)(void))block { CABasicAnimation *flash = [CABasicAnimation animationWithKeyPath:@"opacity"]; flash.fromValue = [NSNumber numberWithFloat:fromValue]; flash.toValue = [NSNumber numberWithFloat:toValue]; flash.duration = 0.5; // 1 second [self.layer addAnimation:flash forKey:@"flashAnimation"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ block(); }); } @end