2020-04-21 13:26:16 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 OpenMarket Ltd
|
|
|
|
Copyright 2020 Vector Creations Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "SectionHeaderView.h"
|
|
|
|
|
2022-02-10 00:14:00 +00:00
|
|
|
#import "GeneratedInterface-Swift.h"
|
|
|
|
|
2020-04-21 13:26:16 +00:00
|
|
|
static const CGFloat kInterItemsSpaceHorizontal = 8.0;
|
|
|
|
|
|
|
|
@implementation SectionHeaderView
|
|
|
|
|
2022-02-10 00:14:00 +00:00
|
|
|
+ (NSString*)defaultReuseIdentifier
|
|
|
|
{
|
|
|
|
return NSStringFromClass([self class]);
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:26:16 +00:00
|
|
|
- (void)setMinimumLeftInset:(CGFloat)minimumLeftInset
|
|
|
|
{
|
|
|
|
_minimumLeftInset = minimumLeftInset;
|
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMinimumRightInset:(CGFloat)minimumRightInset
|
|
|
|
{
|
|
|
|
_minimumRightInset = minimumRightInset;
|
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTopViewHeight:(CGFloat)topViewHeight
|
|
|
|
{
|
|
|
|
_topViewHeight = topViewHeight;
|
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
2022-08-02 15:27:33 +00:00
|
|
|
- (void)setTopPadding:(CGFloat)topPadding
|
|
|
|
{
|
|
|
|
_topPadding = topPadding;
|
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:26:16 +00:00
|
|
|
- (void)setTopSpanningView:(UIView *)topSpanningView
|
|
|
|
{
|
2022-02-10 00:14:00 +00:00
|
|
|
// remove old one
|
|
|
|
[_topSpanningView removeFromSuperview];
|
2020-04-21 13:26:16 +00:00
|
|
|
_topSpanningView = topSpanningView;
|
2022-02-10 00:14:00 +00:00
|
|
|
if (_topSpanningView)
|
|
|
|
{
|
|
|
|
// add new one
|
|
|
|
[self.contentView addSubview:_topSpanningView];
|
|
|
|
}
|
2020-04-21 13:26:16 +00:00
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setHeaderLabel:(UILabel *)headerLabel
|
|
|
|
{
|
2022-02-10 00:14:00 +00:00
|
|
|
// remove old one
|
|
|
|
[_headerLabel removeFromSuperview];
|
2020-04-21 13:26:16 +00:00
|
|
|
_headerLabel = headerLabel;
|
2022-02-10 00:14:00 +00:00
|
|
|
if (_headerLabel)
|
|
|
|
{
|
|
|
|
// add new one
|
|
|
|
[self.contentView addSubview:_headerLabel];
|
|
|
|
}
|
2020-04-21 13:26:16 +00:00
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAccessoryView:(UIView *)accessoryView
|
|
|
|
{
|
2022-02-10 00:14:00 +00:00
|
|
|
// remove old one
|
|
|
|
[_accessoryView removeFromSuperview];
|
2020-04-21 13:26:16 +00:00
|
|
|
_accessoryView = accessoryView;
|
2022-02-10 00:14:00 +00:00
|
|
|
if (_accessoryView)
|
|
|
|
{
|
|
|
|
// add new one
|
|
|
|
[self.contentView addSubview:_accessoryView];
|
|
|
|
}
|
2020-04-21 13:26:16 +00:00
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:48:07 +00:00
|
|
|
- (void)setRightAccessoryView:(UIView *)rightAccessoryView
|
|
|
|
{
|
|
|
|
// remove old one
|
|
|
|
[_rightAccessoryView removeFromSuperview];
|
|
|
|
_rightAccessoryView = rightAccessoryView;
|
|
|
|
if (_rightAccessoryView)
|
|
|
|
{
|
|
|
|
// add new one
|
|
|
|
[self.contentView addSubview:_rightAccessoryView];
|
|
|
|
}
|
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:26:16 +00:00
|
|
|
- (void)setBottomView:(UIView *)bottomView
|
|
|
|
{
|
2022-02-10 00:14:00 +00:00
|
|
|
// remove old one
|
|
|
|
[_bottomView removeFromSuperview];
|
2020-04-21 13:26:16 +00:00
|
|
|
_bottomView = bottomView;
|
2022-02-10 00:14:00 +00:00
|
|
|
if (_bottomView)
|
|
|
|
{
|
|
|
|
// add new one
|
|
|
|
[self.contentView addSubview:_bottomView];
|
|
|
|
}
|
2020-04-21 13:26:16 +00:00
|
|
|
[self setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
return [self initWithFrame:CGRectZero];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
self = [super initWithFrame:frame];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
[self setup];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithCoder:(NSCoder *)aDecoder
|
|
|
|
{
|
|
|
|
self = [super initWithCoder:aDecoder];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
[self setup];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-02-10 00:14:00 +00:00
|
|
|
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
|
|
|
|
{
|
|
|
|
if (self = [super initWithReuseIdentifier:reuseIdentifier])
|
|
|
|
{
|
|
|
|
[self setup];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:26:16 +00:00
|
|
|
- (void)setup
|
|
|
|
{
|
2022-08-02 15:27:33 +00:00
|
|
|
_minimumLeftInset = 16;
|
2020-04-21 13:26:16 +00:00
|
|
|
_minimumRightInset = 16;
|
|
|
|
_topViewHeight = 30;
|
|
|
|
}
|
|
|
|
|
2022-02-10 00:14:00 +00:00
|
|
|
- (void)prepareForReuse
|
|
|
|
{
|
|
|
|
[self.contentView vc_removeAllSubviews];
|
|
|
|
[super prepareForReuse];
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:26:16 +00:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
|
|
|
|
|
|
|
CGFloat _leftInset = 0.0, _rightInset = 0.0;
|
|
|
|
|
2022-02-10 00:14:00 +00:00
|
|
|
CGFloat leftMargin = _minimumLeftInset;
|
|
|
|
CGFloat rightMargin = _minimumRightInset;
|
2020-04-21 13:26:16 +00:00
|
|
|
|
|
|
|
if (_topSpanningView)
|
|
|
|
{
|
2022-02-10 00:14:00 +00:00
|
|
|
CGRect frame = self.contentView.bounds;
|
2022-08-02 15:27:33 +00:00
|
|
|
frame.origin.y += _topPadding;
|
2020-04-21 13:26:16 +00:00
|
|
|
frame.size.height = _topViewHeight;
|
|
|
|
_topSpanningView.frame = frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_headerLabel)
|
|
|
|
{
|
|
|
|
CGRect frame = _headerLabel.frame;
|
|
|
|
frame.origin.x = leftMargin;
|
|
|
|
if (_accessoryView)
|
|
|
|
{
|
|
|
|
rightMargin += _accessoryView.frame.size.width + kInterItemsSpaceHorizontal;
|
|
|
|
}
|
2022-02-28 12:48:07 +00:00
|
|
|
if (_rightAccessoryView)
|
|
|
|
{
|
|
|
|
rightMargin += _rightAccessoryView.frame.size.width + kInterItemsSpaceHorizontal;
|
|
|
|
}
|
2020-04-21 13:26:16 +00:00
|
|
|
if (_bottomView)
|
|
|
|
{
|
|
|
|
// set header label top
|
|
|
|
frame.origin.y = (_topViewHeight - frame.size.height)/2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// center header label vertically
|
2022-02-10 00:14:00 +00:00
|
|
|
frame.origin.y = MAX(0, (self.contentView.bounds.size.height - frame.size.height)/2);
|
2020-04-21 13:26:16 +00:00
|
|
|
}
|
2022-02-28 12:48:07 +00:00
|
|
|
frame.size.width = MIN(self.contentView.bounds.size.width - leftMargin - rightMargin,
|
|
|
|
[_headerLabel sizeThatFits:self.frame.size].width);
|
2022-08-02 15:27:33 +00:00
|
|
|
frame.origin.y += _topPadding;
|
2020-04-21 13:26:16 +00:00
|
|
|
_headerLabel.frame = frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_accessoryView)
|
|
|
|
{
|
|
|
|
// reset margins
|
|
|
|
leftMargin = MAX(_leftInset, 20);
|
|
|
|
rightMargin = MAX(_rightInset, 20);
|
|
|
|
|
|
|
|
CGRect frame = _accessoryView.frame;
|
2022-02-28 12:48:07 +00:00
|
|
|
if(_headerLabel)
|
|
|
|
{
|
|
|
|
frame.origin.x = leftMargin + _headerLabel.frame.size.width + kInterItemsSpaceHorizontal;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
frame.origin.x = leftMargin;
|
|
|
|
}
|
2020-04-21 13:26:16 +00:00
|
|
|
frame.origin.y = MAX(0, (_topViewHeight - frame.size.height)/2);
|
2022-08-02 15:27:33 +00:00
|
|
|
frame.origin.y += _topPadding;
|
2020-04-21 13:26:16 +00:00
|
|
|
_accessoryView.frame = frame;
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:48:07 +00:00
|
|
|
if (_rightAccessoryView)
|
|
|
|
{
|
|
|
|
// reset margins
|
|
|
|
leftMargin = MAX(_leftInset, 20);
|
|
|
|
rightMargin = MAX(_rightInset, 20);
|
|
|
|
|
|
|
|
CGRect frame = _rightAccessoryView.frame;
|
|
|
|
frame.origin.x = self.contentView.bounds.size.width - frame.size.width - rightMargin;
|
|
|
|
frame.origin.y = MAX(0, (_topViewHeight - frame.size.height)/2);
|
2022-08-02 15:27:33 +00:00
|
|
|
frame.origin.y += _topPadding;
|
2022-02-28 12:48:07 +00:00
|
|
|
_rightAccessoryView.frame = frame;
|
|
|
|
}
|
|
|
|
|
2020-04-21 13:26:16 +00:00
|
|
|
if (_bottomView)
|
|
|
|
{
|
|
|
|
// reset margins
|
2022-08-02 15:27:33 +00:00
|
|
|
leftMargin = MAX(_leftInset, 16);
|
|
|
|
rightMargin = MAX(_rightInset, 16);
|
2020-04-21 13:26:16 +00:00
|
|
|
|
|
|
|
CGRect frame = _bottomView.frame;
|
|
|
|
frame.origin.x = leftMargin;
|
|
|
|
frame.origin.y = CGRectGetMaxY(_headerLabel.frame);
|
2022-02-10 00:14:00 +00:00
|
|
|
frame.size.width = self.contentView.bounds.size.width - leftMargin - rightMargin;
|
|
|
|
frame.size.height = self.contentView.bounds.size.height - frame.origin.y;
|
2020-04-21 13:26:16 +00:00
|
|
|
_bottomView.frame = frame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|