@nativescript-community/text 1.4.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +317 -0
- package/LICENSE +201 -0
- package/README.md +14 -0
- package/index-common.d.ts +41 -0
- package/index-common.js +211 -0
- package/index-common.js.map +1 -0
- package/index.android.d.ts +28 -0
- package/index.android.js +352 -0
- package/index.android.js.map +1 -0
- package/index.d.ts +1 -0
- package/index.ios.d.ts +15 -0
- package/index.ios.js +231 -0
- package/index.ios.js.map +1 -0
- package/index.js +25 -0
- package/index.js.map +1 -0
- package/package.json +34 -0
- package/platforms/android/AndroidManifest.xml +3 -0
- package/platforms/android/include.gradle +4 -0
- package/platforms/android/java/com/nativescript/text/BaselineAdjustedSpan.java +71 -0
- package/platforms/android/java/com/nativescript/text/CustomBackgroundSpan.java +70 -0
- package/platforms/android/java/com/nativescript/text/CustomTypefaceSpan.java +37 -0
- package/platforms/android/java/com/nativescript/text/Font.java +406 -0
- package/platforms/android/java/com/nativescript/text/HeightSpan.java +81 -0
- package/platforms/android/java/com/nativescript/text/HtmlToSpannedConverter.java +692 -0
- package/platforms/android/java/com/nativescript/text/URLSpanNoUnderline.java +24 -0
- package/platforms/android/text.aar +0 -0
- package/typings/DTCoreText.ios.d.ts +22 -0
- package/typings/android.d.ts +34 -0
package/index.ios.js
ADDED
@@ -0,0 +1,231 @@
|
|
1
|
+
import { Color, Font, FormattedString } from '@nativescript/core';
|
2
|
+
import { getTransformedText } from '@nativescript/core/ui/text-base';
|
3
|
+
import { computeBaseLineOffset, getMaxFontSize, textAlignmentConverter } from './index-common';
|
4
|
+
import { LightFormattedString } from './index.android';
|
5
|
+
export * from './index-common';
|
6
|
+
let iOSUseDTCoreText = false;
|
7
|
+
export function enableIOSDTCoreText() {
|
8
|
+
iOSUseDTCoreText = true;
|
9
|
+
}
|
10
|
+
export function usingIOSDTCoreText() {
|
11
|
+
return iOSUseDTCoreText;
|
12
|
+
}
|
13
|
+
export function init() { }
|
14
|
+
function _createNativeAttributedString({ text, familyName = '-apple-system', fontSize, fontWeight, letterSpacing, lineHeight, color, textAlignment, autoFontSizeEnabled = false, fontSizeRatio = 1, }) {
|
15
|
+
const trueFontFamily = familyName
|
16
|
+
? familyName
|
17
|
+
.replace(/'/g, '')
|
18
|
+
.split(',')
|
19
|
+
.map((s) => `'${s}'`)
|
20
|
+
.join(',')
|
21
|
+
: null;
|
22
|
+
// if (iOSUseDTCoreText) {
|
23
|
+
const htmlString = color || familyName || fontSize || fontWeight
|
24
|
+
? `<span style=" ${color ? `color: ${color};` : ''} ${trueFontFamily ? `font-family:${trueFontFamily};` : ''}${fontSize ? `font-size: ${fontSize * fontSizeRatio}px;` : ''}${fontWeight ? `font-weight: ${fontWeight};` : ''}">${text}</span>`
|
25
|
+
: text;
|
26
|
+
// } else {
|
27
|
+
// htmlString =
|
28
|
+
// color || familyName || fontSize || fontWeight
|
29
|
+
// ? `<style>body{ ${color ? `color: ${color};` : ''} ${trueFontFamily ? `font-family:${trueFontFamily};` : ''}${fontSize ? `font-size: ${fontSize * fontSizeRatio}px;` : ''}${
|
30
|
+
// fontWeight ? `font-weight: ${fontWeight};` : ''
|
31
|
+
// }}</style>${text}`
|
32
|
+
// : text;
|
33
|
+
// }
|
34
|
+
const nsString = NSString.stringWithString(htmlString);
|
35
|
+
const nsData = nsString.dataUsingEncodingAllowLossyConversion(NSUnicodeStringEncoding, true);
|
36
|
+
let attrText;
|
37
|
+
if (iOSUseDTCoreText) {
|
38
|
+
// on iOS 13.3 there is bug with the system font
|
39
|
+
// https://github.com/Cocoanetics/DTCoreText/issues/1168
|
40
|
+
const options = {
|
41
|
+
[DTDefaultTextAlignment]: kCTLeftTextAlignment,
|
42
|
+
[DTUseiOS6Attributes]: true,
|
43
|
+
[DTDocumentPreserveTrailingSpaces]: true,
|
44
|
+
};
|
45
|
+
attrText = NSMutableAttributedString.alloc().initWithHTMLDataOptionsDocumentAttributes(nsData, options, null);
|
46
|
+
}
|
47
|
+
else {
|
48
|
+
attrText = NSMutableAttributedString.alloc().initWithDataOptionsDocumentAttributesError(nsData, {
|
49
|
+
[NSDocumentTypeDocumentAttribute]: NSHTMLTextDocumentType,
|
50
|
+
[NSCharacterEncodingDocumentAttribute]: NSUTF8StringEncoding,
|
51
|
+
}, null);
|
52
|
+
}
|
53
|
+
if (autoFontSizeEnabled || iOSUseDTCoreText) {
|
54
|
+
attrText.enumerateAttributesInRangeOptionsUsingBlock({ location: 0, length: attrText.length }, 0, (attributes, range, stop) => {
|
55
|
+
if (!!attributes.valueForKey('DTGUID')) {
|
56
|
+
// We need to remove this attribute or links are not colored right
|
57
|
+
//
|
58
|
+
// @see https://github.com/Cocoanetics/DTCoreText/issues/792
|
59
|
+
attrText.removeAttributeRange('CTForegroundColorFromContext', range);
|
60
|
+
}
|
61
|
+
const font = attributes.valueForKey(NSFontAttributeName);
|
62
|
+
if (!!font) {
|
63
|
+
attrText.addAttributeValueRange('OriginalFontSize', font.pointSize, range);
|
64
|
+
}
|
65
|
+
});
|
66
|
+
}
|
67
|
+
// TODO: letterSpacing should be applied per Span.
|
68
|
+
if (letterSpacing !== undefined && letterSpacing !== 0) {
|
69
|
+
attrText.addAttributeValueRange(NSKernAttributeName, letterSpacing * fontSize, { location: 0, length: attrText.length });
|
70
|
+
}
|
71
|
+
if (lineHeight !== undefined) {
|
72
|
+
const paragraphStyle = NSMutableParagraphStyle.alloc().init();
|
73
|
+
paragraphStyle.lineSpacing = lineHeight;
|
74
|
+
// make sure a possible previously set text alignment setting is not lost when line height is specified
|
75
|
+
paragraphStyle.alignment = textAlignment;
|
76
|
+
// if (this.nativeTextViewProtected instanceof UILabel) {
|
77
|
+
// // make sure a possible previously set line break mode is not lost when line height is specified
|
78
|
+
// paragraphStyle.lineBreakMode = this.nativeTextViewProtected.lineBreakMode;
|
79
|
+
// }
|
80
|
+
attrText.addAttributeValueRange(NSParagraphStyleAttributeName, paragraphStyle, { location: 0, length: attrText.length });
|
81
|
+
}
|
82
|
+
else if (textAlignment !== undefined) {
|
83
|
+
const paragraphStyle = NSMutableParagraphStyle.alloc().init();
|
84
|
+
paragraphStyle.alignment = textAlignment;
|
85
|
+
attrText.addAttributeValueRange(NSParagraphStyleAttributeName, paragraphStyle, { location: 0, length: attrText.length });
|
86
|
+
}
|
87
|
+
return attrText;
|
88
|
+
}
|
89
|
+
export function createNativeAttributedString(data, parent, autoFontSizeEnabled = false, fontSizeRatio = 1) {
|
90
|
+
if (data instanceof FormattedString || data instanceof LightFormattedString) {
|
91
|
+
const ssb = NSMutableAttributedString.new();
|
92
|
+
const maxFontSize = getMaxFontSize(data);
|
93
|
+
const _spanRanges = [];
|
94
|
+
let spanStart = 0;
|
95
|
+
let hasLink = false;
|
96
|
+
data.spans.forEach((s) => {
|
97
|
+
const res = createSpannable(s, parent, undefined, maxFontSize, autoFontSizeEnabled, fontSizeRatio);
|
98
|
+
if (res) {
|
99
|
+
if (s._tappable) {
|
100
|
+
hasLink = true;
|
101
|
+
}
|
102
|
+
_spanRanges.push({
|
103
|
+
location: spanStart,
|
104
|
+
length: res.length,
|
105
|
+
});
|
106
|
+
spanStart += res.length;
|
107
|
+
ssb.appendAttributedString(res);
|
108
|
+
}
|
109
|
+
});
|
110
|
+
parent['nativeTextViewProtected'].selectable = parent['selectable'] === true || hasLink;
|
111
|
+
if (parent._setTappableState) {
|
112
|
+
parent._setTappableState(hasLink);
|
113
|
+
}
|
114
|
+
parent['_spanRanges'] = _spanRanges;
|
115
|
+
return ssb;
|
116
|
+
}
|
117
|
+
if (data.textAlignment && typeof data.textAlignment === 'string') {
|
118
|
+
data.textAlignment = textAlignmentConverter(data.textAlignment);
|
119
|
+
}
|
120
|
+
if (data.color && !(data.color instanceof Color)) {
|
121
|
+
data.color = new Color(data.color);
|
122
|
+
}
|
123
|
+
data['autoFontSizeEnabled'] = autoFontSizeEnabled;
|
124
|
+
data['fontSizeRatio'] = fontSizeRatio;
|
125
|
+
return _createNativeAttributedString(data);
|
126
|
+
}
|
127
|
+
export function createSpannable(span, parentView, parent, maxFontSize, autoFontSizeEnabled = false, fontSizeRatio = 1) {
|
128
|
+
let text = span.text;
|
129
|
+
if (!text || (span.visibility && span.visibility !== 'visible')) {
|
130
|
+
return null;
|
131
|
+
}
|
132
|
+
const attrDict = {};
|
133
|
+
const fontFamily = span.fontFamily;
|
134
|
+
const fontSize = span.fontSize;
|
135
|
+
let realFontSize = fontSize || (parent && parent.fontSize) || (parentView && parentView.fontSize);
|
136
|
+
if (span.relativeSize) {
|
137
|
+
realFontSize = ((parent && parent.fontSize) || (parentView && parentView.fontSize)) * span.relativeSize;
|
138
|
+
}
|
139
|
+
const realMaxFontSize = Math.max(maxFontSize, realFontSize);
|
140
|
+
const fontWeight = span.fontWeight;
|
141
|
+
const fontstyle = span.fontStyle;
|
142
|
+
const textcolor = span.color || (parent && parent.color) || (parentView && parentView.color);
|
143
|
+
const backgroundcolor = span.backgroundColor || (parent && parent.backgroundColor);
|
144
|
+
const textDecorations = span.textDecoration || (parent && parent.textDecoration);
|
145
|
+
const letterSpacing = span.letterSpacing || (parent && parent.letterSpacing);
|
146
|
+
const lineHeight = span.lineHeight || (parent && parent.lineHeight);
|
147
|
+
const textAlignment = span.textAlignment || (parent && parent.textAlignment);
|
148
|
+
const verticaltextalignment = span.verticalTextAlignment;
|
149
|
+
let iosFont;
|
150
|
+
if ((fontWeight && fontWeight !== 'normal') || fontstyle || fontFamily || realFontSize || fontSizeRatio !== 1) {
|
151
|
+
const font = new Font(fontFamily || (parent && parent.fontFamily) || (parentView && parentView.fontFamily), realFontSize * fontSizeRatio, fontstyle || (parent && parent.fontStyle) || (parentView && parentView.fontStyle), fontWeight || (parent && parent.fontWeight) || (parentView && parentView.fontWeight));
|
152
|
+
iosFont = font.getUIFont(UIFont.systemFontOfSize(realFontSize));
|
153
|
+
attrDict[NSFontAttributeName] = iosFont;
|
154
|
+
if (autoFontSizeEnabled) {
|
155
|
+
attrDict['OriginalFontSize'] = realFontSize;
|
156
|
+
}
|
157
|
+
}
|
158
|
+
if (verticaltextalignment && verticaltextalignment !== 'initial' && iosFont) {
|
159
|
+
const ascent = CTFontGetAscent(iosFont);
|
160
|
+
const descent = CTFontGetDescent(iosFont);
|
161
|
+
attrDict[NSBaselineOffsetAttributeName] = -computeBaseLineOffset(verticaltextalignment, -ascent, descent, -iosFont.descender, -iosFont.ascender, fontSize, realMaxFontSize);
|
162
|
+
}
|
163
|
+
// if (span._tappable) {
|
164
|
+
// attrDict[NSLinkAttributeName] = text;
|
165
|
+
// }
|
166
|
+
if (textcolor) {
|
167
|
+
const color = !textcolor || textcolor instanceof Color ? textcolor : new Color(textcolor);
|
168
|
+
if (color) {
|
169
|
+
attrDict[NSForegroundColorAttributeName] = color.ios;
|
170
|
+
}
|
171
|
+
}
|
172
|
+
if (backgroundcolor) {
|
173
|
+
const color = !backgroundcolor || backgroundcolor instanceof Color ? backgroundcolor : new Color(backgroundcolor);
|
174
|
+
if (color) {
|
175
|
+
attrDict[NSBackgroundColorAttributeName] = color.ios;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
if (letterSpacing) {
|
179
|
+
attrDict[NSKernAttributeName] = letterSpacing * iosFont.pointSize;
|
180
|
+
}
|
181
|
+
let paragraphStyle;
|
182
|
+
if (lineHeight !== undefined) {
|
183
|
+
paragraphStyle = NSMutableParagraphStyle.alloc().init();
|
184
|
+
switch (textAlignment) {
|
185
|
+
case 'middle':
|
186
|
+
case 'center':
|
187
|
+
paragraphStyle.alignment = 1 /* Center */;
|
188
|
+
break;
|
189
|
+
case 'right':
|
190
|
+
paragraphStyle.alignment = 2 /* Right */;
|
191
|
+
break;
|
192
|
+
default:
|
193
|
+
paragraphStyle.alignment = 0 /* Left */;
|
194
|
+
break;
|
195
|
+
}
|
196
|
+
paragraphStyle.minimumLineHeight = lineHeight;
|
197
|
+
paragraphStyle.maximumLineHeight = lineHeight;
|
198
|
+
}
|
199
|
+
if (paragraphStyle) {
|
200
|
+
attrDict[NSParagraphStyleAttributeName] = paragraphStyle;
|
201
|
+
}
|
202
|
+
if (textDecorations) {
|
203
|
+
const underline = textDecorations.indexOf('underline') !== -1;
|
204
|
+
if (underline) {
|
205
|
+
attrDict[NSUnderlineStyleAttributeName] = underline;
|
206
|
+
}
|
207
|
+
const strikethrough = textDecorations.indexOf('line-through') !== -1;
|
208
|
+
if (strikethrough) {
|
209
|
+
attrDict[NSStrikethroughStyleAttributeName] = strikethrough;
|
210
|
+
}
|
211
|
+
}
|
212
|
+
if (!(text instanceof NSAttributedString)) {
|
213
|
+
if (!(typeof text === 'string')) {
|
214
|
+
text = text.toString();
|
215
|
+
}
|
216
|
+
if (text.indexOf('\n') !== -1) {
|
217
|
+
text = text.replace(/\\n/g, '\u{2029}');
|
218
|
+
}
|
219
|
+
const textTransform = span.textTransform || (parent && parent.textTransform);
|
220
|
+
if (textTransform) {
|
221
|
+
text = getTransformedText(text, textTransform);
|
222
|
+
}
|
223
|
+
return NSMutableAttributedString.alloc().initWithStringAttributes(text, attrDict);
|
224
|
+
}
|
225
|
+
else {
|
226
|
+
const result = NSMutableAttributedString.alloc().initWithAttributedString(text);
|
227
|
+
result.setAttributesRange(attrDict, { location: 0, length: text.length });
|
228
|
+
return result;
|
229
|
+
}
|
230
|
+
}
|
231
|
+
//# sourceMappingURL=index.ios.js.map
|
package/index.ios.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.ios.js","sourceRoot":"../src/","sources":["index.ios.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAa,IAAI,EAAE,eAAe,EAAY,MAAM,oBAAoB,CAAC;AACvF,OAAO,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AACrE,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAC/F,OAAO,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AACvD,cAAc,gBAAgB,CAAC;AAE/B,IAAI,gBA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EAAE,UAAU,CAAC,CAAC;SAC3C;QACD,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,aAAa,CAAC,CAAC;QAC7E,IAAI,aAAa,EAAE;YACf,IAAI,GAAG,kBAAkB,CAAC,IAAI,EAAE,aAAa,CAAC,CAAC;SAClD;QACD,OAAO,yBAAyB,CAAC,KAAK,EAAE,CAAC,wBAAwB,CAAC,IAAI,EAAE,QAAe,CAAC,CAAC;KAC5F;SAAM;QACH,MAAM,MAAM,GAAG,yBAAyB,CAAC,KAAK,EAAE,CAAC,wBAAwB,CAAC,IAAI,CAAC,CAAC;QAChF,MAAM,CAAC,kBAAkB,CAAC,QAAe,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QACjF,OAAO,MAAM,CAAC;KACjB;AACL,CAAC"}
|
package/index.js
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
import { InheritedCssProperty, makeParser, makeValidator, Style } from '@nativescript/core';
|
2
|
+
export const cssProperty = (target, key) => {
|
3
|
+
// property getter
|
4
|
+
const getter = function () {
|
5
|
+
return this.style[key];
|
6
|
+
};
|
7
|
+
// property setter
|
8
|
+
const setter = function (newVal) {
|
9
|
+
this.style[key] = newVal;
|
10
|
+
};
|
11
|
+
Object.defineProperty(target, key, {
|
12
|
+
get: getter,
|
13
|
+
set: setter,
|
14
|
+
enumerable: true,
|
15
|
+
configurable: true,
|
16
|
+
});
|
17
|
+
};
|
18
|
+
export const verticalTextAlignmentConverter = makeParser(makeValidator('initial', 'top', 'middle', 'bottom', 'center'));
|
19
|
+
export const verticalTextAlignmentProperty = new InheritedCssProperty({
|
20
|
+
name: 'verticalTextAlignment',
|
21
|
+
cssName: 'vertical-text-align',
|
22
|
+
valueConverter: verticalTextAlignmentConverter,
|
23
|
+
});
|
24
|
+
verticalTextAlignmentProperty.register(Style);
|
25
|
+
//# sourceMappingURL=index.js.map
|
package/index.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"../src/","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAG5F,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,MAAc,EAAE,GAAoB,EAAE,EAAE;IAChE,kBAAkB;IAClB,MAAM,MAAM,GAAG;QACX,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC,CAAC;IAEF,kBAAkB;IAClB,MAAM,MAAM,GAAG,UAAU,MAAM;QAC3B,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC;IAC7B,CAAC,CAAC;IAEF,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,GAAG,EAAE;QAC/B,GAAG,EAAE,MAAM;QACX,GAAG,EAAE,MAAM;QACX,UAAU,EAAE,IAAI;QAChB,YAAY,EAAE,IAAI;KACrB,CAAC,CAAC;AACP,CAAC,CAAC;AAIF,MAAM,CAAC,MAAM,8BAA8B,GAAG,UAAU,CACpD,aAAa,CAAwB,SAAS,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAC,CACvF,CAAC;AACF,MAAM,CAAC,MAAM,6BAA6B,GAAG,IAAI,oBAAoB,CAA+B;IAChG,IAAI,EAAE,uBAAuB;IAC7B,OAAO,EAAE,qBAAqB;IAC9B,cAAc,EAAE,8BAA8B;CACjD,CAAC,CAAC;AACH,6BAA6B,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC"}
|
package/package.json
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
{
|
2
|
+
"name": "@nativescript-community/text",
|
3
|
+
"version": "1.4.23",
|
4
|
+
"description": "Expands the capabilities of NativeScript's text property.",
|
5
|
+
"main": "./index",
|
6
|
+
"sideEffects": false,
|
7
|
+
"typings": "./index.d.ts",
|
8
|
+
"nativescript": {
|
9
|
+
"platforms": {
|
10
|
+
"android": "3.0.0",
|
11
|
+
"ios": "3.0.0"
|
12
|
+
}
|
13
|
+
},
|
14
|
+
"keywords": [
|
15
|
+
"NativeScript",
|
16
|
+
"JavaScript",
|
17
|
+
"Android",
|
18
|
+
"iOS"
|
19
|
+
],
|
20
|
+
"author": {
|
21
|
+
"name": "Martin Guillon",
|
22
|
+
"email": "martin@akylas.fr"
|
23
|
+
},
|
24
|
+
"bugs": {
|
25
|
+
"url": "https://github.com/nativescript-community/text/issues"
|
26
|
+
},
|
27
|
+
"repository": {
|
28
|
+
"type": "git",
|
29
|
+
"url": "https://github.com/nativescript-community/text"
|
30
|
+
},
|
31
|
+
"license": "Apache-2.0",
|
32
|
+
"readmeFilename": "README.md",
|
33
|
+
"gitHead": "6f84124ee662f2ddc4287063795b2782b6f2a2f1"
|
34
|
+
}
|
@@ -0,0 +1,71 @@
|
|
1
|
+
package com.nativescript.text;
|
2
|
+
|
3
|
+
import android.graphics.Paint;
|
4
|
+
import android.text.TextPaint;
|
5
|
+
import android.text.style.CharacterStyle;
|
6
|
+
|
7
|
+
import android.util.Log;
|
8
|
+
|
9
|
+
public class BaselineAdjustedSpan extends CharacterStyle {
|
10
|
+
private float fontSize;
|
11
|
+
private String align;
|
12
|
+
private float maxFontSize;
|
13
|
+
|
14
|
+
public BaselineAdjustedSpan(float fontSize, String align, float maxFontSize) {
|
15
|
+
super();
|
16
|
+
this.fontSize = fontSize;
|
17
|
+
this.align = align;
|
18
|
+
this.maxFontSize = maxFontSize;
|
19
|
+
}
|
20
|
+
|
21
|
+
@Override
|
22
|
+
public void updateDrawState(TextPaint ds) {
|
23
|
+
this.updateState(ds);
|
24
|
+
}
|
25
|
+
|
26
|
+
private int computeBaseLineOffset(String align, float fontAscent, float fontDescent, float fontBottom,
|
27
|
+
float fontTop, float fontSize, float maxFontSize) {
|
28
|
+
int result = 0;
|
29
|
+
switch (align) {
|
30
|
+
case "top":
|
31
|
+
result = (int) (-maxFontSize - fontBottom - fontTop);
|
32
|
+
break;
|
33
|
+
|
34
|
+
case "bottom":
|
35
|
+
result = (int) fontBottom;
|
36
|
+
break;
|
37
|
+
|
38
|
+
case "text-top":
|
39
|
+
result = (int) (-maxFontSize - fontDescent - fontAscent);
|
40
|
+
break;
|
41
|
+
|
42
|
+
case "text-bottom":
|
43
|
+
result = (int) (fontBottom - fontDescent);
|
44
|
+
break;
|
45
|
+
|
46
|
+
case "middle":
|
47
|
+
case "center":
|
48
|
+
result = (int) ((fontAscent - fontDescent) / 2 - fontAscent - maxFontSize / 2);
|
49
|
+
break;
|
50
|
+
|
51
|
+
case "super":
|
52
|
+
result = -(int) (maxFontSize - fontSize);
|
53
|
+
break;
|
54
|
+
|
55
|
+
case "sub":
|
56
|
+
result = 0;
|
57
|
+
break;
|
58
|
+
}
|
59
|
+
return result;
|
60
|
+
}
|
61
|
+
|
62
|
+
public void updateState(TextPaint paint) {
|
63
|
+
paint.setTextSize(fontSize);
|
64
|
+
Paint.FontMetrics metrics = paint.getFontMetrics();
|
65
|
+
// TODO: when or why should we add bottom?
|
66
|
+
// result += metrics.bottom;
|
67
|
+
int baselineShift = computeBaseLineOffset(align, metrics.ascent, metrics.descent, metrics.bottom, metrics.top,
|
68
|
+
fontSize, maxFontSize);
|
69
|
+
paint.baselineShift = baselineShift;
|
70
|
+
}
|
71
|
+
}
|
@@ -0,0 +1,70 @@
|
|
1
|
+
package com.nativescript.text;
|
2
|
+
import android.graphics.Canvas;
|
3
|
+
import android.graphics.Paint;
|
4
|
+
import android.graphics.Rect;
|
5
|
+
import android.graphics.drawable.ShapeDrawable;
|
6
|
+
import android.graphics.drawable.shapes.RoundRectShape;
|
7
|
+
import android.graphics.drawable.shapes.Shape;
|
8
|
+
import android.text.style.ReplacementSpan;
|
9
|
+
|
10
|
+
public class CustomBackgroundSpan extends ReplacementSpan {
|
11
|
+
public static class RoundedRectDrawable extends ShapeDrawable {
|
12
|
+
private final Paint fillpaint, strokepaint;
|
13
|
+
public RoundedRectDrawable(int radius, int fillColor, int strokeColor, int strokeWidth) {
|
14
|
+
super(new RoundRectShape(new float[] { radius, radius, radius, radius, radius, radius, radius, radius },
|
15
|
+
null, null));
|
16
|
+
fillpaint = new Paint(this.getPaint());
|
17
|
+
fillpaint.setColor(fillColor);
|
18
|
+
strokepaint = new Paint(fillpaint);
|
19
|
+
strokepaint.setStyle(Paint.Style.STROKE);
|
20
|
+
strokepaint.setStrokeWidth(strokeWidth);
|
21
|
+
strokepaint.setColor(strokeColor);
|
22
|
+
}
|
23
|
+
|
24
|
+
@Override
|
25
|
+
protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
|
26
|
+
shape.draw(canvas, fillpaint);
|
27
|
+
shape.draw(canvas, strokepaint);
|
28
|
+
}
|
29
|
+
}
|
30
|
+
private RoundedRectDrawable mDrawable;
|
31
|
+
|
32
|
+
int radius;
|
33
|
+
int fillColor;
|
34
|
+
int strokeColor;
|
35
|
+
int strokeWidth;
|
36
|
+
|
37
|
+
public CustomBackgroundSpan(int radius, int fillColor, int strokeColor, int strokeWidth) {
|
38
|
+
this.mDrawable = new RoundedRectDrawable(radius, fillColor, strokeColor, strokeWidth);
|
39
|
+
this.radius = radius;
|
40
|
+
this.fillColor = fillColor;
|
41
|
+
this.strokeColor = strokeColor;
|
42
|
+
this.strokeWidth = strokeWidth;
|
43
|
+
}
|
44
|
+
|
45
|
+
public CustomBackgroundSpan(CustomBackgroundSpan toCopy) {
|
46
|
+
this(toCopy.radius, toCopy.fillColor, toCopy.strokeColor, toCopy.strokeWidth);
|
47
|
+
}
|
48
|
+
|
49
|
+
@Override
|
50
|
+
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
|
51
|
+
return measureText(paint, text, start, end);
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
private int measureText(Paint paint, CharSequence text, int start, int end) {
|
56
|
+
return Math.round(paint.measureText(text, start, end));
|
57
|
+
}
|
58
|
+
|
59
|
+
@Override
|
60
|
+
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
|
61
|
+
float dx = strokeWidth / 2;
|
62
|
+
Rect rect = new Rect((int)(x + dx), (int)(top + dx), (int)(x + measureText(paint, text, start, end) - strokeWidth/2), (int)(bottom - strokeWidth/2));
|
63
|
+
this.mDrawable.setBounds(rect);
|
64
|
+
canvas.save();
|
65
|
+
this.mDrawable.draw(canvas);
|
66
|
+
canvas.restore();
|
67
|
+
canvas.drawText(text, start, end, x, y, paint);
|
68
|
+
}
|
69
|
+
|
70
|
+
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
package com.nativescript.text;
|
2
|
+
|
3
|
+
import android.annotation.SuppressLint;
|
4
|
+
import android.graphics.Typeface;
|
5
|
+
import android.text.TextPaint;
|
6
|
+
import android.text.style.TypefaceSpan;
|
7
|
+
|
8
|
+
/**
|
9
|
+
* Created by hhristov on 2/27/17.
|
10
|
+
*/
|
11
|
+
|
12
|
+
@SuppressLint("ParcelCreator")
|
13
|
+
public class CustomTypefaceSpan extends TypefaceSpan {
|
14
|
+
private Typeface typeface;
|
15
|
+
|
16
|
+
public CustomTypefaceSpan(String family, Typeface typeface) {
|
17
|
+
super(family);
|
18
|
+
this.typeface = typeface;
|
19
|
+
}
|
20
|
+
|
21
|
+
public Typeface getTypeface() {
|
22
|
+
return this.typeface;
|
23
|
+
}
|
24
|
+
|
25
|
+
public void updateDrawState(TextPaint ds) {
|
26
|
+
this.applyCustomTypeFace(ds);
|
27
|
+
}
|
28
|
+
|
29
|
+
public void updateMeasureState(TextPaint paint) {
|
30
|
+
this.applyCustomTypeFace(paint);
|
31
|
+
}
|
32
|
+
|
33
|
+
private void applyCustomTypeFace(TextPaint paint) {
|
34
|
+
Typeface typeface = this.typeface;
|
35
|
+
paint.setTypeface(typeface);
|
36
|
+
}
|
37
|
+
}
|