Skip to content

Commit cd20a4f

Browse files
committed
adaptive font size of return for iPad floating keyboard
1 parent a7d76de commit cd20a4f

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

uipanel/Key.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ func getShadow(_ colorScheme: ColorScheme) -> Color {
1616
return colorScheme == .dark ? darkShadow : lightShadow
1717
}
1818

19+
func adjustFontSize(_ text: String, _ fontSize: CGFloat, _ widthLimit: CGFloat) -> CGFloat {
20+
let textWidth = getTextWidth(text, fontSize)
21+
return textWidth <= widthLimit ? fontSize : fontSize * widthLimit / textWidth
22+
}
23+
1924
struct KeyView: View {
2025
@Environment(\.colorScheme) var colorScheme
2126
let x: CGFloat
@@ -199,7 +204,8 @@ struct EnterView: View {
199204
.aspectRatio(contentMode: .fit)
200205
.frame(height: height * 0.4)
201206
} else {
202-
Text(label)
207+
Text(label).font(
208+
.system(size: adjustFontSize(label, height * 0.32, (width - columnGap) * 0.95)))
203209
}
204210
}
205211
.keyProperties(

uipanel/WrapLayout.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import UIKit
22

3+
func getTextWidth(_ text: String, _ fontSize: CGFloat) -> CGFloat {
4+
return (text as NSString).size(withAttributes: [
5+
.font: UIFont.systemFont(ofSize: fontSize)
6+
]).width
7+
}
8+
39
func calculateLayout(_ candidates: [String], _ width: CGFloat) -> [Int] {
410
var rowItemCount = [Int]()
511
var currentRowCount = 0
@@ -8,9 +14,7 @@ func calculateLayout(_ candidates: [String], _ width: CGFloat) -> [Int] {
814

915
for candidate in candidates {
1016
let itemWidth = max(
11-
(candidate as NSString).size(withAttributes: [
12-
.font: UIFont.systemFont(ofSize: candidateFontSize)
13-
]).width + 2 * candidateHorizontalPadding, minWidth)
17+
getTextWidth(candidate, candidateFontSize) + 2 * candidateHorizontalPadding, minWidth)
1418
if currentRowWidth + itemWidth <= width {
1519
currentRowWidth += itemWidth
1620
currentRowCount += 1

0 commit comments

Comments
 (0)