Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to get text scaling from Android and IOS settings?
Forum Updated to NodeBB v4.3 + New Features

How to get text scaling from Android and IOS settings?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
iosandroidscaling
2 Posts 1 Posters 450 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sammasas
    wrote on 23 Jan 2024, 08:12 last edited by Sammasas
    #1

    I have to make my Application accessible and I want to use the text scaling that is set in the phones settings. Is there a universal way for Android and IOS to get those settings? I know there is the sp unit for text size that should scale accordingly but I can't find a way to implement that in QT.

    S 1 Reply Last reply 14 Feb 2024, 08:37
    0
    • S Sammasas
      23 Jan 2024, 08:12

      I have to make my Application accessible and I want to use the text scaling that is set in the phones settings. Is there a universal way for Android and IOS to get those settings? I know there is the sp unit for text size that should scale accordingly but I can't find a way to implement that in QT.

      S Offline
      S Offline
      Sammasas
      wrote on 14 Feb 2024, 08:37 last edited by Sammasas
      #2

      @Sammasas
      I didn't find a universal way, but with an QJNIObject and objective c it works now. Java with JNiObject:

      package Klingelball;
      
      import android.content.Context;
      import android.content.res.Configuration;
      import android.util.DisplayMetrics;
      import android.view.WindowManager;
      
      public class AndroidSettings {
         public static float getFontScale(Context context) {
             //return getResources().getConfiguration().fontScale;
             Configuration configuration = context.getResources().getConfiguration();
      
                     // Get the font scale from the configuration
                     float fontScale = configuration.fontScale;
      
                     return fontScale;
         }
      }
      
      

      It's important that the file is under src/mypackage.

      .cpp file:

      #ifdef Q_OS_ANDROID
          QJniObject context = QNativeInterface::QAndroidApplication::context();
          if(QJniObject::isClassAvailable("Klingelball/AndroidSettings")) {
              QJniObject androidSettingsJavaObject = QJniObject("Klingelball/AndroidSettings");
      
              fontScale = new float(androidSettingsJavaObject.callStaticMethod<jfloat>("Klingelball/AndroidSettings", "getFontScale", "(Landroid/content/Context;)F", context.object<jobject>()));
              qDebug() <<"Font scale:" << *fontScale;
              setup_fontAndroid(*fontScale);
      
          }
          else {
              qDebug() << "JAVA CLASS UNAVAIABLE!";
              fontScale = new float(1);
              setup_fontAndroid(*fontScale);
      
          }
      #endif
      

      iOs with objective-c:
      .mm file:

      include "iOSSettings.h"
      #include <UIKit/UIKit.h>
      
      
      int iOSSettings::getPrefferedFont() {
          return [UIFont preferredFontForTextStyle:UIFontTextStyleBody].pointSize;
      }
      

      .h file:

      
      class iOSSettings {
      public:
          static int getPrefferedFont();
      };
      

      .cpp file:

      #ifdef Q_OS_IOS
              
              fontScale = new float(getfontScalefrompointSize(iOSSettings::getPrefferedFont()));
              setup_fontiOS(iOSSettings::getPrefferedFont());
      #endif
      

      iOS returns the preffered Fontsize, I implemented a function that returns a factor based on the fontsize because I had to scale the Icons too.
      Hope this helps somebody :)

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved