Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 1efec39

Browse files
committed
Add LineHeightSpan, RelativeLineHeightSpan and LetterSpacingSpan. Add few examples to example app.
1 parent 34a6dfc commit 1efec39

File tree

9 files changed

+175
-69
lines changed

9 files changed

+175
-69
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ android {
44
compileSdkVersion 26
55
defaultConfig {
66
applicationId "io.supernova.supernovauitoolkit"
7-
minSdkVersion 16
7+
minSdkVersion 21
88
targetSdkVersion 26
99
versionCode 1
1010
versionName "1.0"

app/src/androidTest/java/io/supernova/supernovauitoolkit/ExampleInstrumentedTest.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package io.supernova.toolkit.example;
22

3-
import android.support.v7.app.AppCompatActivity;
43
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
55
import android.text.SpannableString;
66
import android.text.Spanned;
7-
import android.text.style.LineHeightSpan;
87
import android.widget.TextView;
98

109
import io.supernova.supernovauitoolkit.R;
11-
import io.supernova.uitoolkit.text.LineSpacingSpan;
10+
import io.supernova.uitoolkit.text.LetterSpacingSpan;
11+
import io.supernova.uitoolkit.text.LineHeightSpan;
12+
import io.supernova.uitoolkit.text.RelativeLineHeightSpan;
1213

1314

1415
public class MainActivity extends AppCompatActivity {
1516

1617
private TextView exampleTextView;
18+
private TextView exampleTextView2;
19+
1720

1821
@Override
1922
protected void onCreate(Bundle savedInstanceState) {
@@ -26,11 +29,41 @@ protected void onCreate(Bundle savedInstanceState) {
2629

2730
private void init() {
2831
this.exampleTextView = findViewById(R.id.exampleTextView);
32+
this.exampleTextView2 = findViewById(R.id.exampleTextView2);
2933

30-
SpannableString spannableString = new SpannableString("Example String\nShowcasing line spacing span\nIt's great, trust me");
34+
this.setupLineSpacingExample();
35+
this.setupLetterSpacingExample();
36+
}
3137

32-
LineHeightSpan lineHeightSpan = new LineSpacingSpan();
38+
private void setupLineSpacingExample() {
39+
40+
SpannableString spannableString = new SpannableString("Example String\nShowcasing line spacing span\nIt's awesome, trust me");
41+
42+
// Setup line height span
43+
LineHeightSpan lineHeightSpan = new LineHeightSpan(40);
3344
spannableString.setSpan(lineHeightSpan, 15, 42, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
45+
46+
// Setup relative line height span
47+
RelativeLineHeightSpan relativeLineHeightSpan = new RelativeLineHeightSpan(0.7f);
48+
spannableString.setSpan(relativeLineHeightSpan, 44, spannableString.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
49+
50+
// Pass spannable text to the view
3451
this.exampleTextView.setText(spannableString);
3552
}
53+
54+
private void setupLetterSpacingExample() {
55+
56+
SpannableString spannableString = new SpannableString("Example String\nShowcasing letter spacing span\nIt's awesome, trust me");
57+
58+
// Setup stretch line height span
59+
LetterSpacingSpan stretchingSpan = new LetterSpacingSpan(0.2f);
60+
spannableString.setSpan(stretchingSpan, 0, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
61+
62+
// Setup shrink letter spacing span
63+
LetterSpacingSpan shrinkingSpan = new LetterSpacingSpan(-0.1f);
64+
spannableString.setSpan(shrinkingSpan, 30, spannableString.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
65+
66+
// Pass spannable text to the view
67+
this.exampleTextView2.setText(spannableString);
68+
}
3669
}

app/src/main/res/layout/activity_main.xml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,35 @@
99

1010

1111
<TextView
12-
android:id="@+id/exampleTextView"
12+
android:id="@+id/exampleTextView2"
1313
android:layout_width="wrap_content"
1414
android:layout_height="wrap_content"
15-
android:layout_marginBottom="8dp"
1615
android:layout_marginEnd="8dp"
1716
android:layout_marginLeft="8dp"
1817
android:layout_marginRight="8dp"
1918
android:layout_marginStart="8dp"
20-
android:layout_marginTop="8dp"
2119
android:gravity="center"
22-
android:text="TextView"
20+
android:textSize="14sp"
2321
app:layout_constraintBottom_toBottomOf="parent"
2422
app:layout_constraintEnd_toEndOf="parent"
2523
app:layout_constraintStart_toStartOf="parent"
26-
app:layout_constraintTop_toTopOf="parent"/>
24+
app:layout_constraintTop_toBottomOf="@+id/exampleTextView"
25+
tools:text="TextView"/>
26+
27+
28+
<TextView
29+
android:id="@+id/exampleTextView"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:layout_marginEnd="8dp"
33+
android:layout_marginLeft="8dp"
34+
android:layout_marginRight="8dp"
35+
android:layout_marginStart="8dp"
36+
android:gravity="center"
37+
app:layout_constraintBottom_toTopOf="@+id/exampleTextView2"
38+
app:layout_constraintEnd_toEndOf="parent"
39+
app:layout_constraintStart_toStartOf="parent"
40+
app:layout_constraintTop_toTopOf="parent"
41+
app:layout_constraintVertical_chainStyle="spread"
42+
tools:text="TextView"/>
2743
</android.support.constraint.ConstraintLayout>

app/src/test/java/io/supernova/supernovauitoolkit/ExampleUnitTest.java

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.supernova.uitoolkit.text;
2+
3+
import android.os.Build;
4+
import android.support.annotation.RequiresApi;
5+
import android.text.TextPaint;
6+
import android.text.style.MetricAffectingSpan;
7+
8+
9+
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
10+
public class LetterSpacingSpan extends MetricAffectingSpan {
11+
12+
private float letterSpacing;
13+
14+
15+
public LetterSpacingSpan(float letterSpacing) {
16+
this.letterSpacing = letterSpacing;
17+
}
18+
19+
20+
21+
@Override
22+
public void updateMeasureState(TextPaint p) {
23+
p.setLetterSpacing(this.letterSpacing);
24+
}
25+
26+
27+
@Override
28+
public void updateDrawState(TextPaint tp) {
29+
tp.setLetterSpacing(this.letterSpacing);
30+
}
31+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.supernova.uitoolkit.text;
2+
3+
import android.graphics.Paint;
4+
import android.support.annotation.Nullable;
5+
import android.text.TextPaint;
6+
7+
8+
public class LineHeightSpan implements android.text.style.LineHeightSpan.WithDensity {
9+
10+
private final int height;
11+
12+
13+
public LineHeightSpan(int height) {
14+
this.height = height;
15+
}
16+
17+
18+
@Override
19+
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) {
20+
// Should not get called, at least not by StaticLayout.
21+
this.chooseHeight(text, start, end, spanstartv, v, fm, null);
22+
}
23+
24+
@Override
25+
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm, @Nullable TextPaint paint) {
26+
int size = height;
27+
28+
// Apply density if provided.
29+
if (paint != null) {
30+
size *= paint.density;
31+
}
32+
33+
this.applyLineHeight(size, fm);
34+
}
35+
36+
private void applyLineHeight(int newHeight, Paint.FontMetricsInt fm) {
37+
fm.top = fm.bottom - newHeight;
38+
fm.ascent = fm.descent - newHeight;
39+
}
40+
}

lib/src/main/java/io/supernova/uitoolkit/text/LineSpacingSpan.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.supernova.uitoolkit.text;
2+
3+
import android.graphics.Paint;
4+
import android.support.annotation.Nullable;
5+
import android.text.TextPaint;
6+
import android.text.style.LineHeightSpan;
7+
8+
9+
public class RelativeLineHeightSpan implements LineHeightSpan.WithDensity {
10+
11+
private final float lineHeightMultiplier;
12+
13+
14+
public RelativeLineHeightSpan(float lineHeightMultiplier) {
15+
this.lineHeightMultiplier = lineHeightMultiplier;
16+
}
17+
18+
19+
@Override
20+
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) {
21+
// Should not get called, at least not by StaticLayout.
22+
this.chooseHeight(text, start, end, spanstartv, v, fm, null);
23+
}
24+
25+
@Override
26+
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm, @Nullable TextPaint paint) {
27+
28+
// Calculate new line height.
29+
float currentHeight = this.getLineHeightFromMetrics(fm);
30+
float newHeight = currentHeight * lineHeightMultiplier;
31+
32+
this.applyLineHeight(((int) newHeight), fm);
33+
}
34+
35+
36+
private int getLineHeightFromMetrics(Paint.FontMetricsInt fm) {
37+
return fm.descent - fm.ascent;
38+
}
39+
40+
private void applyLineHeight(int newHeight, Paint.FontMetricsInt fm) {
41+
fm.top = fm.bottom - newHeight;
42+
fm.ascent = fm.descent - newHeight;
43+
}
44+
}

0 commit comments

Comments
 (0)