123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- //
- // LGAlertViewCell.m
- // LGAlertView
- //
- //
- // The MIT License (MIT)
- //
- // Copyright © 2015 Grigory Lutkov <Friend.LGA@gmail.com>
- // (https://github.com/Friend-LGA/LGAlertView)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in all
- // copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- // SOFTWARE.
- //
- #import "LGAlertViewCell.h"
- #import "LGAlertViewHelper.h"
- @interface LGAlertViewCell ()
- @property (readwrite) UIView *separatorView;
- @end
- @implementation LGAlertViewCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
- self.selectionStyle = UITableViewCellSelectionStyleNone;
- [self clearBackgrounds];
- self.separatorView = [UIView new];
- [self addSubview:self.separatorView];
- self.enabled = YES;
- }
- return self;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- CGFloat frameWidth = CGRectGetWidth(self.bounds);
- CGFloat frameHeight = CGRectGetHeight(self.bounds);
- // -----
- BOOL imageExists = (self.imageView.image != nil);
- CGSize imageSize = self.imageView.image.size;
- CGFloat textLabelMaxWidth;
- if (imageExists) {
- textLabelMaxWidth = frameWidth - (LGAlertViewPaddingWidth * 2.0) - imageSize.width - LGAlertViewButtonImageOffsetFromTitle;
- }
- else {
- textLabelMaxWidth = frameWidth - (LGAlertViewPaddingWidth * 2.0);
- }
- CGRect textLabelFrame = CGRectMake(NSNotFound,
- LGAlertViewPaddingHeight,
- textLabelMaxWidth,
- frameHeight - (LGAlertViewPaddingHeight * 2.0));
- if (self.textLabel.textAlignment == NSTextAlignmentLeft) {
- textLabelFrame.origin.x = LGAlertViewPaddingWidth;
- if (imageExists && self.iconPosition == LGAlertViewButtonIconPositionLeft) {
- textLabelFrame.origin.x += imageSize.width + LGAlertViewButtonImageOffsetFromTitle;
- }
- }
- else if (self.textLabel.textAlignment == NSTextAlignmentRight) {
- textLabelFrame.origin.x = frameWidth - textLabelMaxWidth - LGAlertViewPaddingWidth;
- if (imageExists && self.iconPosition == LGAlertViewButtonIconPositionRight) {
- textLabelFrame.origin.x -= imageSize.width + LGAlertViewButtonImageOffsetFromTitle;
- }
- }
- else {
- textLabelFrame.origin.x = (frameWidth / 2.0) - (textLabelMaxWidth / 2.0);
- if (imageExists) {
- if (self.iconPosition == LGAlertViewButtonIconPositionLeft) {
- textLabelFrame.origin.x += (imageSize.width / 2.0) + (LGAlertViewButtonImageOffsetFromTitle / 2.0);
- }
- else {
- textLabelFrame.origin.x -= (imageSize.width / 2.0) + (LGAlertViewButtonImageOffsetFromTitle / 2.0);
- }
- }
- }
- if (LGAlertViewHelper.isNotRetina) {
- textLabelFrame = CGRectIntegral(textLabelFrame);
- }
- self.textLabel.frame = textLabelFrame;
- // -----
- if (self.imageView.image) {
- CGSize imageSize = self.imageView.image.size;
- CGRect imageViewFrame = CGRectMake(NSNotFound,
- (frameHeight / 2.0) - (imageSize.height / 2.0),
- imageSize.width,
- imageSize.height);
- if (self.textLabel.textAlignment == NSTextAlignmentCenter) {
- CGSize textLabelSize = [self.textLabel sizeThatFits:CGSizeMake(textLabelMaxWidth, CGFLOAT_MAX)];
- CGFloat imageAndTextWidth = imageSize.width + textLabelSize.width + LGAlertViewButtonImageOffsetFromTitle;
- if (self.iconPosition == LGAlertViewButtonIconPositionLeft) {
- imageViewFrame.origin.x = (frameWidth / 2.0) - (imageAndTextWidth / 2.0);
- }
- else {
- imageViewFrame.origin.x = (frameWidth / 2.0) + (imageAndTextWidth / 2.0) - imageSize.width;
- }
- }
- else {
- if (self.iconPosition == LGAlertViewButtonIconPositionLeft) {
- imageViewFrame.origin.x = LGAlertViewPaddingWidth;
- }
- else {
- imageViewFrame.origin.x = frameWidth - imageSize.width - LGAlertViewPaddingWidth;
- }
- }
- if (LGAlertViewHelper.isNotRetina) {
- imageViewFrame = CGRectIntegral(imageViewFrame);
- }
- self.imageView.frame = imageViewFrame;
- }
- // -----
- if (!self.separatorView.hidden) {
- self.separatorView.frame = CGRectMake(0.0,
- CGRectGetHeight(self.bounds) - LGAlertViewHelper.separatorHeight,
- CGRectGetWidth(self.bounds),
- LGAlertViewHelper.separatorHeight);
- }
- }
- - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
- [super setHighlighted:highlighted animated:animated];
- if (highlighted) {
- self.textLabel.textColor = self.titleColorHighlighted;
- self.imageView.image = self.imageHighlighted;
- self.backgroundColor = self.backgroundColorHighlighted;
- }
- else {
- [self setEnabled:self.enabled];
- }
- [self clearBackgrounds];
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- if (selected) {
- self.textLabel.textColor = self.titleColorHighlighted;
- self.imageView.image = self.imageHighlighted;
- self.backgroundColor = self.backgroundColorHighlighted;
- }
- else {
- [self setEnabled:self.enabled];
- }
- [self clearBackgrounds];
- }
- - (void)setEnabled:(BOOL)enabled {
- _enabled = enabled;
- self.userInteractionEnabled = enabled;
- if (enabled) {
- self.textLabel.textColor = self.titleColor;
- self.imageView.image = self.image;
- self.backgroundColor = self.backgroundColorNormal;
- }
- else {
- self.textLabel.textColor = self.titleColorDisabled;
- self.imageView.image = self.imageDisabled;
- self.backgroundColor = self.backgroundColorDisabled;
- }
- }
- - (CGSize)sizeThatFits:(CGSize)size {
- CGSize sizeForTextLabel = size;
- sizeForTextLabel.width -= LGAlertViewPaddingWidth * 2.0;
- if (self.imageView.image) {
- sizeForTextLabel.width -= self.imageView.image.size.width + LGAlertViewButtonImageOffsetFromTitle;
- }
- CGSize labelSize = [self.textLabel sizeThatFits:size];
- CGSize resultSize;
- if (self.imageView.image) {
- CGSize imageSize = self.imageView.image.size;
- CGFloat width = MAX(labelSize.width, imageSize.width);
- width = MIN(width, size.width);
- CGFloat height = MAX(labelSize.height, imageSize.height);
- resultSize = CGSizeMake(width, height);
- }
- else {
- resultSize = labelSize;
- }
- resultSize.height += LGAlertViewPaddingHeight * 2.0;
- return resultSize;
- }
- - (void)clearBackgrounds {
- self.textLabel.backgroundColor = UIColor.clearColor;
- self.imageView.backgroundColor = UIColor.clearColor;
- }
- @end
|