FontSizeCalculator.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // FontSizeCalculator.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 02/01/23.
  6. // Copyright © 2023 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import UIKit
  25. //
  26. // Credits: https://github.com/tbaranes/FittableFontLabel/blob/master/Source/UILabelExtension.swift
  27. //
  28. public class FontSizeCalculator {
  29. static var shared: FontSizeCalculator = FontSizeCalculator()
  30. private enum FontSizeState {
  31. case fit, tooBig, tooSmall
  32. }
  33. /**
  34. Returns a font size of a specific string in a specific font that fits a specific size
  35. - parameter text: The text to use
  36. - parameter maxFontSize: The max font size available
  37. - parameter minFontScale: The min font scale that the font will have
  38. - parameter rectSize: Rect size where the label must fit
  39. - parameter fontColor: The color of font
  40. */
  41. public func fontSizeThatFits(text string: String, maxFontSize: CGFloat = 100, minFontScale: CGFloat = 0.1, rectSize: CGSize) -> CGFloat {
  42. let font = UIFont.systemFont(ofSize: 10)
  43. let maxFontSize = maxFontSize.isNaN ? 100 : maxFontSize
  44. let minFontScale = minFontScale.isNaN ? 0.1 : minFontScale
  45. let minimumFontSize = maxFontSize * minFontScale
  46. guard !string.isEmpty else {
  47. return font.pointSize
  48. }
  49. let constraintSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: rectSize.height)
  50. let calculatedFontSize = binarySearch(font: font, string: string, minSize: minimumFontSize, maxSize: maxFontSize, size: rectSize, constraintSize: constraintSize)
  51. return (calculatedFontSize * 10.0).rounded(.down) / 10.0
  52. }
  53. private func binarySearch(font: UIFont, string: String, minSize: CGFloat, maxSize: CGFloat, size: CGSize, constraintSize: CGSize) -> CGFloat {
  54. let fontSize = (minSize + maxSize) / 2
  55. var attributes: [NSAttributedString.Key: Any] = [:]
  56. attributes[NSAttributedString.Key.font] = font.withSize(fontSize)
  57. let rect = string.boundingRect(with: constraintSize, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
  58. let state = singleLineSizeState(rect: rect, size: size)
  59. // if the search range is smaller than 0.1 of a font size we stop
  60. // returning either side of min or max depending on the state
  61. let diff = maxSize - minSize
  62. guard diff > 0.1 else {
  63. switch state {
  64. case .tooSmall:
  65. return maxSize
  66. default:
  67. return minSize
  68. }
  69. }
  70. switch state {
  71. case .fit: return fontSize
  72. case .tooBig: return binarySearch(font: font, string: string, minSize: minSize, maxSize: fontSize, size: size, constraintSize: constraintSize)
  73. case .tooSmall: return binarySearch(font: font, string: string, minSize: fontSize, maxSize: maxSize, size: size, constraintSize: constraintSize)
  74. }
  75. }
  76. private func singleLineSizeState(rect: CGRect, size: CGSize) -> FontSizeState {
  77. if rect.width >= size.width + 10 && rect.width <= size.width {
  78. return .fit
  79. } else if rect.width > size.width {
  80. return .tooBig
  81. } else {
  82. return .tooSmall
  83. }
  84. }
  85. }