DEV Community

HarmonyOS
HarmonyOS

Posted on

Font Size Manager Utility for Accessibility

Read the original article:Font Size Manager Utility for Accessibility

Font Size Manager Utility for Accessibility Support

Requirement Description

The FontSizeManager utility is designed to provide dynamic font sizing capabilities that automatically adapt to accessibility settings. It addresses the need for applications to respond to screen reader activation by scaling font sizes appropriately, ensuring better readability for users with visual impairments.

Background Knowledge

In modern mobile applications, accessibility features like screen readers are crucial for users with visual disabilities. When a screen reader is active, users often benefit from larger text sizes to improve readability. The FontSizeManager leverages the HarmonyOS Accessibility Kit to detect screen reader status and apply appropriate scaling factors to font sizes.

Implementation Steps

  1. Create Base Font Size Configuration: Define standard font sizes for different text types (title, header, body, caption, small)
  2. Integrate Accessibility API: Use the Accessibility Kit to detect screen reader status
  3. Implement Scaling Logic: Apply scaling factors based on accessibility settings
  4. Provide Type-Specific Methods: Create dedicated methods for common font size categories

Code Snippet / Configuration

// utils/FontSizeManager.ts
import { accessibility } from '@kit.AccessibilityKit';

interface GeneratedObjectLiteralInterface_1 {
  title: number;
  header: number;
  body: number;
  caption: number;
  small: number;
}

export class FontSizeManager {
  private baseSizes: GeneratedObjectLiteralInterface_1 = {
    title: 24,
    header: 20,
    body: 16,
    caption: 14,
    small: 12
  };

  public getScaledSize(baseSize: number): number {
    // Get font scale from accessibility (assuming API availability)
    // If direct API not available, use a multiplier based on screen reader state
    const scaleFactor = accessibility.isScreenReaderOpenSync() ? 1.2 : 1.0;
    return Math.round(baseSize * scaleFactor);
  }

  public getTitleSize(): number {
    return this.getScaledSize(this.baseSizes.title);
  }

  public getBodySize(): number {
    return this.getScaledSize(this.baseSizes.body);
  }

  public getCaptionSize(): number {
    return this.getScaledSize(this.baseSizes.caption);
  }
}
Enter fullscreen mode Exit fullscreen mode

Test Results

  • Screen Reader Inactive: All font sizes return base values (title: 24, body: 16, caption: 14)
  • Screen Reader Active: Font sizes scale by 20% (title: 29, body: 19, caption: 17)

Limitations or Considerations

  • Platform Dependency: Relies on HarmonyOS Accessibility Kit availability
  • Fixed Scaling Factor: Uses a hardcoded 1.2x multiplier instead of system font scale preferences
  • Limited Font Categories: Only provides methods for title, body, and caption sizes

Written by Dogan Evci

Top comments (0)