Commit d8d14243 authored by Peter Steffey's avatar Peter Steffey Committed by Mehdi Achour

feat: add getFontScale() to get device font scale (#278)

parent 9bce15f3
......@@ -195,6 +195,7 @@ var DeviceInfo = require('react-native-device-info');
| [getDeviceLocale()](#getdevicelocale) | `string` | ✅ | ✅ | ✅ | 0.7.0 |
| [getDeviceName()](#getdevicename) | `string` | ✅ | ✅ | ✅ | ? |
| [getFirstInstallTime()](#getfirstinstalltime) | `number` | ❌ | ✅ | ❌ | 0.12.0 |
| [getFontScale()](#getfontscale) | `number` | ✅ | ✅ | ❌ | `next` |
| [getFreeDiskStorage()](#getfreediskstorage) | `number` | ✅ | ✅ | ❌ | `next` |
| [getIPAddress()](#getipaddress) | `Promise<string>` | ❌ | ✅ | ❌ | 0.12.0 |
| [getInstanceID()](#getinstanceid) | `string` | ❌ | ✅ | ❌ | ? |
......@@ -391,6 +392,20 @@ const firstInstallTime = DeviceInfo.getFirstInstallTime();
---
### getFontScale()
Gets the device font scale.
The font scale is the ratio of the current system font to the "normal" font size, so if normal text is 10pt and the system font is currently 15pt, the font scale would be 1.5
This can be used to determine if accessability settings has been changed for the device; you may want to re-layout certain views if the font scale is significantly larger ( > 2.0 )
**Examples**
```js
const fontScale = DeviceInfo.getFontScale(); // 1.2
```
---
### getFreeDiskStorage()
Gets available storage size, in bytes.
......
......@@ -200,6 +200,28 @@ RCT_EXPORT_MODULE(RNDeviceInfo)
return [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
}
// Font scales based on font sizes from https://developer.apple.com/ios/human-interface-guidelines/visual-design/typography/
- (NSNumber*) fontScale
{
float fontScale = 1.0;
NSString *contentSize = [UIApplication sharedApplication].preferredContentSizeCategory;
if ([contentSize isEqual: @"UICTContentSizeCategoryXS"]) fontScale = 0.82;
else if ([contentSize isEqual: @"UICTContentSizeCategoryS"]) fontScale = 0.88;
else if ([contentSize isEqual: @"UICTContentSizeCategoryM"]) fontScale = 0.95;
else if ([contentSize isEqual: @"UICTContentSizeCategoryL"]) fontScale = 1.0;
else if ([contentSize isEqual: @"UICTContentSizeCategoryXL"]) fontScale = 1.12;
else if ([contentSize isEqual: @"UICTContentSizeCategoryXXL"]) fontScale = 1.23;
else if ([contentSize isEqual: @"UICTContentSizeCategoryXXXL"]) fontScale = 1.35;
else if ([contentSize isEqual: @"UICTContentSizeCategoryAccessibilityM"]) fontScale = 1.64;
else if ([contentSize isEqual: @"UICTContentSizeCategoryAccessibilityL"]) fontScale = 1.95;
else if ([contentSize isEqual: @"UICTContentSizeCategoryAccessibilityXL"]) fontScale = 2.35;
else if ([contentSize isEqual: @"UICTContentSizeCategoryAccessibilityXXL"]) fontScale = 2.76;
else if ([contentSize isEqual: @"UICTContentSizeCategoryAccessibilityXXXL"]) fontScale = 3.12;
return [NSNumber numberWithFloat: fontScale];
}
- (bool) is24Hour
{
NSString *format = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
......@@ -264,6 +286,7 @@ RCT_EXPORT_MODULE(RNDeviceInfo)
@"isEmulator": @(self.isEmulator),
@"isTablet": @(self.isTablet),
@"is24Hour": @(self.is24Hour),
@"fontScale": self.fontScale,
@"totalMemory": @(self.totalMemory),
@"totalDiskCapacity": @(self.totalDiskCapacity),
@"freeDiskStorage": @(self.freeDiskStorage),
......
......@@ -94,6 +94,10 @@ public class RNDeviceModule extends ReactContextBaseJavaModule {
return layout == Configuration.SCREENLAYOUT_SIZE_LARGE || layout == Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
private float fontScale() {
return getReactApplicationContext().getResources().getConfiguration().fontScale;
}
private Boolean is24Hour() {
return android.text.format.DateFormat.is24HourFormat(this.reactContext.getApplicationContext());
}
......@@ -206,6 +210,7 @@ public class RNDeviceModule extends ReactContextBaseJavaModule {
constants.put("timezone", TimeZone.getDefault().getID());
constants.put("isEmulator", this.isEmulator());
constants.put("isTablet", this.isTablet());
constants.put("fontScale", this.fontScale());
constants.put("is24Hour", this.is24Hour());
if (getCurrentActivity() != null &&
(getCurrentActivity().checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED ||
......
......@@ -21,6 +21,7 @@ export function getTimezone(): string;
export function getInstanceID(): string;
export function isEmulator(): boolean;
export function isTablet(): boolean;
export function getFontScale(): number;
export function is24Hour(): boolean;
export function isPinOrFingerprintSet(): (
cb: (isPinOrFingerprintSet: boolean) => void
......
......@@ -71,6 +71,9 @@ module.exports = {
getTimezone: function() {
return RNDeviceInfo.timezone;
},
getFontScale: function() {
return RNDeviceInfo.fontScale;
},
isEmulator: function() {
return RNDeviceInfo.isEmulator;
},
......
......@@ -21,6 +21,7 @@ declare module.exports: {
getInstanceID: () => string,
isEmulator: () => boolean,
isTablet: () => boolean,
getFontScale: () => number,
is24Hour: () => boolean,
isPinOrFingerprintSet: () => (
cb: (isPinOrFingerprintSet: boolean) => void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment