// // KBSuggestionBarView.m // CustomKeyboard // #import "KBSuggestionBarView.h" #import "Masonry.h" #import "KBSkinManager.h" @interface KBSuggestionBarView () @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) UIStackView *stackView; @property (nonatomic, copy) NSArray *items; @property (nonatomic, strong) UIColor *pillColor; @property (nonatomic, strong) UIColor *textColor; @end @implementation KBSuggestionBarView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor clearColor]; [self setupUI]; } return self; } - (void)setupUI { [self addSubview:self.scrollView]; [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; [self.scrollView addSubview:self.stackView]; [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.scrollView).insets(UIEdgeInsetsMake(0, 8, 0, 8)); make.height.equalTo(self.scrollView); }]; [self applyTheme:[KBSkinManager shared].current]; } - (void)updateSuggestions:(NSArray *)suggestions { self.items = suggestions ?: @[]; for (UIView *view in self.stackView.arrangedSubviews) { [self.stackView removeArrangedSubview:view]; [view removeFromSuperview]; } for (NSString *item in self.items) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.layer.cornerRadius = 12.0; btn.layer.masksToBounds = YES; btn.backgroundColor = self.pillColor ?: [UIColor colorWithWhite:1 alpha:0.9]; btn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; [btn setTitle:item forState:UIControlStateNormal]; [btn setTitleColor:self.textColor ?: [UIColor blackColor] forState:UIControlStateNormal]; btn.contentEdgeInsets = UIEdgeInsetsMake(4, 10, 4, 10); [btn addTarget:self action:@selector(onTapSuggestion:) forControlEvents:UIControlEventTouchUpInside]; [self.stackView addArrangedSubview:btn]; } self.hidden = (self.items.count == 0); } - (void)applyTheme:(KBSkinTheme *)theme { UIColor *bg = theme.keyBackground ?: [UIColor whiteColor]; UIColor *text = theme.keyTextColor ?: [UIColor blackColor]; UIColor *barBg = theme.keyboardBackground ?: [UIColor colorWithWhite:0.95 alpha:1.0]; self.backgroundColor = barBg; self.pillColor = bg; self.textColor = text; for (UIView *view in self.stackView.arrangedSubviews) { if (![view isKindOfClass:[UIButton class]]) { continue; } UIButton *btn = (UIButton *)view; btn.backgroundColor = self.pillColor; [btn setTitleColor:self.textColor forState:UIControlStateNormal]; } } #pragma mark - Actions - (void)onTapSuggestion:(UIButton *)sender { NSString *title = sender.currentTitle ?: @""; if (title.length == 0) { return; } if ([self.delegate respondsToSelector:@selector(suggestionBarView:didSelectSuggestion:)]) { [self.delegate suggestionBarView:self didSelectSuggestion:title]; } } #pragma mark - Lazy - (UIScrollView *)scrollView { if (!_scrollView) { _scrollView = [[UIScrollView alloc] init]; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.alwaysBounceHorizontal = YES; } return _scrollView; } - (UIStackView *)stackView { if (!_stackView) { _stackView = [[UIStackView alloc] init]; _stackView.axis = UILayoutConstraintAxisHorizontal; _stackView.alignment = UIStackViewAlignmentCenter; _stackView.spacing = 8.0; } return _stackView; } @end