|
1 | 1 | # TypefaceHelper |
2 | 2 |
|
3 | | -[](https://gitter.im/Drivemode/TypefaceHelper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
4 | | -[](https://android-arsenal.com/details/1/1246) |
5 | | -[](https://github.com/Drivemode/TypefaceHelper/blob/master/LICENSE) |
6 | | -[](https://circleci.com/gh/Drivemode/TypefaceHelper/tree/master) |
| 3 | +[](https://github.com/applibgroup/TypefaceHelper/actions/workflows/main.yml) |
| 4 | +[](https://sonarcloud.io/dashboard?id=applibgroup_TypefaceHelper) |
7 | 5 |
|
8 | | -Helper object for injecting typeface into various text views of android. |
| 6 | +Helper object for injecting typeface into various text components of HarmonyOS. |
9 | 7 |
|
10 | 8 | ## Overview |
11 | 9 |
|
12 | | -We can use various custom typefaces asset for any text views(like TextView, Button, RadioButton, EditText, etc.), |
| 10 | +We can use various custom typefaces asset for any text components (like Text, Button, RadioButton, etc.), |
13 | 11 | but there's no way to set the typeface as a styled theme to apply the typeface for overall screens in the app. |
14 | 12 |
|
15 | 13 | This library helps to do it in easy way :) |
16 | 14 |
|
17 | | -And there's also a serious bug that creating typeface from asset resource will cause memory leak ([See this link](https://code.google.com/p/android/issues/detail?id=9904) for more details), |
| 15 | +And there's also a serious bug that creating typeface from asset resource will cause memory leak, |
18 | 16 | this library will take care about this problem as well. |
19 | 17 |
|
20 | | -## How to use |
| 18 | +## Source |
| 19 | +This library has been inspired by https://github.com/Drivemode/TypefaceHelper |
21 | 20 |
|
22 | | -First, put your typeface into `asset` directory. |
| 21 | +## How to use |
23 | 22 |
|
24 | | -In your application class, take care about the helper object lifecycle. |
| 23 | +In your MyApplication class, take care about the helper object lifecycle. |
25 | 24 |
|
26 | 25 | ```java |
27 | | -public class MyApp extends Application { |
28 | | - @Override |
29 | | - public void onCreate() { |
30 | | - super.onCreate(); |
31 | | - |
32 | | - TypefaceHelper.initialize(this); |
33 | | - } |
34 | | - |
35 | | - @Override |
36 | | - public void onTerminate() { |
37 | | - TypefaceHelper.destroy(); |
38 | | - super.onTerminate(); |
39 | | - } |
| 26 | +public abstract class MyApplication extends AbilityPackage { |
| 27 | + @Override |
| 28 | + public void onInitialize() { |
| 29 | + super.onInitialize(); |
| 30 | + TypefaceHelper.initialize(this); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void onEnd() { |
| 35 | + super.onEnd(); |
| 36 | + TypefaceHelper.destroy(); |
| 37 | + } |
40 | 38 | } |
41 | 39 | ``` |
42 | 40 |
|
43 | | -And in your activity, if you would like to set your typeface to a text view, |
| 41 | +And in your MainAbilitySlice, if you would like to set your typeface to a text , |
44 | 42 |
|
45 | 43 | ```java |
46 | | -public class MyActivity extends Activity { |
47 | | - @Override |
48 | | - public void onCreate(Bundle savedInstanceState) { |
49 | | - super.onCreate(savedInstanceState); |
50 | | - setContentView(R.layout.activity_main); |
51 | | - |
52 | | - TextView hello = (TextView) findViewById(R.id.hello_world); |
53 | | - TypefaceHelper.getInstance().setTypeface(hello, "font/font_file.ttf"); |
54 | | - } |
| 44 | + |
| 45 | +public class MainAbilitySlice extends AbilitySlice { |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onStart(Intent intent) { |
| 49 | + super.onStart(intent); |
| 50 | + super.setUIContent(ResourceTable.Layout_ability_main); |
| 51 | + |
| 52 | + Text hello = (Text) findComponentById(ResourceTable.Id_hello_world); |
| 53 | + TypefaceHelper.getInstance().setTypeface(hello, "font_file.ttf"); |
| 54 | + } |
55 | 55 | } |
| 56 | + |
56 | 57 | ``` |
57 | 58 |
|
58 | | -You can also set your typeface for all text views that belong to a specific view group just like this. |
| 59 | +You can also set your typeface for all text that belong to a specific ComoponentConatainer just like this. |
59 | 60 |
|
60 | 61 | ```java |
61 | | -public class MyActivity extends Activity { |
62 | | - @Override |
63 | | - public void onCreate(Bundle savedInstanceState) { |
64 | | - super.onCreate(savedInstanceState); |
65 | | - setContentView(R.layout.activity_main); |
66 | | - |
67 | | - LinearLayout container = (LinearLayout) findViewById(R.id.text_container); |
68 | | - TypefaceHelper.getInstance().setTypeface(container, "font/font_file.ttf"); |
69 | | - } |
| 62 | +public class MainAbilitySlice extends AbilitySlice { |
| 63 | + @Override |
| 64 | + public void onStart(Intent intent) { |
| 65 | + super.onStart(intent); |
| 66 | + super.setUIContent(ResourceTable.Layout_ability_main); |
| 67 | + |
| 68 | + DirectionalLayout container = (DirectionalLayout) findComponentById(ResourceTable.Id_container); |
| 69 | + TypefaceHelper.getInstance().setTypeface(container, "font_file.ttf"); |
| 70 | + } |
70 | 71 | } |
71 | 72 | ``` |
72 | 73 |
|
73 | | -If you want to apply the typeface for all text views under the activity layout, |
| 74 | +If you want to apply the typeface for all text under the AbilitySlice, |
74 | 75 |
|
75 | 76 | ```java |
76 | | -public class MyActivity extends Activity { |
77 | | - @Override |
78 | | - public void onCreate(Bundle savedInstanceState) { |
79 | | - super.onCreate(savedInstanceState); |
80 | | - setContentView(TypefaceHelper.getInstance().setTypeface(this, R.layout.activity_main, "font/font_file.ttf")); |
81 | | - } |
| 77 | +public class MainAbilitySlice extends AbilitySlice { |
| 78 | + @Override |
| 79 | + public void onStart(Intent intent) { |
| 80 | + super.onStart(intent); |
| 81 | + super.setUIContent(ResourceTable.Layout_ability_main); |
| 82 | + |
| 83 | + TypefaceHelper.getInstance().setTypeface(this, "font_file.ttf"); |
| 84 | + } |
82 | 85 | } |
| 86 | + |
83 | 87 | ``` |
84 | 88 |
|
85 | 89 | Nice and easy! |
86 | 90 |
|
87 | 91 | You can apply the typeface to your whole window like this. |
88 | 92 |
|
89 | 93 | ```java |
90 | | -public class MyActivity extends Activity { |
91 | | - @Override |
92 | | - public void onCreate(Bundle savedInstanceState) { |
93 | | - super.onCreate(savedInstanceState); |
94 | | - setContentView(R.layout.layout_activity_main); |
95 | | - TypefaceHelper.getInstance().setTypeface(this, "font/font_file.ttf"); |
96 | | - } |
| 94 | + |
| 95 | +public class MainAbilitySlice extends AbilitySlice { |
| 96 | + @Override |
| 97 | + public void onStart(Intent intent) { |
| 98 | + super.onStart(intent); |
| 99 | + super.setUIContent(ResourceTable.Layout_ability_main); |
| 100 | + |
| 101 | + TypefaceHelper.getInstance().setTypeface(this, "font_file.ttf"); |
| 102 | + } |
97 | 103 | } |
| 104 | + |
98 | 105 | ``` |
99 | 106 |
|
100 | 107 | And... you can also pass the font name as a string resource id: |
101 | 108 |
|
102 | 109 | ```java |
103 | | -public class MyActivity extends Activity { |
104 | | - @Override |
105 | | - public void onCreate(Bundle savedInstanceState) { |
106 | | - super.onCreate(savedInstanceState); |
107 | | - setContentView(R.layout.layout_activity_main); |
108 | | - TypefaceHelper.getInstance().setTypeface(this, R.string.font_primary); |
109 | | - } |
| 110 | + |
| 111 | + public class MainAbilitySlice extends AbilitySlice { |
| 112 | + @Override |
| 113 | + public void onStart(Intent intent) { |
| 114 | + super.onStart(intent); |
| 115 | + super.setUIContent(ResourceTable.Layout_ability_main); |
| 116 | + |
| 117 | + TypefaceHelper.getInstance().setTypeface(this, Resourcetable.String_font_primary); |
| 118 | + } |
110 | 119 | } |
111 | 120 | ``` |
112 | 121 |
|
113 | | -## Download |
114 | 122 |
|
115 | | -Gradle: |
116 | 123 |
|
| 124 | + |
| 125 | + |
| 126 | +## Integration |
| 127 | + |
| 128 | +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. |
| 129 | +```java |
| 130 | + implementation project(path: ':TypefaceHelper') |
| 131 | +``` |
| 132 | +2. 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. |
| 133 | + ```java |
| 134 | + implementation fileTree(dir: 'libs', include: ['*.har']) |
117 | 135 | ``` |
118 | | -compile 'com.drivemode:TypefaceHelper:1.2.0@aar' |
| 136 | +3. For using TypefaceHelper module from a remote repository in separate application, add the below dependencies in entry/build.gradle file. |
| 137 | +```java |
| 138 | +implementation 'dev.applibgroup:TypefaceHelper:1.0.0' |
119 | 139 | ``` |
120 | 140 |
|
121 | 141 | ## License |
|
0 commit comments