Skip to content

Commit

Permalink
MongolTextView line spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
suragch committed Nov 24, 2018
1 parent 5e9802c commit e00ac7a
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public static Rect getDesiredSize(CharSequence source,
* @param canvas the canvas to draw the layout on
*/
public void draw(Canvas canvas) {
//draw(canvas, null, null, 0);
if (mHeight <= 0) return;

if (needsLineUpdate) updateLines();
Expand All @@ -129,7 +128,6 @@ public void drawText(Canvas canvas) {

if (needsLineUpdate) updateLines();

float metricsTop;
int metricsBottom = mTextPaint.getFontMetricsInt().bottom;

int x = metricsBottom; // start position of each vertical line
Expand Down Expand Up @@ -168,7 +166,8 @@ public void drawText(Canvas canvas) {
} else {
lineHeight = mLinesInfo.get(i).top;
}
metricsTop = metricsBottom - lineHeight;
int extraSpacing = (int) (mLinesInfo.get(i).extraSpacing);
float metricsTop = metricsBottom - lineHeight + extraSpacing;
tl.draw(canvas, x, metricsTop, y + gravityOffset, metricsBottom);

x += lineHeight;
Expand Down Expand Up @@ -301,9 +300,8 @@ private void updateLines() {
// TODO should be using a different height if there is a span
lineHeightMax = mTextPaint.getFontMetrics().bottom - mTextPaint.getFontMetrics().top;
}
extraSpacing = getExtraSpacing(lineHeightMax);
top += lineHeightMax + extraSpacing;
mLinesInfo.add(new LineInfo(lineStart, top, measuredSum, extraSpacing));
top += lineHeightMax;
mLinesInfo.add(new LineInfo(lineStart, top, measuredSum, 0));
}
}

Expand All @@ -317,23 +315,6 @@ private float getExtraSpacing(float lineHeight) {
return -(int)(-extra + EXTRA_ROUNDING);
}
}
//
// private int findNewTop(float oldTop, float lineHeight) {
// int sum = (int) (oldTop + lineHeight);
// int extra;
// if (mSpacingAdd != 0 || mSpacingMult != 1) {
// double ex = lineHeight * (mSpacingMult - 1) + mSpacingAdd;
// if (ex >= 0) {
// extra = (int)(ex + EXTRA_ROUNDING);
// } else {
// extra = -(int)(-ex + EXTRA_ROUNDING);
// }
// } else {
// extra = 0;
// }
//
// return sum + extra;
// }

