11using System ;
22using System . Collections . Concurrent ;
3+ using Foundation ;
34using Microsoft . Extensions . Logging ;
45using UIKit ;
56
67namespace Microsoft . Maui
78{
89 /// <inheritdoc/>
9- public class FontManager : IFontManager
10+ public class FontManager : IFontManager , IDisposable
1011 {
1112 // UIFontWeight[Constant] is internal in Xamarin.iOS but the convertion from
1213 // the public (int-based) enum is not helpful in this case.
@@ -28,6 +29,7 @@ public class FontManager : IFontManager
2829 readonly IFontRegistrar _fontRegistrar ;
2930 readonly IServiceProvider ? _serviceProvider ;
3031
32+ NSObject ? _contentSizeCategoryObserver ;
3133 UIFont ? _defaultFont ;
3234
3335 /// <summary>
@@ -44,7 +46,7 @@ public FontManager(IFontRegistrar fontRegistrar, IServiceProvider? serviceProvid
4446 // When the preferred content size category changes (Dynamic Type),
4547 // clear the font cache so subsequent requests create new fonts
4648 // with the current content size category scaling.
47- UIApplication . Notifications . ObserveContentSizeCategoryChanged ( ( sender , args ) => _fonts . Clear ( ) ) ;
49+ _contentSizeCategoryObserver = UIApplication . Notifications . ObserveContentSizeCategoryChanged ( ( sender , args ) => _fonts . Clear ( ) ) ;
4850 }
4951
5052 /// <inheritdoc/>
@@ -188,6 +190,12 @@ UIFont ApplyScaling(Font font, UIFont uiFont)
188190 }
189191 }
190192
193+ public void Dispose ( )
194+ {
195+ _contentSizeCategoryObserver ? . Dispose ( ) ;
196+ _contentSizeCategoryObserver = null ;
197+ }
198+
191199 string ? CleanseFontName ( string fontName )
192200 {
193201 // First check Alias
0 commit comments