ZDChatroomAlertView.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // ZDChatroomAlertView.m
  3. // Zhidao
  4. //
  5. // Created by zhuchao on 13-10-9.
  6. // Copyright (c) 2013年 Baidu. All rights reserved.
  7. //
  8. #import "ZDChatroomAlertView.h"
  9. @interface ZDChatroomAlertView ()
  10. + (ZDChatroomAlertView *)sharedAlertView;
  11. @end
  12. @implementation ZDChatroomAlertView
  13. + (ZDChatroomAlertView *)sharedAlertView
  14. {
  15. static ZDChatroomAlertView *_sharedAlertView = nil;
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. _sharedAlertView = [[ZDChatroomAlertView alloc] initWithFrame:CGRectZero];
  19. });
  20. return _sharedAlertView;
  21. }
  22. - (void)showInView:(UIView *)aView
  23. {
  24. [self setFrame:aView.bounds];
  25. [aView addSubview:self];
  26. [self setNeedsLayout];
  27. }
  28. - (void)hideView
  29. {
  30. @synchronized(self)
  31. {
  32. [self removeAlertView];
  33. }
  34. }
  35. - (void)removeAlertView
  36. {
  37. [self removeFromSuperview];
  38. }
  39. - (id)initWithFrame:(CGRect)frame
  40. {
  41. self = [super initWithFrame:frame];
  42. if (self) {
  43. // Initialization code
  44. [self setBackgroundColor:[UIColor clearColor]];
  45. self.contentMode=UIViewContentModeRedraw;
  46. _rectangleWidth = 145.0f;
  47. _rectangleHeight = 145.0f;
  48. _cornerRadio = 10.0f;
  49. _blackAlpha = 0.4f;
  50. // _rectangleWidth = 0.0f;
  51. // _rectangleHeight = 0.0f;
  52. // _cornerRadio = 0.0f;
  53. // _blackAlpha = 0.0f;
  54. self.offSet = CGPointMake(0.0f, 0.0f);
  55. _customImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
  56. [self addSubview:_customImageView];
  57. _label1 = [[UILabel alloc] initWithFrame:CGRectZero];
  58. [_label1 setBackgroundColor:[UIColor clearColor]];
  59. [_label1 setTextAlignment:NSTextAlignmentCenter];
  60. [_label1 setLineBreakMode:NSLineBreakByWordWrapping];
  61. [_label1 setNumberOfLines:0];
  62. [_label1 setFont:[UIFont systemFontOfSize:15.0f]];
  63. [self addSubview:_label1];
  64. [_label1 setTextColor:[UIColor whiteColor]];
  65. [_label1 setText:@""];
  66. _label2 = [[UILabel alloc] initWithFrame:CGRectZero];
  67. [_label2 setBackgroundColor:[UIColor clearColor]];
  68. [_label2 setTextAlignment:NSTextAlignmentCenter];
  69. [_label2 setFont:[UIFont systemFontOfSize:13.0f]];
  70. [self addSubview:_label2];
  71. [_label2 setTextColor:[UIColor whiteColor]];
  72. [_label2 setText:@""];
  73. _textField = [[UITextField alloc] initWithFrame:CGRectZero];
  74. [self addSubview:_textField];
  75. _customImageOffSet = CGPointMake(0.0f, 0.0f);
  76. _label1OffSet = CGPointMake(0.0f, 0.0f);
  77. _label1LeftAndRightMarign = 15.0f;
  78. }
  79. return self;
  80. }
  81. - (void)setCustomView:(UIView *)customView
  82. {
  83. [_customView removeFromSuperview];
  84. _customView = customView;
  85. [_customImageView setHidden:YES];
  86. [self addSubview:_customView];
  87. }
  88. - (void)layoutSubviews
  89. {
  90. [super layoutSubviews];
  91. [_customImageView sizeToFit];
  92. CGRect rectangleFrame = [self rectangleFrame];
  93. CGRect frame = CGRectMake(floor((self.bounds.size.width-_customImageView.bounds.size.width)/2.0), floor(rectangleFrame.origin.y + 21.0f), _customImageView.bounds.size.width, _customImageView.bounds.size.height);
  94. frame.origin.x += _customImageOffSet.x;
  95. frame.origin.y += _customImageOffSet.y;
  96. [_customImageView setFrame:frame];
  97. if (_customView) {
  98. CGRect customFrame = CGRectMake(floor((self.bounds.size.width-_customView.bounds.size.width)/2.0 + _customViewOffSet.x), floor(rectangleFrame.origin.y + _customViewOffSet.y), _customView.bounds.size.width, _customView.bounds.size.height);
  99. [_customView setFrame:customFrame];
  100. }
  101. CGRect lable1Frame = CGRectMake(rectangleFrame.origin.x + _label1LeftAndRightMarign, rectangleFrame.origin.y + (rectangleFrame.size.height - 50.0f), rectangleFrame.size.width - _label1LeftAndRightMarign*2, _label1.bounds.size.height);
  102. [_label1 setFrame:lable1Frame];
  103. [_label1 sizeToFit];
  104. //移到中间
  105. lable1Frame = _label1.frame;
  106. lable1Frame.origin.x = floor(rectangleFrame.origin.x +(rectangleFrame.size.width - _label1.frame.size.width)/2.0 + _label1OffSet.x);
  107. lable1Frame.origin.y = floor(lable1Frame.origin.y + _label1OffSet.y);
  108. [_label1 setFrame:lable1Frame];
  109. [_label2 setFrame:CGRectMake(floor(rectangleFrame.origin.x +10.0f), floor(rectangleFrame.origin.y + (rectangleFrame.size.height - 30.0f)), rectangleFrame.size.width - 20.0f, 23.0f)];
  110. }
  111. - (void)drawRect:(CGRect)rect
  112. {
  113. //取中间的一块
  114. UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:[self rectangleFrame] cornerRadius:_cornerRadio];
  115. [[UIColor colorWithWhite:0 alpha:self.blackAlpha] setFill];
  116. [path fill];
  117. }
  118. - (CGRect)rectangleFrame
  119. {
  120. CGRect frame = CGRectMake((self.bounds.size.width - _rectangleWidth)/2.0f, (self.bounds.size.height-_rectangleHeight)/2.0, _rectangleWidth, _rectangleHeight);
  121. frame.origin.x += _offSet.x;
  122. frame.origin.y += _offSet.y;
  123. return frame;
  124. }
  125. @end