/**
* Call this if the height has not changed but something else like the font size has.
Expand Down
3 changes: 3 additions & 0 deletions testing-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
</activity>
<activity android:label="@string/mongol_textview_activity_title"
android:name=".MongolTextViewActivity"/>

<activity android:label="@string/mongol_textview_spacing_activity_title"
android:name=".MongolTextViewSpacingActivity"/>
<activity android:name=".KeyboardActivity"/>
</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected void onCreate(Bundle savedInstanceState) {
ArrayList<String> testingActivities = new ArrayList<>();
testingActivities.add("Keyboard");
testingActivities.add("MongolTextView");
testingActivities.add("MongolTextView text spacing");


// set up the RecyclerView
Expand All @@ -45,6 +46,10 @@ public void onItemClick(View view, int position) {
intent = new Intent(this, MongolTextViewActivity.class);
startActivity(intent);
break;
case 2: // MongolTextView text spacing
intent = new Intent(this, MongolTextViewSpacingActivity.class);
startActivity(intent);
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package net.studymongolian.testingapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.TextView;

import net.studymongolian.mongollibrary.MongolTextView;

public class MongolTextViewSpacingActivity extends AppCompatActivity {

private static final String TEXT = "ᠨᠢᠭᠡ ᠬᠣᠶᠠᠷ ᠭᠣᠷᠪᠠ ᠳᠥᠷᠪᠡ ᠲᠠᠪᠤ ᠵᠢᠷᠭᠤᠭ᠎ᠠ ᠳᠣᠯᠣᠭ᠎ᠠ ᠨᠠ\u200dᠢᠮᠠ ᠶᠢᠰᠦ ᠠᠷᠪᠠ ᠠᠷᠪᠠᠨ ᠨᠢᠭᠡ ᠠᠷᠪᠠᠨ ᠬᠣᠶᠠᠷ ᠠᠷᠪᠠᠨ ᠭᠣᠷᠪᠠ ᠠᠷᠪᠠᠨ ᠳᠥᠷᠪᠡ ᠠᠷᠪᠠᠨ ᠲᠠᠪᠤ ᠠᠷᠪᠠᠨ ᠵᠢᠷᠭᠤᠭ᠎ᠠ ᠠᠷᠪᠠᠨ ᠳᠣᠯᠣᠭ᠎ᠠ ᠠᠷᠪᠠᠨ ᠨᠠ\u200dᠢᠮᠠ ᠠᠷᠪᠠ ᠶᠢᠰᠦ ᠬᠣᠷᠢ \uD83D\uDE42 ᠬᠣᠷᠢᠨ ᠨᠢᠭᠡ ᠬᠣᠷᠢᠨ ᠬᠣᠶᠠᠷ ᠬᠣᠷᠢᠨ ᠭᠣᠷᠪᠠ ᠬᠣᠷᠢᠨ ᠳᠥᠷᠪᠡ ᠬᠣᠷᠢᠨ ᠲᠠᠪᠤ ᠬᠣᠷᠢᠨ ᠵᠢᠷᠭᠤᠭ᠎ᠠ ᠬᠣᠷᠢᠨ ᠳᠣᠯᠣᠭ᠎ᠠ ᠬᠣᠷᠢᠨ ᠨᠠ\u200dᠢᠮᠠ ᠬᠣᠷᠢ ᠶᠢᠰᠦ ᠭᠣᠴᠢ one two three four five six seven eight nine ten 一二三四五六七八九十\uD83D\uDE03\uD83D\uDE0A\uD83D\uDE1C\uD83D\uDE01\uD83D\uDE2C\uD83D\uDE2E\uD83D\uDC34\uD83D\uDC02\uD83D\uDC2B\uD83D\uDC11\uD83D\uDC10ᆾ①②③㉑㊿〖汉字〗한국어モンゴル語English?︽ᠮᠣᠩᠭᠣᠯ︖︾";

MongolTextView tvSampleText;
TextView tvLineHeight;
SeekBar seekBar;
RadioButton rbAdd;
RadioButton rbMult;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mtv_spacing);

seekBar = findViewById(R.id.seekBar);
tvSampleText = findViewById(R.id.tv_sample_text);
tvSampleText.setText(TEXT);
tvLineHeight = findViewById(R.id.tv_line_height);
seekBar.setOnSeekBarChangeListener(seekBarChangeListener);
rbAdd = findViewById(R.id.radioButtonAdd);
rbMult = findViewById(R.id.radioButtonMultiply);

}

private SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (rbAdd.isChecked()) {
tvSampleText.setLineSpacing(progress, 1);
setLabels(progress, 1);
} else {
float multiplier = progress / 50f;
tvSampleText.setLineSpacing(0, multiplier);
setLabels(0, multiplier);
}

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
};

private void setLabels(float add, float mult) {
tvLineHeight.setText("Line height = " + tvSampleText.getLineWidth());
rbAdd.setText("Add " + add);
rbMult.setText("Multiply " + mult);
}

public void onAddClick(View view) {
seekBar.setProgress(0);
}

public void onMultiplyClick(View view) {
seekBar.setProgress(50);
}
}
80 changes: 80 additions & 0 deletions testing-app/src/main/res/layout/activity_mtv_spacing.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MongolTextViewSpacingActivity">

<net.studymongolian.mongollibrary.MongolTextView
android:id="@+id/tv_sample_text"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/tv_line_height"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
app:textColor="#fff" />

<SeekBar
android:id="@+id/seekBar"
android:layout_width="0dp"
android:layout_height="18dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="24dp"
android:max="100"
android:progress="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/seekBar"
app:layout_constraintStart_toStartOf="parent">

<RadioButton
android:id="@+id/radioButtonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:onClick="onAddClick"
android:text="Add" />

<RadioButton
android:id="@+id/radioButtonMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onMultiplyClick"
android:text="Multiply" />
</RadioGroup>

<TextView
android:id="@+id/tv_line_height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/radioGroup"
app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>
1 change: 1 addition & 0 deletions testing-app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<resources>
<string name="app_name">Testing App</string>
<string name="mongol_textview_activity_title">MongolTextView tests</string>
<string name="mongol_textview_spacing_activity_title">MongolTextView text spacing</string>
</resources>

0 comments on commit e00ac7a

Please sign in to comment.