ZDChatroomAlertView.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. self.offSet = CGPointMake(0.0f, 0.0f);
  51. _customImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
  52. [self addSubview:_customImageView];
  53. _label1 = [[UILabel alloc] initWithFrame:CGRectZero];
  54. [_label1 setBackgroundColor:[UIColor clearColor]];
  55. [_label1 setTextAlignment:NSTextAlignmentCenter];
  56. [_label1 setLineBreakMode:NSLineBreakByWordWrapping];
  57. [_label1 setNumberOfLines:0];
  58. [_label1 setFont:[UIFont systemFontOfSize:15.0f]];
  59. [self addSubview:_label1];
  60. [_label1 setTextColor:[UIColor whiteColor]];
  61. [_label1 setText:@"xxxxxx"];
  62. _label2 = [[UILabel alloc] initWithFrame:CGRectZero];
  63. [_label2 setBackgroundColor:[UIColor clearColor]];
  64. [_label2 setTextAlignment:NSTextAlignmentCenter];
  65. [_label2 setFont:[UIFont systemFontOfSize:13.0f]];
  66. [self addSubview:_label2];
  67. [_label2 setTextColor:[UIColor whiteColor]];
  68. [_label2 setText:@"xxxxxx"];
  69. _textField = [[UITextField alloc] initWithFrame:CGRectZero];
  70. [self addSubview:_textField];
  71. _customImageOffSet = CGPointMake(0.0f, 0.0f);
  72. _label1OffSet = CGPointMake(0.0f, 0.0f);
  73. _label1LeftAndRightMarign = 15.0f;
  74. }
  75. return self;
  76. }
  77. - (void)setCustomView:(UIView *)customView
  78. {
  79. [_customView removeFromSuperview];
  80. _customView = customView;
  81. [_customImageView setHidden:YES];
  82. [self addSubview:_customView];
  83. }
  84. - (void)layoutSubviews
  85. {
  86. [super layoutSubviews];
  87. [_customImageView sizeToFit];
  88. CGRect rectangleFrame = [self rectangleFrame];
  89. 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);
  90. frame.origin.x += _customImageOffSet.x;
  91. frame.origin.y += _customImageOffSet.y;
  92. [_customImageView setFrame:frame];
  93. if (_customView) {
  94. 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);
  95. [_customView setFrame:customFrame];
  96. }
  97. CGRect lable1Frame = CGRectMake(rectangleFrame.origin.x + _label1LeftAndRightMarign, rectangleFrame.origin.y + (rectangleFrame.size.height - 50.0f), rectangleFrame.size.width - _label1LeftAndRightMarign*2, _label1.bounds.size.height);
  98. [_label1 setFrame:lable1Frame];
  99. [_label1 sizeToFit];
  100. //移到中间
  101. lable1Frame = _label1.frame;
  102. lable1Frame.origin.x = floor(rectangleFrame.origin.x +(rectangleFrame.size.width - _label1.frame.size.width)/2.0 + _label1OffSet.x);
  103. lable1Frame.origin.y = floor(lable1Frame.origin.y + _label1OffSet.y);
  104. [_label1 setFrame:lable1Frame];
  105. [_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)];
  106. }
  107. - (void)drawRect:(CGRect)rect
  108. {
  109. //取中间的一块
  110. UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:[self rectangleFrame] cornerRadius:_cornerRadio];
  111. [[UIColor colorWithWhite:0 alpha:self.blackAlpha] setFill];
  112. [path fill];
  113. }
  114. - (CGRect)rectangleFrame
  115. {
  116. CGRect frame = CGRectMake((self.bounds.size.width - _rectangleWidth)/2.0f, (self.bounds.size.height-_rectangleHeight)/2.0, _rectangleWidth, _rectangleHeight);
  117. frame.origin.x += _offSet.x;
  118. frame.origin.y += _offSet.y;
  119. return frame;
  120. }
  121. @end