// // SortButton.m // iSales-NPD // // Created by Jack on 16/9/6. // Copyright © 2016年 United Software Applications, Inc. All rights reserved. // #import "SortButton.h" @implementation SortButton - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self addSubview:self.titleLabel]; [self addSubview:self.imageView]; } return self; } + (SortButton*)sortButtonWithHeight:(CGFloat)height { SortButton *btn = [[SortButton alloc] initWithFrame:CGRectMake(10, 0, 85, height)]; return btn; } - (UILabel *)titleLabel { if (!_titleLabel) { CGSize size = [self string:@"sort by" sizeWithSize:CGSizeMake(60, 22) font:[UIFont systemFontOfSize:17]]; CGFloat h = CGRectGetHeight(self.bounds); _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (h - size.height) / 2, size.width, size.height)]; _titleLabel.font = [UIFont systemFontOfSize:17]; _titleLabel.textColor = [UIColor blackColor]; _titleLabel.text = @"Sort By"; [_titleLabel sizeToFit]; } return _titleLabel; } - (UIImageView *)imageView { if (!_imageView) { CGFloat h = CGRectGetHeight(self.bounds); CGFloat x = CGRectGetMaxX(self.titleLabel.frame) + 5; CGFloat y = (h - 22) / 2; _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, 22, 22)]; _imageView.image = [UIImage imageNamed:@"DX_22"]; _imageView.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageView:)]; [_imageView addGestureRecognizer:tap]; } return _imageView; } -(CGSize)string:(NSString *)string sizeWithSize:(CGSize)size font:(UIFont *)font{ if (!font) { font = [UIFont systemFontOfSize:15.0f]; } NSDictionary *attribute = @{NSFontAttributeName: font}; CGSize resSize = [string boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attribute context:nil].size; return resSize; } - (void)clickImageView:(id)recognizer { [self sendActionsForControlEvents:UIControlEventTouchUpInside]; } @end