Skip to content

Helper object for injecting typeface into various text views of HMOS.

License

Notifications You must be signed in to change notification settings

YashDaga17/TypefaceHelper

 
 

Repository files navigation

TypefaceHelper

Build Quality Gate Status

Helper object for injecting typeface into various text components of HarmonyOS.

Overview

We can use various custom typefaces asset for any text components (like Text, Button, RadioButton, etc.), but there's no way to set the typeface as a styled theme to apply the typeface for overall screens in the app.

This library helps to do it in easy way :)

And there's also a serious bug that creating typeface from asset resource will cause memory leak, this library will take care about this problem as well.

Source

This library has been inspired by https://github.com/Drivemode/TypefaceHelper

How to use

In your MyApplication class, take care about the helper object lifecycle.

public abstract class MyApplication extends AbilityPackage {
    @Override
    public void onInitialize() {
        super.onInitialize();
        TypefaceHelper.initialize(this);
    }

    @Override
    public void onEnd() {
        super.onEnd();
        TypefaceHelper.destroy();
    }
}

And in your MainAbilitySlice, if you would like to set your typeface to a text ,

public class MainAbilitySlice extends AbilitySlice {
    
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        
        Text hello = (Text) findComponentById(ResourceTable.Id_hello_world);
        TypefaceHelper.getInstance().setTypeface(hello, "font_file.ttf");
    }
}

You can also set your typeface for all text that belong to a specific ComoponentConatainer just like this.

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        DirectionalLayout container = (DirectionalLayout) findComponentById(ResourceTable.Id_container);
        TypefaceHelper.getInstance().setTypeface(container, "font_file.ttf");
    }
}

If you want to apply the typeface for all text under the AbilitySlice,

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        
        TypefaceHelper.getInstance().setTypeface(this, "font_file.ttf");
    }
}

Nice and easy!

You can apply the typeface to your whole window like this.

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        
        TypefaceHelper.getInstance().setTypeface(this, "font_file.ttf");
    }
}

And... you can also pass the font name as a string resource id:

 
 public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        
        TypefaceHelper.getInstance().setTypeface(this, Resourcetable.String_font_primary);
    }
}

Integration

  1. For using TypefaceHelper module in sample app, include the source code and add the below dependencies in entry/build.gradle to generate hap/support.har.
 implementation project(path: ':TypefaceHelper')
  1. For using TypefaceHelper module in separate application using har file, add the har file in the entry/libs folder and add the dependencies in entry/build.gradle file.
implementation fileTree(dir: 'libs', include: ['*.har'])
  1. For using TypefaceHelper module from a remote repository in separate application, add the below dependencies in entry/build.gradle file.
implementation 'dev.applibgroup:TypefaceHelper:1.0.0'

License

Copyright (C) 2014 Drivemode, Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

And the sample app uses the following font under OFL.

About

Helper object for injecting typeface into various text views of HMOS.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%