fix(ios): support for a11y font scale (#10207)

This commit is contained in:
Dimitris-Rafail Katsampas
2023-03-23 00:13:46 +02:00
committed by GitHub
parent ab436dbfe6
commit 95f3772e77
6 changed files with 50 additions and 33 deletions

View File

@ -0,0 +1,17 @@
import * as TKUnit from '../../tk-unit';
import * as helper from '../../ui-helper';
import { Label } from '@nativescript/core';
export function test_native_font_size_with_a11y_font_scale() {
const page = helper.getCurrentPage();
const testView = new Label();
const deviceFontScaleMock = 4.0;
page.content = testView;
testView.style._fontScale = deviceFontScaleMock;
const nativeFontSize = testView.nativeTextViewProtected.font.pointSize;
const expectedNativeFontSize = testView.style.fontInternal.fontSize * deviceFontScaleMock;
TKUnit.assertEqual(nativeFontSize, expectedNativeFontSize, 'View font size does not respect a11y font scaling');
}

View File

@ -31,20 +31,11 @@ export const accessibilityEnabledProperty = new CssProperty<Style, boolean>({
}); });
accessibilityEnabledProperty.register(Style); accessibilityEnabledProperty.register(Style);
const accessibilityHiddenPropertyName = 'accessibilityHidden'; export const accessibilityHiddenProperty = new (global.isIOS ? InheritedCssProperty : CssProperty)({
const accessibilityHiddenCssName = 'a11y-hidden'; name: 'accessibilityHidden',
cssName: 'a11y-hidden',
export const accessibilityHiddenProperty = global.isIOS valueConverter: booleanConverter,
? new InheritedCssProperty({ });
name: accessibilityHiddenPropertyName,
cssName: accessibilityHiddenCssName,
valueConverter: booleanConverter,
})
: new CssProperty({
name: accessibilityHiddenPropertyName,
cssName: accessibilityHiddenCssName,
valueConverter: booleanConverter,
});
accessibilityHiddenProperty.register(Style); accessibilityHiddenProperty.register(Style);
export const accessibilityIdentifierProperty = new Property<View, string>({ export const accessibilityIdentifierProperty = new Property<View, string>({

View File

@ -91,7 +91,8 @@ export class Font extends FontBase {
getUIFont(defaultFont: UIFont): UIFont { getUIFont(defaultFont: UIFont): UIFont {
return getUIFontCached({ return getUIFontCached({
fontFamily: parseFontFamily(this.fontFamily), fontFamily: parseFontFamily(this.fontFamily),
fontSize: this.fontSize || defaultFont.pointSize, // Apply a11y scale and calculate proper font size (avoid applying multiplier to native point size as it's messing calculations)
fontSize: this.fontSize ? this.fontSize * this.fontScale : defaultFont.pointSize,
fontWeight: getNativeFontWeight(this.fontWeight), fontWeight: getNativeFontWeight(this.fontWeight),
fontVariationSettings: this.fontVariationSettings, fontVariationSettings: this.fontVariationSettings,
isBold: this.isBold, isBold: this.isBold,

View File

@ -1329,20 +1329,7 @@ fontFamilyProperty.register(Style);
export const fontScaleProperty = new InheritedCssProperty<Style, number>({ export const fontScaleProperty = new InheritedCssProperty<Style, number>({
name: '_fontScale', name: '_fontScale',
cssName: '_fontScale', cssName: '_fontScale',
affectsLayout: global.isIOS, defaultValue: 1.0,
valueChanged: (target, oldValue, newValue) => {
if (global.isIOS) {
if (target.viewRef['handleFontSize'] === true) {
return;
}
const currentFont = target.fontInternal || Font.default;
if (currentFont.fontScale !== newValue) {
const newFont = currentFont.withFontScale(newValue);
target.fontInternal = Font.equals(Font.default, newFont) ? unsetValue : newFont;
}
}
},
valueConverter: (v) => parseFloat(v), valueConverter: (v) => parseFloat(v),
}); });
fontScaleProperty.register(Style); fontScaleProperty.register(Style);

View File

@ -105,6 +105,9 @@ export class Style extends Observable implements StyleDefinition {
} }
public fontInternal: Font; public fontInternal: Font;
/**
* This property ensures inheritance of a11y scale among views.
*/
public _fontScale: number; public _fontScale: number;
public backgroundInternal: Background; public backgroundInternal: Background;

View File

@ -4,16 +4,15 @@ import { CSSShadow } from '../styling/css-shadow';
// Requires // Requires
import { Font } from '../styling/font'; import { Font } from '../styling/font';
import { TextBaseCommon, textProperty, formattedTextProperty, textAlignmentProperty, textDecorationProperty, textTransformProperty, textShadowProperty, letterSpacingProperty, lineHeightProperty, resetSymbol } from './text-base-common'; import { TextBaseCommon, textProperty, formattedTextProperty, textAlignmentProperty, textDecorationProperty, textTransformProperty, textShadowProperty, letterSpacingProperty, lineHeightProperty, maxLinesProperty, resetSymbol } from './text-base-common';
import { Color } from '../../color'; import { Color } from '../../color';
import { FormattedString } from './formatted-string'; import { FormattedString } from './formatted-string';
import { Span } from './span'; import { Span } from './span';
import { colorProperty, fontInternalProperty, Length } from '../styling/style-properties'; import { colorProperty, fontInternalProperty, fontScaleProperty, Length } from '../styling/style-properties';
import { isString, isNullOrUndefined } from '../../utils/types'; import { isString, isNullOrUndefined } from '../../utils/types';
import { iOSNativeHelper } from '../../utils'; import { iOSNativeHelper } from '../../utils';
import { Trace } from '../../trace'; import { Trace } from '../../trace';
import { CoreTypes } from '../../core-types'; import { CoreTypes } from '../../core-types';
import { maxLinesProperty } from './text-base-common';
export * from './text-base-common'; export * from './text-base-common';
@ -188,7 +187,26 @@ export class TextBase extends TextBaseCommon {
if (!(value instanceof Font) || !this.formattedText) { if (!(value instanceof Font) || !this.formattedText) {
let nativeView = this.nativeTextViewProtected; let nativeView = this.nativeTextViewProtected;
nativeView = nativeView instanceof UIButton ? nativeView.titleLabel : nativeView; nativeView = nativeView instanceof UIButton ? nativeView.titleLabel : nativeView;
nativeView.font = value instanceof Font ? value.getUIFont(nativeView.font) : value;
if (value instanceof Font) {
// Apply a11y font scale if not set
if (value.fontScale !== this.style._fontScale) {
value.fontScale = this.style._fontScale;
}
nativeView.font = value.getUIFont(nativeView.font);
} else {
nativeView.font = value;
}
}
}
[fontScaleProperty.setNative](value: number) {
const nativeView = this.nativeTextViewProtected instanceof UIButton ? this.nativeTextViewProtected.titleLabel : this.nativeTextViewProtected;
const currentFont = this.style.fontInternal || Font.default.withFontSize(nativeView.font.pointSize);
if (currentFont.fontScale !== value) {
const newFont = currentFont.withFontScale(value);
this.style.fontInternal = newFont;
this.requestLayout();
} }
} }