Skip to content

Commit 9346a06

Browse files
a7medevHeshamMegid
authored andcommitted
[EP-351] Add Custom Font API (#292)
1 parent 8add95f commit 9346a06

File tree

7 files changed

+68
-0
lines changed

7 files changed

+68
-0
lines changed

android/src/main/java/com/instabug/flutter/modules/InstabugApi.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ public void setCustomBrandingImage(@NonNull String light, @NonNull String dark)
314314
}
315315
}
316316

317+
@Override
318+
public void setFont(@NonNull String font) {
319+
// iOS Only
320+
}
321+
317322
@Override
318323
public void addFileAttachmentWithURL(@NonNull String filePath, @NonNull String fileName) {
319324
final File file = new File(filePath);

example/ios/InstabugSampleTests/InstabugApiTests.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,18 @@ - (void)testSetCustomBrandingImage {
320320
OCMVerify([self.mInstabug setCustomBrandingImage:[OCMArg isKindOfClass:[UIImageAsset class]]]);
321321
}
322322

323+
- (void)testSetFont {
324+
NSString* fontName = @"fonts/OpenSans-Regular.ttf";
325+
UIFont* font = [UIFont fontWithName:@"Open Sans" size:10.0];
326+
FlutterError *error;
327+
328+
OCMStub([self.mApi getFontForAsset:fontName error:&error]).andReturn(font);
329+
330+
[self.api setFontFont:fontName error:&error];
331+
332+
OCMVerify([self.mInstabug setFont:font]);
333+
}
334+
323335
- (void)testAddFileAttachmentWithURL {
324336
NSString *path = @"buggy.txt";
325337
NSString *name = @"Buggy";

ios/Classes/Modules/InstabugApi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ extern void InitInstabugApi(id<FlutterBinaryMessenger> messenger);
55
@interface InstabugApi : NSObject <InstabugHostApi>
66

77
- (UIImage *)getImageForAsset:(NSString *)assetName;
8+
- (UIFont *)getFontForAsset:(NSString *)assetName error:(FlutterError *_Nullable *_Nonnull)error;
89

910
@end

ios/Classes/Modules/InstabugApi.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import <Foundation/Foundation.h>
2+
#import <CoreText/CoreText.h>
23
#import <Flutter/Flutter.h>
34
#import "Instabug.h"
45
#import "InstabugApi.h"
@@ -196,6 +197,35 @@ - (void)reportScreenChangeScreenName:(NSString *)screenName error:(FlutterError
196197
}
197198
}
198199

200+
- (UIFont *)getFontForAsset:(NSString *)assetName error:(FlutterError *_Nullable *_Nonnull)error {
201+
NSString *key = [FlutterDartProject lookupKeyForAsset:assetName];
202+
NSString *path = [[NSBundle mainBundle] pathForResource:key ofType:nil];
203+
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
204+
CFErrorRef fontError;
205+
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef) data);
206+
CGFontRef cgFont = CGFontCreateWithDataProvider(provider);
207+
UIFont *font;
208+
209+
if(!CTFontManagerRegisterGraphicsFont(cgFont, &fontError)){
210+
CFStringRef errorDescription = CFErrorCopyDescription(fontError);
211+
*error = [FlutterError errorWithCode:@"IBGFailedToLoadFont" message:(__bridge NSString *)errorDescription details:nil];
212+
CFRelease(errorDescription);
213+
} else {
214+
NSString *fontName = (__bridge NSString *)CGFontCopyFullName(cgFont);
215+
font = [UIFont fontWithName:fontName size:10.0];
216+
}
217+
218+
if (cgFont) CFRelease(cgFont);
219+
if (provider) CFRelease(provider);
220+
221+
return font;
222+
}
223+
224+
- (void)setFontFont:(NSString *)fontAsset error:(FlutterError *_Nullable *_Nonnull)error {
225+
UIFont *font = [self getFontForAsset:fontAsset error:error];
226+
Instabug.font = font;
227+
}
228+
199229
- (void)addFileAttachmentWithURLFilePath:(NSString *)filePath fileName:(NSString *)fileName error:(FlutterError *_Nullable *_Nonnull)error {
200230
[Instabug addFileAttachmentWithURL:[NSURL URLWithString:filePath]];
201231
}

lib/src/modules/instabug.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,14 @@ class Instabug {
358358
return _host.reportScreenChange(screenName);
359359
}
360360

361+
/// Changes the font of Instabug's UI.
362+
/// [font] The asset path to the font file (e.g. "fonts/Poppins.ttf").
363+
static Future<void> setFont(String font) async {
364+
if (IBGBuildInfo.I.isIOS) {
365+
return _host.setFont(font);
366+
}
367+
}
368+
361369
/// Sets the repro steps mode
362370
/// [mode] repro steps mode
363371
static Future<void> setReproStepsMode(ReproStepsMode reproStepsMode) async {

pigeons/instabug.api.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ abstract class InstabugHostApi {
4545
void reportScreenChange(String screenName);
4646

4747
void setCustomBrandingImage(String light, String dark);
48+
void setFont(String font);
4849

4950
void addFileAttachmentWithURL(String filePath, String fileName);
5051
void addFileAttachmentWithData(Uint8List data, String fileName);

test/instabug_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,17 @@ void main() {
328328
).called(1);
329329
});
330330

331+
test('[setFont] should call host method', () async {
332+
const font = "fonts/OpenSans-Regular.ttf";
333+
when(mBuildInfo.isIOS).thenReturn(true);
334+
335+
await Instabug.setFont(font);
336+
337+
verify(
338+
mHost.setFont(font),
339+
).called(1);
340+
});
341+
331342
test('[addFileAttachmentWithURL] should call host method', () async {
332343
const path = "/opt/android/logs/";
333344
const name = "log.txt";

0 commit comments

Comments
 (0)