Skip to content

Commit 880c260

Browse files
committed
feat: Paint letterSpacing support
1 parent 25311fb commit 880c260

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
{
22
"uses": [
3-
"java.nio:ByteOrder*","com.akylas.canvas*:*", "android.graphics:Canvas*", "android.graphics:Matrix*", "android.graphics:DashPathEffect*", "android.graphics:LinearGradient*", "android.graphics:RadialGradient*", "android.graphics:PorterDuff*", "android.graphics:PorterDuffColorFilter*", "android.graphics:PorterDuffXfermode*", "android.graphics:PointF*", "android.text:Layout*", "android.text.Layout:Alignment*", "android.graphics:PathEffect*", "android.graphics:DrawFilter*", "android.graphics.Paint*:*", "android.graphics:Paint*", "android.graphics:RecordingCanvas*", "android.graphics:Path*", "android.graphics.Path*", "android.graphics:Rect*", "android.graphics:RectF*", "android.graphics:Shader*", "android.graphics.Shader*:*", "android.graphics:Region*", "android.graphics.Region:Op*"
3+
"java.nio:ByteOrder*",
4+
"com.akylas.canvas*:*",
5+
"android.graphics:Canvas*",
6+
"android.graphics:Matrix*",
7+
"android.graphics:DashPathEffect*",
8+
"android.graphics:LinearGradient*",
9+
"android.graphics:RadialGradient*",
10+
"android.graphics:PorterDuff*",
11+
"android.graphics:PorterDuffColorFilter*",
12+
"android.graphics:PorterDuffXfermode*",
13+
"android.graphics:PointF*",
14+
"android.text:Layout*",
15+
"android.text.Layout:Alignment*",
16+
"android.graphics:PathEffect*",
17+
"android.graphics:DrawFilter*",
18+
"android.graphics.Paint*:*",
19+
"android.graphics:Paint*",
20+
"android.graphics:RecordingCanvas*",
21+
"android.graphics:Path*",
22+
"android.graphics.Path*",
23+
"android.graphics:Rect*",
24+
"android.graphics:RectF*",
25+
"android.graphics:Shader*",
26+
"android.graphics.Shader*:*",
27+
"android.graphics:Region*",
28+
"android.graphics.Region:Op*"
429
]
530
}

src-native/android/build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ buildscript {
44
// ext.kotlin_version = '1.2.70'
55
repositories {
66
google()
7-
jcenter()
8-
}
7+
}
98
dependencies {
109
classpath 'com.android.tools.build:gradle:7.+'
1110
}
@@ -14,8 +13,7 @@ buildscript {
1413
allprojects {
1514
repositories {
1615
google()
17-
jcenter()
18-
}
16+
}
1917
}
2018
task clean(type: Delete) {
2119
delete rootProject.buildDir

src/canvas.android.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CSSType, Color, Font, ImageSource, View } from '@nativescript/core';
1+
import { CSSType, Color, Font, ImageSource, View, Device } from '@nativescript/core';
22
import { FontStyle, FontWeight } from '@nativescript/core/ui/styling/font';
33
import { android as androidApp } from '@nativescript/core/application';
44
import lazy from '@nativescript/core/utils/lazy';
@@ -9,6 +9,7 @@ import { CanvasBase, hardwareAcceleratedProperty } from './canvas.common';
99
declare global {
1010
const __runtimeVersion: string;
1111
}
12+
const sdkVersion = lazy(() => parseInt(Device.sdkVersion));
1213

1314
export * from './canvas.common';
1415
export {
@@ -267,6 +268,8 @@ class Canvas extends ProxyClass<android.graphics.Canvas> {
267268
} else if (methodName === 'drawView') {
268269
drawViewOnCanvas(native, args[0], args[1]);
269270
return true;
271+
} else if (methodName === 'setLetterSpacing' && sdkVersion() < 21) {
272+
return true;
270273
}
271274
}
272275
getImage() {

src/canvas.ios.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ export class Paint implements IPaint {
857857
antiAlias = true;
858858
dither;
859859
alpha = 255;
860+
letterSpacing: number;
860861
currentContext: any;
861862
shadowLayer?: {
862863
radius: number;
@@ -1096,6 +1097,16 @@ export class Paint implements IPaint {
10961097
this.mFont = this.font.withFontSize(textSize);
10971098
this.mTextAttribs = null;
10981099
}
1100+
getLetterSpacing() {
1101+
return this.letterSpacing;
1102+
}
1103+
setLetterSpacing(spacing) {
1104+
if (this.letterSpacing === spacing) {
1105+
return;
1106+
}
1107+
this.letterSpacing = spacing;
1108+
this.mTextAttribs = null;
1109+
}
10991110
get color(): Color | number | string {
11001111
return this.mColor;
11011112
}
@@ -1219,12 +1230,15 @@ export class Paint implements IPaint {
12191230
}
12201231
getDrawTextAttribs() {
12211232
if (!this.mTextAttribs) {
1222-
this.mTextAttribs = NSMutableDictionary.dictionaryWithObjectsForKeys([this.getUIFont()], [NSFontAttributeName]);
1233+
const iosFont = this.getUIFont();
1234+
this.mTextAttribs = NSMutableDictionary.dictionaryWithObjectsForKeys([iosFont], [NSFontAttributeName]);
12231235
const color = this.getUIColor();
1224-
this.mTextAttribs = NSMutableDictionary.dictionaryWithObjectsForKeys([this.getUIFont()], [NSFontAttributeName]);
12251236
if (color) {
12261237
this.mTextAttribs.setObjectForKey(color, NSForegroundColorAttributeName);
12271238
}
1239+
if (this.letterSpacing !== undefined) {
1240+
this.mTextAttribs.setObjectForKey(this.letterSpacing * iosFont.pointSize, NSKernAttributeName);
1241+
}
12281242
if (this.align === Align.CENTER) {
12291243
const paragraphStyle = NSMutableParagraphStyle.new();
12301244
paragraphStyle.alignment = NSTextAlignment.Center;
@@ -1925,7 +1939,6 @@ export class Canvas implements ICanvas {
19251939
x = params[1];
19261940
y = params[2];
19271941
}
1928-
// const attribs = paint.getDrawTextAttribs();
19291942
// const nsstring = NSString.stringWithUTF8String(text);
19301943
// const attrString = NSAttributedString.alloc().initWithStringAttributes(text.replace(/\n/g, ' '), attribs);
19311944
let offsetx = x;
@@ -1948,7 +1961,10 @@ export class Canvas implements ICanvas {
19481961
}
19491962
const font = paint.getUIFont();
19501963
const color = paint.getUIColor();
1951-
if (text instanceof NSAttributedString) {
1964+
if (paint.letterSpacing !== undefined) {
1965+
const attribs = paint.getDrawTextAttribs();
1966+
UIDrawingText.drawStringXYWithAttributes(text, offsetx, offsety - font.ascender, attribs);
1967+
} else if (text instanceof NSAttributedString) {
19521968
UIDrawingText.drawAttributedStringXYFontColor(text, offsetx, offsety - font.ascender, font, color);
19531969
} else {
19541970
UIDrawingText.drawStringXYFontColor(text, offsetx, offsety - font.ascender, font, color);

0 commit comments

Comments
 (0)