@@ -35,6 +35,11 @@ static const CGFloat kKBLettersRow2EdgeSpacerMultiplier = 0.5;
@ property ( nonatomic , strong ) KBKeyboardLayoutConfig * layoutConfig ;
// / 跨 行 统 一 字 符 键 宽 度 ( 按 最 多 字 符 键 的 行 计 算 ) , 0 表 示 不 启 用
@ property ( nonatomic , assign ) CGFloat kb_uniformCharKeyWidth ;
// / 记 录 当 前 行 间 距 , 便 于 切 换 布 局 时 判 断 是 否 需 要 重 建 容 器
@ property ( nonatomic , assign ) CGFloat kb_currentRowSpacing ;
// / 记 录 当 前 顶 / 底 间 距 , 便 于 切 换 布 局 时 判 断 是 否 需 要 重 建 容 器
@ property ( nonatomic , assign ) CGFloat kb_currentTopInset ;
@ property ( nonatomic , assign ) CGFloat kb_currentBottomInset ;
@ end
@ implementation KBKeyboardView
@@ -82,11 +87,23 @@ static const CGFloat kKBLettersRow2EdgeSpacerMultiplier = 0.5;
rows = @ [ [ KBKeyboardRowConfig new ] , [ KBKeyboardRowConfig new ] ,
[ KBKeyboardRowConfig new ] , [ KBKeyboardRowConfig new ] ] ;
}
[ self kb_rebuildRowContainersForRows : rows ] ;
CGFloat rowSpacing = [ self kb_rowSpacingForLayout : layout ] ;
CGFloat topInset = [ self kb_topInsetForLayout : layout ] ;
CGFloat bottomInset = [ self kb_bottomInsetForLayout : layout ] ;
self . kb_currentRowSpacing = rowSpacing ;
self . kb_currentTopInset = topInset ;
self . kb_currentBottomInset = bottomInset ;
[ self kb_rebuildRowContainersForRows : rows
rowSpacing : rowSpacing
topInset : topInset
bottomInset : bottomInset ] ;
}
// / 根 据 行 配 置 数 组 , 动 态 创 建 / 重 建 行 容 器 ( 支 持 4 行 、 5 行 等 任 意 行 数 )
- ( void ) kb_rebuildRowContainersForRows : ( NSArray < KBKeyboardRowConfig * > * ) rowConfigs {
- ( void ) kb_rebuildRowContainersForRows : ( NSArray < KBKeyboardRowConfig * > * ) rowConfigs
rowSpacing : ( CGFloat ) rowSpacing
topInset : ( CGFloat ) topInset
bottomInset : ( CGFloat ) bottomInset {
// 移 除 旧 的 行 容 器
for ( UIView * row in self . rowViews ) {
[ row removeFromSuperview ] ;
@@ -97,9 +114,6 @@ static const CGFloat kKBLettersRow2EdgeSpacerMultiplier = 0.5;
if ( rowCount = = 0 ) return ;
KBKeyboardLayoutConfig * config = [ self kb_layoutConfig ] ;
CGFloat rowSpacing = [ self kb_metricValue : config . metrics . rowSpacing fallback : nil defaultValue : 8.0 ] ;
CGFloat topInset = [ self kb_metricValue : config . metrics . topInset fallback : nil defaultValue : 8.0 ] ;
CGFloat bottomInset = [ self kb_metricValue : config . metrics . bottomInset fallback : nil defaultValue : 6.0 ] ;
UIView * firstRow = nil ;
UIView * previousRow = nil ;
@@ -144,6 +158,9 @@ static const CGFloat kKBLettersRow2EdgeSpacerMultiplier = 0.5;
[ self . backspaceHandler bindDeleteButton : nil showClearLabel : NO ] ;
KBKeyboardLayout * layout = [ self kb_currentLayout ] ;
CGFloat rowSpacing = [ self kb_rowSpacingForLayout : layout ] ;
CGFloat topInset = [ self kb_topInsetForLayout : layout ] ;
CGFloat bottomInset = [ self kb_bottomInsetForLayout : layout ] ;
NSLog ( @ "[KBKeyboardView] reloadKeys: layoutName=%@ rows=%lu shiftRows=%lu shiftOn=%d" ,
self . currentLayoutJsonId , ( unsigned long ) layout . rows . count , ( unsigned long ) layout . shiftRows . count , self . shiftOn ) ;
@@ -159,8 +176,18 @@ static const CGFloat kKBLettersRow2EdgeSpacerMultiplier = 0.5;
( unsigned long ) rows . count , ( unsigned long ) self . rowViews . count ) ;
// 行 数 变 化 时 ( 如 从 4 行 布 局 切 到 5 行 注 音 布 局 ) , 重 建 行 容 器
if ( rows . count >= 4 && rows . count ! = self . rowViews . count ) {
[ self kb_rebuildRowContainersForRo ws: rows ] ;
if ( rows . count >= 4 &&
( rows . count ! = self . rowVie ws. count ||
fabs ( self . kb_currentRowSpacing - rowSpacing ) > 0.1 ||
fabs ( self . kb_currentTopInset - topInset ) > 0.1 ||
fabs ( self . kb_currentBottomInset - bottomInset ) > 0.1 ) ) {
self . kb_currentRowSpacing = rowSpacing ;
self . kb_currentTopInset = topInset ;
self . kb_currentBottomInset = bottomInset ;
[ self kb_rebuildRowContainersForRows : rows
rowSpacing : rowSpacing
topInset : topInset
bottomInset : bottomInset ] ;
}
// 移 除 旧 按 钮
@@ -1003,6 +1030,24 @@ edgeSpacerMultiplier:(CGFloat)edgeSpacerMultiplier {
return self . layoutConfig ;
}
- ( CGFloat ) kb_rowSpacingForLayout : ( KBKeyboardLayout * ) layout {
KBKeyboardLayoutConfig * config = [ self kb_layoutConfig ] ;
NSNumber * layoutSpacing = layout . rowSpacing ;
return [ self kb_metricValue : layoutSpacing fallback : config . metrics . rowSpacing defaultValue : 8.0 ] ;
}
- ( CGFloat ) kb_topInsetForLayout : ( KBKeyboardLayout * ) layout {
KBKeyboardLayoutConfig * config = [ self kb_layoutConfig ] ;
NSNumber * layoutInset = layout . topInset ;
return [ self kb_metricValue : layoutInset fallback : config . metrics . topInset defaultValue : 8.0 ] ;
}
- ( CGFloat ) kb_bottomInsetForLayout : ( KBKeyboardLayout * ) layout {
KBKeyboardLayoutConfig * config = [ self kb_layoutConfig ] ;
NSNumber * layoutInset = layout . bottomInset ;
return [ self kb_metricValue : layoutInset fallback : config . metrics . bottomInset defaultValue : 6.0 ] ;
}
- ( KBKeyboardLayout * ) kb_layoutForName : ( NSString * ) name {
return [ [ self kb_layoutConfig ] layoutForName : name ] ;
}