// // KBKeyboardSubscriptionFeatureItemView.m // CustomKeyboard // #import "KBKeyboardSubscriptionFeatureItemView.h" #import "Masonry.h" @interface KBKeyboardSubscriptionFeatureItemView () @property (nonatomic, strong) UIImageView *iconView; @property (nonatomic, strong) UILabel *titleLabel; @end @implementation KBKeyboardSubscriptionFeatureItemView static const CGFloat kKBFeatureItemPadding = 5.0; static const CGFloat kKBFeatureItemIconSize = 35.0; static const CGFloat kKBFeatureItemGap = 5.0; - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // self.layer.cornerRadius = 24; // self.layer.masksToBounds = YES; // self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.85]; [self addSubview:self.iconView]; [self addSubview:self.titleLabel]; [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.mas_left).offset(kKBFeatureItemPadding); make.centerY.equalTo(self.mas_centerY); make.width.height.mas_equalTo(kKBFeatureItemIconSize); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.iconView.mas_right).offset(kKBFeatureItemGap); make.centerY.equalTo(self.mas_centerY); make.right.equalTo(self.mas_right).offset(-kKBFeatureItemPadding); }]; } return self; } - (void)configureWithImage:(UIImage *)image title:(NSString *)title { self.iconView.image = image; self.titleLabel.text = title ?: @""; } + (CGFloat)preferredWidthForTitle:(NSString *)title { UIFont *font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold]; NSString *text = title ?: @""; NSArray *lines = [text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; CGFloat maxLineWidth = 0; for (NSString *line in lines) { NSString *trimLine = [line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; if (trimLine.length == 0) { continue; } CGSize size = [trimLine sizeWithAttributes:@{NSFontAttributeName: font}]; maxLineWidth = MAX(maxLineWidth, ceil(size.width)); } if (maxLineWidth <= 0) { maxLineWidth = 80; } CGFloat width = maxLineWidth + 50.0; // 5 + 35 + 5 + 5 width = MIN(MAX(width, 120.0), 240.0); return width; } - (UIImageView *)iconView { if (!_iconView) { _iconView = [[UIImageView alloc] init]; _iconView.contentMode = UIViewContentModeScaleAspectFit; } return _iconView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold]; _titleLabel.textColor = [UIColor colorWithHex:0x4A4A4A]; _titleLabel.numberOfLines = 0; _titleLabel.lineBreakMode = NSLineBreakByWordWrapping; } return _titleLabel; } @end