添加联想
This commit is contained in:
@@ -31,6 +31,9 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/// emoji 面板点击搜索
|
||||
- (void)keyBoardMainViewDidTapEmojiSearch:(KBKeyBoardMainView *)keyBoardMainView;
|
||||
|
||||
/// 选择了联想词
|
||||
- (void)keyBoardMainView:(KBKeyBoardMainView *)keyBoardMainView didSelectSuggestion:(NSString *)suggestion;
|
||||
@end
|
||||
|
||||
@interface KBKeyBoardMainView : UIView
|
||||
@@ -39,6 +42,9 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
/// 应用当前皮肤(会触发键区重载以应用按键颜色)
|
||||
- (void)kb_applyTheme;
|
||||
|
||||
/// 更新联想候选
|
||||
- (void)kb_setSuggestions:(NSArray<NSString *> *)suggestions;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
@@ -11,14 +11,17 @@
|
||||
#import "KBFunctionView.h"
|
||||
#import "KBKey.h"
|
||||
#import "KBEmojiPanelView.h"
|
||||
#import "KBSuggestionBarView.h"
|
||||
#import "Masonry.h"
|
||||
#import "KBSkinManager.h"
|
||||
|
||||
@interface KBKeyBoardMainView ()<KBToolBarDelegate, KBKeyboardViewDelegate, KBEmojiPanelViewDelegate>
|
||||
@interface KBKeyBoardMainView ()<KBToolBarDelegate, KBKeyboardViewDelegate, KBEmojiPanelViewDelegate, KBSuggestionBarViewDelegate>
|
||||
@property (nonatomic, strong) KBToolBar *topBar;
|
||||
@property (nonatomic, strong) KBSuggestionBarView *suggestionBar;
|
||||
@property (nonatomic, strong) KBKeyboardView *keyboardView;
|
||||
@property (nonatomic, strong) KBEmojiPanelView *emojiView;
|
||||
@property (nonatomic, assign) BOOL emojiPanelVisible;
|
||||
@property (nonatomic, assign) BOOL suggestionBarHasItems;
|
||||
// 注意:功能面板的展示/隐藏由外部控制器决定,此处不再直接管理显隐
|
||||
@end
|
||||
@implementation KBKeyBoardMainView
|
||||
@@ -31,9 +34,17 @@
|
||||
self.topBar = [[KBToolBar alloc] init];
|
||||
self.topBar.delegate = self;
|
||||
[self addSubview:self.topBar];
|
||||
|
||||
// 联想栏
|
||||
self.suggestionBar = [[KBSuggestionBarView alloc] init];
|
||||
self.suggestionBar.delegate = self;
|
||||
self.suggestionBar.hidden = YES;
|
||||
[self addSubview:self.suggestionBar];
|
||||
|
||||
// 键盘区域(高度按照设计值做等比缩放,避免不同机型上按键被压缩/拉伸)
|
||||
CGFloat keyboardAreaHeight = KBFit(200.0f);
|
||||
CGFloat bottomInset = KBFit(4.0f);
|
||||
CGFloat topBarHeight = KBFit(40.0f);
|
||||
CGFloat barSpacing = KBFit(6.0f);
|
||||
|
||||
self.keyboardView = [[KBKeyboardView alloc] init];
|
||||
@@ -57,7 +68,17 @@
|
||||
[self.topBar mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.right.equalTo(self);
|
||||
make.top.equalTo(self.mas_top).offset(0);
|
||||
make.bottom.equalTo(self.keyboardView.mas_top).offset(0);
|
||||
make.height.mas_equalTo(topBarHeight);
|
||||
}];
|
||||
|
||||
[self.suggestionBar mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.right.equalTo(self);
|
||||
make.top.equalTo(self.topBar);
|
||||
make.bottom.equalTo(self.topBar);
|
||||
}];
|
||||
|
||||
[self.keyboardView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.equalTo(self.topBar.mas_bottom).offset(barSpacing);
|
||||
}];
|
||||
// 功能面板切换交由外部控制器处理;此处不直接创建/管理
|
||||
}
|
||||
@@ -74,17 +95,24 @@
|
||||
} else {
|
||||
self.keyboardView.hidden = NO;
|
||||
self.topBar.hidden = NO;
|
||||
self.suggestionBar.hidden = !self.suggestionBarHasItems;
|
||||
}
|
||||
|
||||
void (^changes)(void) = ^{
|
||||
self.emojiView.alpha = visible ? 1.0 : 0.0;
|
||||
self.keyboardView.alpha = visible ? 0.0 : 1.0;
|
||||
self.topBar.alpha = visible ? 0.0 : 1.0;
|
||||
self.suggestionBar.alpha = visible ? 0.0 : (self.suggestionBarHasItems ? 1.0 : 0.0);
|
||||
};
|
||||
void (^completion)(BOOL) = ^(BOOL finished) {
|
||||
self.emojiView.hidden = !visible;
|
||||
self.keyboardView.hidden = visible;
|
||||
self.topBar.hidden = visible;
|
||||
if (visible) {
|
||||
self.suggestionBar.hidden = YES;
|
||||
} else {
|
||||
self.suggestionBar.hidden = !self.suggestionBarHasItems;
|
||||
}
|
||||
};
|
||||
|
||||
if (animated) {
|
||||
@@ -211,10 +239,34 @@
|
||||
if ([self.topBar respondsToSelector:@selector(kb_applyTheme)]) {
|
||||
[self.topBar kb_applyTheme];
|
||||
}
|
||||
[self.suggestionBar applyTheme:mgr.current];
|
||||
[self.keyboardView reloadKeys];
|
||||
if (self.emojiView) {
|
||||
[self.emojiView applyTheme:mgr.current];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Suggestions
|
||||
|
||||
- (void)kb_setSuggestions:(NSArray<NSString *> *)suggestions {
|
||||
self.suggestionBarHasItems = (suggestions.count > 0);
|
||||
[self.suggestionBar updateSuggestions:suggestions];
|
||||
|
||||
if (self.emojiPanelVisible) {
|
||||
self.suggestionBar.hidden = YES;
|
||||
self.suggestionBar.alpha = 0.0;
|
||||
} else {
|
||||
self.suggestionBar.hidden = !self.suggestionBarHasItems;
|
||||
self.suggestionBar.alpha = self.suggestionBarHasItems ? 1.0 : 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - KBSuggestionBarViewDelegate
|
||||
|
||||
- (void)suggestionBarView:(KBSuggestionBarView *)view didSelectSuggestion:(NSString *)suggestion {
|
||||
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didSelectSuggestion:)]) {
|
||||
[self.delegate keyBoardMainView:self didSelectSuggestion:suggestion];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
26
CustomKeyboard/View/KBSuggestionBarView.h
Normal file
26
CustomKeyboard/View/KBSuggestionBarView.h
Normal file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// KBSuggestionBarView.h
|
||||
// CustomKeyboard
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class KBSuggestionBarView;
|
||||
@class KBSkinTheme;
|
||||
|
||||
@protocol KBSuggestionBarViewDelegate <NSObject>
|
||||
- (void)suggestionBarView:(KBSuggestionBarView *)view didSelectSuggestion:(NSString *)suggestion;
|
||||
@end
|
||||
|
||||
@interface KBSuggestionBarView : UIView
|
||||
|
||||
@property (nonatomic, weak) id<KBSuggestionBarViewDelegate> delegate;
|
||||
|
||||
- (void)updateSuggestions:(NSArray<NSString *> *)suggestions;
|
||||
- (void)applyTheme:(KBSkinTheme *)theme;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
114
CustomKeyboard/View/KBSuggestionBarView.m
Normal file
114
CustomKeyboard/View/KBSuggestionBarView.m
Normal file
@@ -0,0 +1,114 @@
|
||||
//
|
||||
// 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<NSString *> *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<NSString *> *)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
|
||||
Reference in New Issue
Block a user