@domir/react-native-measure-text 0.1.6 → 0.1.8
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/android/build.gradle
CHANGED
|
@@ -54,14 +54,18 @@ afterEvaluate {
|
|
|
54
54
|
android {
|
|
55
55
|
compileSdkVersion safeExtGet("compileSdkVersion", 31)
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
|
|
58
|
+
if (agpVersion.tokenize('.')[0].toInteger() < 8) {
|
|
59
|
+
compileOptions {
|
|
60
|
+
sourceCompatibility JavaVersion.VERSION_11
|
|
61
|
+
targetCompatibility JavaVersion.VERSION_11
|
|
62
|
+
}
|
|
61
63
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
+
kotlinOptions {
|
|
65
|
+
jvmTarget = JavaVersion.VERSION_11.majorVersion
|
|
66
|
+
}
|
|
64
67
|
}
|
|
68
|
+
|
|
65
69
|
namespace "expo.modules.measuretext"
|
|
66
70
|
defaultConfig {
|
|
67
71
|
minSdkVersion safeExtGet("minSdkVersion", 21)
|
|
@@ -2,6 +2,7 @@ package expo.modules.measuretext
|
|
|
2
2
|
|
|
3
3
|
import android.graphics.Typeface
|
|
4
4
|
import android.text.TextPaint
|
|
5
|
+
import com.facebook.react.uimanager.PixelUtil
|
|
5
6
|
import com.facebook.react.views.text.ReactFontManager
|
|
6
7
|
import com.facebook.react.views.text.ReactTypefaceUtils.parseFontWeight
|
|
7
8
|
import expo.modules.kotlin.modules.Module
|
|
@@ -10,6 +11,9 @@ import expo.modules.kotlin.records.Field
|
|
|
10
11
|
import expo.modules.kotlin.records.Record
|
|
11
12
|
|
|
12
13
|
class StyleOptions : Record {
|
|
14
|
+
@Field
|
|
15
|
+
val letterSpacing: Float? = null
|
|
16
|
+
|
|
13
17
|
@Field
|
|
14
18
|
val fontFamily: String? = null
|
|
15
19
|
|
|
@@ -18,6 +22,9 @@ class StyleOptions : Record {
|
|
|
18
22
|
|
|
19
23
|
@Field
|
|
20
24
|
val fontSize: Int? = null
|
|
25
|
+
|
|
26
|
+
@Field
|
|
27
|
+
val allowFontScaling: Boolean? = null
|
|
21
28
|
}
|
|
22
29
|
|
|
23
30
|
class ReactNativeMeasureTextModule : Module() {
|
|
@@ -25,16 +32,21 @@ class ReactNativeMeasureTextModule : Module() {
|
|
|
25
32
|
Name("ReactNativeMeasureText")
|
|
26
33
|
|
|
27
34
|
Function("measureWidth") { text: String, style: StyleOptions ->
|
|
28
|
-
val paint = createTextPaint(style.fontSize, style.fontFamily, style.fontWeight);
|
|
35
|
+
val paint = createTextPaint(style.fontSize, style.fontFamily, style.fontWeight, style.letterSpacing, style.allowFontScaling);
|
|
29
36
|
return@Function paint.measureText(text);
|
|
30
37
|
}
|
|
31
38
|
}
|
|
32
39
|
|
|
33
|
-
private fun createTextPaint(fontSize: Int?, fontFamily: String?, fontWeight: String?): TextPaint {
|
|
40
|
+
private fun createTextPaint(fontSize: Int?, fontFamily: String?, fontWeight: String?, letterSpacing: Float?, allowFontScaling: Boolean?): TextPaint {
|
|
34
41
|
val paint = TextPaint(TextPaint.ANTI_ALIAS_FLAG);
|
|
35
42
|
val fontScale = appContext.reactContext?.resources?.configuration?.fontScale ?: 1.0f;
|
|
43
|
+
val allowScaledFont = allowFontScaling ?: true;
|
|
36
44
|
val usedFontSize = (fontSize ?: 0) * fontScale;
|
|
37
45
|
paint.textSize = usedFontSize;
|
|
46
|
+
val letterSpacingPixels = if (allowScaledFont) PixelUtil.toPixelFromSP(letterSpacing ?: 0f) else PixelUtil.toPixelFromDIP(letterSpacing ?: 0f);
|
|
47
|
+
val mFontSize = if (allowScaledFont) Math.ceil(PixelUtil.toPixelFromSP(usedFontSize).toDouble()).toInt() else Math.ceil(PixelUtil.toPixelFromDIP(usedFontSize).toDouble()).toInt();
|
|
48
|
+
val usedLetterSpacing = if (letterSpacingPixels <= 0f) letterSpacing ?: 0f else letterSpacingPixels / mFontSize;
|
|
49
|
+
paint.letterSpacing = usedLetterSpacing;
|
|
38
50
|
val assetManager = appContext.reactContext?.assets;
|
|
39
51
|
|
|
40
52
|
if (fontFamily == null || assetManager == null || fontWeight == null) {
|
|
@@ -10,6 +10,12 @@ struct StyleOptions: Record {
|
|
|
10
10
|
|
|
11
11
|
@Field
|
|
12
12
|
var fontSize: Int? = nil
|
|
13
|
+
|
|
14
|
+
@Field
|
|
15
|
+
var letterSpacing: Double? = nil
|
|
16
|
+
|
|
17
|
+
@Field
|
|
18
|
+
var allowFontScaling: Bool? = nil // ANDROID ONLY
|
|
13
19
|
}
|
|
14
20
|
|
|
15
21
|
public class ReactNativeMeasureTextModule: Module {
|
|
@@ -18,10 +24,11 @@ public class ReactNativeMeasureTextModule: Module {
|
|
|
18
24
|
|
|
19
25
|
Function("measureWidth") { (text: String, style: StyleOptions) -> CGFloat in
|
|
20
26
|
let fontSize = style.fontSize != nil ? RCTConvert.cgFloat(style.fontSize) : CGFloat(0)
|
|
27
|
+
let letterSpacing = style.letterSpacing != nil ? RCTConvert.cgFloat(style.letterSpacing) : CGFloat(0)
|
|
21
28
|
let usedFontWeight = style.fontWeight ?? "Normal"
|
|
22
29
|
|
|
23
30
|
if let font = style.fontFamily != nil ?
|
|
24
|
-
RCTConvert.uiFont(["fontWeight": usedFontWeight, "fontFamily": style.fontFamily!, "fontSize": fontSize]) : RCTConvert.uiFont(["fontWeight": usedFontWeight, "fontSize": fontSize])
|
|
31
|
+
RCTConvert.uiFont(["fontWeight": usedFontWeight, "fontFamily": style.fontFamily!, "fontSize": fontSize, NSAttributedString.Key.kern: letterSpacing ]) : RCTConvert.uiFont(["fontWeight": usedFontWeight, "fontSize": fontSize])
|
|
25
32
|
{
|
|
26
33
|
let size: CGSize = text.size(withAttributes: [.font: font])
|
|
27
34
|
return size.width
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@ import ReactNativeMeasureText from "./ReactNativeMeasureTextModule";
|
|
|
2
2
|
|
|
3
3
|
export function measureWidth(
|
|
4
4
|
text: string,
|
|
5
|
-
style: { fontSize?: number; fontFamily?: string; fontWeight?: string } = {}
|
|
5
|
+
style: { fontSize?: number; fontFamily?: string; fontWeight?: string, letterSpacing?: number, allowFontScaling?: boolean } = {}
|
|
6
6
|
): number {
|
|
7
7
|
return ReactNativeMeasureText.measureWidth(text, style);
|
|
8
8
|
}
|