Bez popisu

LGAlertViewButton.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // LGAlertViewButton.m
  3. // LGAlertView
  4. //
  5. //
  6. // The MIT License (MIT)
  7. //
  8. // Copyright © 2015 Grigory Lutkov <Friend.LGA@gmail.com>
  9. // (https://github.com/Friend-LGA/LGAlertView)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy
  12. // of this software and associated documentation files (the "Software"), to deal
  13. // in the Software without restriction, including without limitation the rights
  14. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. // copies of the Software, and to permit persons to whom the Software is
  16. // furnished to do so, subject to the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be included in all
  19. // copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. // SOFTWARE.
  28. //
  29. #import "LGAlertViewButton.h"
  30. #import "LGAlertViewHelper.h"
  31. @implementation LGAlertViewButton
  32. - (instancetype)init {
  33. self = [super init];
  34. if (self) {
  35. self.backgroundColor = UIColor.clearColor;
  36. self.titleLabel.backgroundColor = UIColor.clearColor;
  37. self.imageView.backgroundColor = UIColor.clearColor;
  38. self.contentEdgeInsets = UIEdgeInsetsMake(LGAlertViewPaddingHeight,
  39. LGAlertViewPaddingWidth,
  40. LGAlertViewPaddingHeight,
  41. LGAlertViewPaddingWidth);
  42. self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  43. self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  44. self.adjustsImageWhenHighlighted = NO;
  45. self.adjustsImageWhenDisabled = NO;
  46. }
  47. return self;
  48. }
  49. - (void)layoutSubviews {
  50. [super layoutSubviews];
  51. if (!self.imageView.image || !self.titleLabel.text.length) {
  52. return;
  53. }
  54. CGRect imageViewFrame = self.imageView.frame;
  55. CGRect titleLabelFrame = self.titleLabel.frame;
  56. if (self.iconPosition == LGAlertViewButtonIconPositionLeft) {
  57. if (self.titleLabel.textAlignment == NSTextAlignmentLeft) {
  58. imageViewFrame.origin.x = self.contentEdgeInsets.left;
  59. titleLabelFrame.origin.x = CGRectGetMaxX(imageViewFrame) + LGAlertViewButtonImageOffsetFromTitle;
  60. }
  61. else if (self.titleLabel.textAlignment == NSTextAlignmentRight) {
  62. imageViewFrame.origin.x = self.contentEdgeInsets.left;
  63. titleLabelFrame.origin.x = CGRectGetWidth(self.bounds) - self.contentEdgeInsets.right;
  64. }
  65. else {
  66. imageViewFrame.origin.x -= LGAlertViewButtonImageOffsetFromTitle / 2.0;
  67. titleLabelFrame.origin.x += LGAlertViewButtonImageOffsetFromTitle / 2.0;
  68. }
  69. }
  70. else {
  71. if (self.titleLabel.textAlignment == NSTextAlignmentLeft) {
  72. titleLabelFrame.origin.x = self.contentEdgeInsets.left;
  73. imageViewFrame.origin.x = CGRectGetWidth(self.bounds) - self.contentEdgeInsets.right - CGRectGetWidth(imageViewFrame);
  74. }
  75. else if (self.titleLabel.textAlignment == NSTextAlignmentRight) {
  76. imageViewFrame.origin.x = CGRectGetWidth(self.bounds) - self.contentEdgeInsets.right - CGRectGetWidth(imageViewFrame);
  77. titleLabelFrame.origin.x = CGRectGetMinX(imageViewFrame) - LGAlertViewButtonImageOffsetFromTitle - CGRectGetWidth(titleLabelFrame);
  78. }
  79. else {
  80. imageViewFrame.origin.x += CGRectGetWidth(titleLabelFrame) + (LGAlertViewButtonImageOffsetFromTitle / 2.0);
  81. titleLabelFrame.origin.x -= CGRectGetWidth(imageViewFrame) + (LGAlertViewButtonImageOffsetFromTitle / 2.0);
  82. }
  83. }
  84. if (LGAlertViewHelper.isNotRetina) {
  85. imageViewFrame = CGRectIntegral(imageViewFrame);
  86. }
  87. self.imageView.frame = imageViewFrame;
  88. if (LGAlertViewHelper.isNotRetina) {
  89. titleLabelFrame = CGRectIntegral(imageViewFrame);
  90. }
  91. self.titleLabel.frame = titleLabelFrame;
  92. }
  93. - (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state {
  94. [self setBackgroundImage:[LGAlertViewHelper image1x1WithColor:color] forState:state];
  95. }
  96. @end