22#import " NSCFontDescriptors.h"
33#import " NSCFontFaceSet.h"
44#import " NSCFontResolver.h"
5+ #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
6+ #import < UIKit/UIKit.h>
7+ #endif
58
69@interface NSCFontFace ()
710@property (nonatomic , copy , nullable ) NSString *localOrRemoteSource;
811@property (nonatomic , copy , nullable ) NSString *fontPath;
912@property (nonatomic , assign ) BOOL reloadPending;
13+ #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
14+ @property (nonatomic , strong , nullable , readwrite ) UIFont *uiFont;
15+ #endif
1016@end
1117
1218@implementation NSCFontFace
@@ -84,8 +90,8 @@ - (void)setWeight:(NSCFontWeight)weight {
8490 [self _scheduleReloadIfNeeded ];
8591}
8692
87- - (NSCFontStyle)style { return self.fontDescriptors .style ; }
88- - (void )setStyle : (NSCFontStyle)style {
93+ - (NSCFontStyle * )style { return self.fontDescriptors .style ; }
94+ - (void )setStyle : (NSCFontStyle * )style {
8995 self.fontDescriptors .style = style;
9096 [self _scheduleReloadIfNeeded ];
9197}
@@ -248,6 +254,48 @@ - (nullable NSData *)rawData {
248254 return nil ;
249255}
250256
257+ #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
258+
259+ // Apply italic/oblique trait to a base UIFont via descriptor.
260+ - (UIFont *)_applyStyleTraitsToFont : (UIFont *)base size : (CGFloat)size {
261+ NSCFontStyleType styleType = self.fontDescriptors .style .type ;
262+ if (styleType == NSCFontStyleTypeItalic || styleType == NSCFontStyleTypeOblique) {
263+ UIFontDescriptor *desc = [base.fontDescriptor
264+ fontDescriptorWithSymbolicTraits: UIFontDescriptorTraitItalic];
265+ if (desc) {
266+ UIFont *italic = [UIFont fontWithDescriptor: desc size: size];
267+ if (italic) return italic;
268+ }
269+ }
270+ return base;
271+ }
272+
273+ // For system/generic families — re-create UIFont from the resolved family name.
274+ // Falls back to a weight-matched system font so we never return nil.
275+ - (UIFont *)_uiFontFromFamily : (NSString *)family size : (CGFloat)size {
276+ UIFont *base = [UIFont fontWithName: family size: size];
277+ if (!base) {
278+ base = [UIFont systemFontOfSize: size weight: NSCUIFontWeight(self .fontDescriptors.weight)];
279+ }
280+ return [self _applyStyleTraitsToFont: base size: size];
281+ }
282+
283+ // For custom registered fonts — bridge through the PostScript name.
284+ - (nullable UIFont *)_uiFontFromCGFont : (CGFontRef)cgFont size : (CGFloat)size {
285+ CFStringRef psRef = CGFontCopyPostScriptName (cgFont);
286+ if (!psRef) return nil ;
287+ NSString *psName = (__bridge_transfer NSString *)psRef;
288+ UIFont *base = [UIFont fontWithName: psName size: size];
289+ if (!base) return nil ;
290+ return [self _applyStyleTraitsToFont: base size: size];
291+ }
292+
293+ - (nullable UIFont *)uiFontWithSize : (CGFloat)size {
294+ return [self .uiFont fontWithSize: size];
295+ }
296+
297+ #endif
298+
251299- (void )_loadInternalWithCompletion : (void (^)(NSString * _Nullable))callback {
252300 NSString *family = self.fontDescriptors .family ;
253301 NSString *src = self.localOrRemoteSource ;
@@ -257,6 +305,9 @@ - (void)_loadInternalWithCompletion:(void (^)(NSString * _Nullable))callback {
257305 CGFontRef font = [[NSCFontResolver shared ] registerFontFromData: self .fontData error: &error];
258306 if (font) {
259307 self.font = font;
308+ #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
309+ self.uiFont = [self _uiFontFromCGFont: font size: UIFont.labelFontSize];
310+ #endif
260311 self.status = NSCFontFaceStatusLoaded;
261312 callback (nil );
262313 } else {
@@ -285,6 +336,16 @@ - (void)_loadInternalWithCompletion:(void (^)(NSString * _Nullable))callback {
285336 self->_fontPath = src;
286337 }
287338
339+ #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_MACCATALYST
340+ // Custom source font: bridge via PostScript name.
341+ // System/generic font (no src): re-create from resolved family name.
342+ if (src.length > 0 ) {
343+ self.uiFont = [self _uiFontFromCGFont: font size: UIFont.labelFontSize];
344+ } else {
345+ NSString *resolvedFamily = [[NSCFontResolver shared ] resolveGenericFamily: family];
346+ self.uiFont = [self _uiFontFromFamily: resolvedFamily size: UIFont.labelFontSize];
347+ }
348+ #endif
288349 self.status = NSCFontFaceStatusLoaded;
289350 callback (nil );
290351 }];
0 commit comments