1
0
mirror of https://github.com/danog/Telegram.git synced 2024-12-03 09:57:46 +01:00
Telegram/Telegraph/TGVariantCollectionItemView.m
2016-02-25 01:03:51 +01:00

127 lines
4.5 KiB
Objective-C

/*
* This is the source code of Telegram for iOS v. 1.1
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Peter Iakovlev, 2013.
*/
#import "TGVariantCollectionItemView.h"
#import "TGImageUtils.h"
#import "TGFont.h"
@interface TGVariantCollectionItemView ()
{
UILabel *_titleLabel;
UILabel *_variantLabel;
UIImageView *_iconView;
UIImageView *_disclosureIndicator;
}
@end
@implementation TGVariantCollectionItemView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self != nil)
{
_titleLabel = [[UILabel alloc] init];
_titleLabel.textColor = [UIColor blackColor];
_titleLabel.backgroundColor = [UIColor clearColor];
_titleLabel.font = TGSystemFontOfSize(17);
[self addSubview:_titleLabel];
_variantLabel = [[UILabel alloc] init];
_variantLabel.textColor = UIColorRGB(0x929297);
_variantLabel.backgroundColor = [UIColor clearColor];
_variantLabel.font = TGSystemFontOfSize(17);
[self addSubview:_variantLabel];
_disclosureIndicator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ModernListsDisclosureIndicator.png"]];
[self addSubview:_disclosureIndicator];
}
return self;
}
- (void)setTitle:(NSString *)title
{
_titleLabel.text = title;
[self setNeedsLayout];
}
- (void)setVariant:(NSString *)variant
{
_variantLabel.text = variant;
[self setNeedsLayout];
}
- (void)setIcon:(UIImage *)icon
{
if (_iconView == nil && icon != nil)
{
_iconView = [[UIImageView alloc] initWithFrame:CGRectMake(15, (self.frame.size.height - 15) / 2, 29, 29)];
_iconView.contentMode = UIViewContentModeCenter;
[self addSubview:_iconView];
}
_iconView.image = icon;
self.separatorInset = (icon != nil) ? 59.0f : 15.0f;
[self setNeedsLayout];
}
- (void)setEnabled:(bool)enabled {
self.userInteractionEnabled = enabled;
_titleLabel.textColor = enabled ? [UIColor blackColor] : UIColorRGB(0x8f8f8f);
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect bounds = self.bounds;
CGSize titleSize = [_titleLabel sizeThatFits:CGSizeMake(bounds.size.width, CGFLOAT_MAX)];
CGSize variantSize = [_variantLabel sizeThatFits:CGSizeMake(bounds.size.width, CGFLOAT_MAX)];
_disclosureIndicator.frame = CGRectMake(bounds.size.width - _disclosureIndicator.frame.size.width - 15, CGFloor((bounds.size.height - _disclosureIndicator.frame.size.height) / 2), _disclosureIndicator.frame.size.width, _disclosureIndicator.frame.size.height);
CGFloat startingX = (_iconView.image != nil) ? 59.0f : 15.0f;
CGFloat indicatorSpacing = 10.0f;
CGFloat labelSpacing = 8.0f;
CGFloat availableWidth = _disclosureIndicator.frame.origin.x - startingX - indicatorSpacing;
CGFloat titleY = CGFloor((bounds.size.height - titleSize.height) / 2.0f) + TGRetinaPixel;
CGFloat variantY = CGFloor((bounds.size.height - variantSize.height) / 2.0f) + TGRetinaPixel;
if (titleSize.width + labelSpacing + variantSize.width <= availableWidth)
{
_titleLabel.frame = CGRectMake(startingX, titleY, titleSize.width, titleSize.height);
_variantLabel.frame = CGRectMake(startingX + availableWidth - variantSize.width, variantY, variantSize.width, variantSize.height);
}
else if (titleSize.width > variantSize.width)
{
CGFloat titleWidth = CGFloor(availableWidth * 2.0f / 3.0f) - labelSpacing;
_titleLabel.frame = CGRectMake(startingX, titleY, titleWidth, titleSize.height);
CGFloat variantWidth = MIN(variantSize.width, availableWidth - titleWidth - labelSpacing);
_variantLabel.frame = CGRectMake(startingX + availableWidth - variantWidth, variantY, variantWidth, variantSize.height);
}
else
{
CGFloat variantWidth = CGFloor(availableWidth / 2.0f) - labelSpacing;
_variantLabel.frame = CGRectMake(startingX + availableWidth - variantWidth, variantY, variantWidth, variantSize.height);
CGFloat titleWidth = MIN(titleSize.width, availableWidth - variantWidth - labelSpacing);
_titleLabel.frame = CGRectMake(startingX, titleY, titleWidth, titleSize.height);
}
if (_iconView.image != nil)
{
_iconView.frame = CGRectMake(_iconView.frame.origin.x, (self.frame.size.height - _iconView.frame.size.height) / 2, _iconView.frame.size.width, _iconView.frame.size.height);
}
}
@end