Skip to content

Commit 55ccff2

Browse files
author
farfromrefuge
committed
fix(android): apply a little factor to paint text size so that font size is exactly the same as with TextView
1 parent df81ccc commit 55ccff2

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

packages/ui-canvas/platforms/android/java/com/akylas/canvas/CanvasView.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,13 @@ protected void dispatchDraw(Canvas canvas) {
2828
drawListener.onDraw(canvas);
2929
}
3030
}
31+
32+
private static float FONT_SIZE_FACTOR = -1;
33+
public static float getFontSizeFactor(Context context) {
34+
if (FONT_SIZE_FACTOR == -1) {
35+
FONT_SIZE_FACTOR = android.util.TypedValue.applyDimension(
36+
android.util.TypedValue.COMPLEX_UNIT_SP, 1, context.getResources().getDisplayMetrics());
37+
}
38+
return FONT_SIZE_FACTOR;
39+
}
3140
}

src/ui-canvas/canvas.android.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-duplicate-imports */
2-
import { Application, Color, Device, Font, ImageSource, Utils } from '@nativescript/core';
2+
import { Application, Color, Device, Font, ImageSource, Screen, Utils } from '@nativescript/core';
33
import type { View } from '@nativescript/core';
44
import { arrayToNativeArray } from '@nativescript-community/arraybuffers';
55
import { FontStyleType, FontWeightType } from '@nativescript/core/ui/styling/font-interfaces';
@@ -166,6 +166,7 @@ class Canvas extends ProxyClass<android.graphics.Canvas> {
166166
}
167167
}
168168

169+
let FONT_SIZE_FACTOR;
169170
export class Paint extends ProxyClass<android.graphics.Paint> {
170171
mNative: android.graphics.Paint;
171172
mFontInternal: Font;
@@ -191,13 +192,17 @@ export class Paint extends ProxyClass<android.graphics.Paint> {
191192
return this;
192193
}
193194
handleCustomMethods(target, native, methodName: string, args: any[]): any {
194-
if (methodName === 'setShadowLayer') {
195-
args[3] = createColorParam(args[3]);
196-
} else if (methodName === 'setColor') {
195+
if (methodName === 'setColor') {
197196
if (!args[0]) {
198197
return;
199198
}
200199
args[0] = createColorParam(args[0]);
200+
} else if (methodName === 'setTextSize') {
201+
// we apply a small factor so that font size is the same as in TextView
202+
if (!FONT_SIZE_FACTOR) {
203+
FONT_SIZE_FACTOR = com.akylas.canvas.CanvasView.getFontSizeFactor(Utils.android.getApplicationContext()) / Screen.mainScreen.scale;
204+
}
205+
args[0] *= FONT_SIZE_FACTOR;
201206
} else if (methodName === 'setTypeface') {
202207
if (args[0] instanceof Font) {
203208
this.mFontInternal = args[0];
@@ -210,6 +215,8 @@ export class Paint extends ProxyClass<android.graphics.Paint> {
210215
return true;
211216
} else if (methodName === 'getLetterSpacing' && sdkVersion < 21) {
212217
return 0;
218+
} else if (methodName === 'setShadowLayer') {
219+
args[3] = createColorParam(args[3]);
213220
}
214221
}
215222
setFont(font: Font) {

src/ui-canvas/typings/android.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ declare namespace com {
1818
export class CanvasView extends globalAndroid.view.View {
1919
sizeChangedListener?: SizeChangedListener;
2020
drawListener?: DrawListener;
21+
static getFontSizeFactor(context: globalAndroid.content.Context): number;
2122
}
2223
export class SizeChangedListener {
2324
constructor(impl?: { onSizeChanged(w: number, h: number, oldw: number, oldh: number) });

0 commit comments

Comments
 (0)