CustomAlertView.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // CustomAlertView.m
  3. // NoteLedge for Mac
  4. //
  5. // Created by LiuZhiXun on 16/4/27.
  6. // Copyright © 2016年 kdanmobile. All rights reserved.
  7. //
  8. #import "CustomAlertView.h"
  9. #import <Quartz/Quartz.h>
  10. @implementation CustomAlertView
  11. - (void)drawRect:(NSRect)dirtyRect {
  12. [super drawRect:dirtyRect];
  13. // Drawing code here.
  14. }
  15. + (CustomAlertView *)alertViewWithMessage:(NSString *)message
  16. fromView:(NSView *)supverView
  17. withStyle:(KMCustomAlertStyle)alertStyle
  18. backgroundColor:(NSColor *)color {
  19. if (alertStyle != KMCustomAlertStyle_Black && alertStyle != KMCustomAlertStyle_Blue) {
  20. return nil;
  21. }
  22. CustomAlertView *view = [[CustomAlertView alloc] init];
  23. NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
  24. [style setLineBreakMode:NSLineBreakByWordWrapping];
  25. CGFloat fontSize = 0;
  26. CGSize offsetSize = CGSizeZero;
  27. if (alertStyle == KMCustomAlertStyle_Black) {
  28. fontSize = 16;
  29. } else {
  30. fontSize = 12;
  31. }
  32. NSDictionary * attribute = @{NSFontAttributeName:[NSFont systemFontOfSize:fontSize],
  33. NSParagraphStyleAttributeName:style};
  34. NSSize size = [message boundingRectWithSize:NSMakeSize(MIN(supverView.frame.size.width-80,500), supverView.frame.size.height-40) options:(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attribute].size;
  35. size.width = ceil(size.width);
  36. size.height = ceil(size.height);
  37. if (alertStyle == KMCustomAlertStyle_Black) {
  38. offsetSize = NSMakeSize(size.width+60, size.height+30);
  39. view.frame = NSMakeRect((supverView.frame.size.width-offsetSize.width)/2,
  40. (supverView.frame.size.height-offsetSize.height)/2,
  41. offsetSize.width,offsetSize.height);
  42. } else {
  43. offsetSize = NSMakeSize(size.width + size.height + 14 + 10, size.height+14);
  44. view.frame = NSMakeRect((supverView.frame.size.width-offsetSize.width)/2,
  45. (supverView.frame.size.height-offsetSize.height) - 10,
  46. offsetSize.width,offsetSize.height);
  47. }
  48. [supverView addSubview:view];
  49. view.wantsLayer = YES;
  50. NSTextField *messageLabel = nil;
  51. if (alertStyle == KMCustomAlertStyle_Black) {
  52. view.layer.cornerRadius = 8;
  53. if (color) {
  54. view.layer.backgroundColor = color.CGColor;
  55. } else {
  56. view.layer.backgroundColor = [[NSColor blackColor] colorWithAlphaComponent:0.7].CGColor;
  57. }
  58. messageLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(15, 15, size.width+30., size.height)];
  59. } else {
  60. view.layer.cornerRadius = view.frame.size.height/2;
  61. if (color) {
  62. view.layer.backgroundColor = color.CGColor;
  63. } else {
  64. view.layer.backgroundColor = [NSColor colorWithRed:78/255.0 green:163/255.0 blue:255/255.0 alpha:1].CGColor;
  65. }
  66. messageLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(view.frame.size.height/2, 7, size.width + 10, size.height)];
  67. }
  68. messageLabel.font = [NSFont systemFontOfSize:fontSize];
  69. messageLabel.textColor = [NSColor whiteColor];
  70. messageLabel.backgroundColor = [NSColor clearColor];
  71. messageLabel.bordered = NO;
  72. messageLabel.editable = NO;
  73. if ([messageLabel respondsToSelector:@selector(setLineBreakMode:)]) {
  74. messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
  75. }
  76. messageLabel.alignment = NSTextAlignmentCenter;
  77. messageLabel.stringValue = message;
  78. [view addSubview:messageLabel];
  79. [view alphaAnimationFrom:0
  80. to:1.
  81. duration:0.3
  82. finishedBlock:^
  83. {
  84. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  85. [view alphaAnimationFrom:1.0 to:0. duration:0.3
  86. finishedBlock:^{
  87. [view removeFromSuperview];
  88. }];
  89. });
  90. }];
  91. return view;
  92. }
  93. + (CustomAlertView *)alertViewWithMessage:(NSString *)message fromView:(NSView *)supverView withStyle:(KMCustomAlertStyle)alertStyle {
  94. return [CustomAlertView alertViewWithMessage:message fromView:supverView withStyle:alertStyle backgroundColor:nil];
  95. }
  96. - (void)alphaAnimationFrom:(float)fromValue
  97. to:(float)toValue
  98. duration:(float)duration
  99. finishedBlock:(void (^)(void))block {
  100. CABasicAnimation *flash = [CABasicAnimation animationWithKeyPath:@"opacity"];
  101. flash.fromValue = [NSNumber numberWithFloat:fromValue];
  102. flash.toValue = [NSNumber numberWithFloat:toValue];
  103. flash.duration = 0.5; // 1 second
  104. [self.layer addAnimation:flash forKey:@"flashAnimation"];
  105. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  106. block();
  107. });
  108. }
  109. @end