2020import UIKit
2121import Core
2222import Kingfisher
23+ import Common
2324
2425struct FaviconsHelper {
25-
26+
27+ private static let tld : TLD = AppDependencyProvider . shared. storageCache. tld
28+
2629 static func loadFaviconSync( forDomain domain: String ? ,
2730 usingCache cacheType: Favicons . CacheType ,
2831 useFakeFavicon: Bool ,
29- preferredFakeFaviconLetter : String ? = nil ,
32+ preferredFakeFaviconLetters : String ? = nil ,
3033 completion: ( ( UIImage ? , Bool ) -> Void ) ? = nil ) {
31-
34+
3235 func complete( _ image: UIImage ? ) {
3336 var fake = false
3437 var resultImage : UIImage ?
@@ -38,7 +41,8 @@ struct FaviconsHelper {
3841 } else if useFakeFavicon, let domain = domain {
3942 fake = true
4043 resultImage = Self . createFakeFavicon ( forDomain: domain,
41- preferredFakeFaviconLetter: preferredFakeFaviconLetter)
44+ backgroundColor: UIColor . forDomain ( domain) ,
45+ preferredFakeFaviconLetters: preferredFakeFaviconLetters)
4246 }
4347 completion ? ( resultImage, fake)
4448 }
@@ -92,12 +96,13 @@ struct FaviconsHelper {
9296 size: CGFloat = 192 ,
9397 backgroundColor: UIColor = UIColor . greyishBrown2,
9498 bold: Bool = true ,
95- preferredFakeFaviconLetter: String ? = nil ) -> UIImage ? {
99+ preferredFakeFaviconLetters: String ? = nil ,
100+ letterCount: Int = 2 ) -> UIImage ? {
96101
97102 let cornerRadius = size * 0.125
98- let fontSize = size * 0.76
99-
100103 let imageRect = CGRect ( x: 0 , y: 0 , width: size, height: size)
104+ let padding = size * 0.16
105+ let labelFrame = CGRect ( x: padding, y: padding, width: imageRect. width - ( 2 * padding) , height: imageRect. height - ( 2 * padding) )
101106
102107 let renderer = UIGraphicsImageRenderer ( size: imageRect. size)
103108 let icon = renderer. image { imageContext in
@@ -106,21 +111,29 @@ struct FaviconsHelper {
106111 context. setFillColor ( backgroundColor. cgColor)
107112 context. addPath ( CGPath ( roundedRect: imageRect, cornerWidth: cornerRadius, cornerHeight: cornerRadius, transform: nil ) )
108113 context. fillPath ( )
109-
110- let label = UILabel ( frame: imageRect)
111- label. font = bold ? UIFont . boldAppFont ( ofSize: fontSize) : UIFont . appFont ( ofSize: fontSize)
114+
115+ let label = UILabel ( frame: labelFrame)
116+ label. numberOfLines = 1
117+ label. adjustsFontSizeToFitWidth = true
118+ label. minimumScaleFactor = 0.1
119+ label. baselineAdjustment = . alignCenters
120+ label. font = bold ? UIFont . boldAppFont ( ofSize: size) : UIFont . appFont ( ofSize: size)
112121 label. textColor = UIColor . white
113122 label. textAlignment = . center
114- label. text = preferredFakeFaviconLetter ?? String ( domain. droppingWwwPrefix ( ) . prefix ( 1 ) . uppercased ( ) )
115- label. sizeToFit ( )
116-
117- context. translateBy ( x: ( imageRect. width - label. bounds. width) / 2.0 ,
118- y: ( imageRect. height - label. font. ascender) / 2.0 - ( label. font. ascender - label. font. capHeight) / 2.0 )
119-
123+
124+ if let prefferedPrefix = preferredFakeFaviconLetters? . prefix ( letterCount) . capitalized {
125+ label. text = prefferedPrefix
126+ } else {
127+ label. text = String ( tld. eTLDplus1 ( domain) ? . prefix ( letterCount) ?? " # " ) . capitalized
128+ }
129+
130+ context. translateBy ( x: padding, y: padding)
131+
120132 label. layer. draw ( in: context)
121133 }
122134
123135 return icon. withRenderingMode ( . alwaysOriginal)
124136 }
137+
125138
126139}
0 commit comments