-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJustifiedTextView.cs
111 lines (94 loc) · 2.57 KB
/
JustifiedTextView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Text;
using Android.Graphics;
namespace JustifiedXamarin
{
public class JustifiedTextView : TextView
{
private int mLineY;
private int mViewWidth;
public JustifiedTextView (Context context) :
base (context)
{
Initialize ();
}
public JustifiedTextView (Context context, IAttributeSet attrs) :
base (context, attrs)
{
Initialize ();
}
public JustifiedTextView (Context context, IAttributeSet attrs, int defStyle) :
base (context, attrs, defStyle)
{
Initialize ();
}
void Initialize ()
{
}
protected override void OnLayout (bool changed, int left, int top, int right, int bottom)
{
base.OnLayout (changed, left, top, right, bottom);
}
protected override void OnDraw (Android.Graphics.Canvas canvas)
{
TextPaint paint = Paint;
paint.Color = new Color(CurrentTextColor);
paint.DrawableState = GetDrawableState();
mViewWidth = MeasuredWidth;
String text = (String) this.Text;
mLineY = 0;
mLineY += (int)TextSize;
Layout layout = Layout;
for (int i = 0; i < layout.LineCount; i++) {
int lineStart = layout.GetLineStart(i);
int lineEnd = layout.GetLineEnd(i);
String line = text.Substring(lineStart, lineEnd-lineStart);
float width = StaticLayout.GetDesiredWidth(text, lineStart, lineEnd, Paint);
if (NeedScale(line)) {
DrawScaledText(canvas, lineStart, line, width);
} else {
canvas.DrawText(line, 0, mLineY, paint);
}
mLineY += LineHeight;
}
}
private void DrawScaledText(Canvas canvas, int lineStart, String line, float lineWidth) {
float x = 0;
if (IsFirstLineOfParagraph(lineStart, line)) {
String blanks = " ";
canvas.DrawText(blanks, x, mLineY, Paint);
float bw = StaticLayout.GetDesiredWidth(blanks, Paint);
x += bw;
line = line.Substring(3);
}
float d = (mViewWidth - lineWidth) / line.Length - 1;
for (int i = 0; i < line.Length; i++) {
String c = line[i].ToString();
float cw = StaticLayout.GetDesiredWidth(c, Paint);
canvas.DrawText(c, x, mLineY, Paint);
x += cw + d;
}
}
private bool IsFirstLineOfParagraph(int lineStart, String line) {
return line.Length > 3 && line[0] == ' ' && line[1] == ' ';
}
private bool NeedScale(String line) {
if (line.Length == 0) {
return false;
} else {
return line[line.Length - 1] != '\n';
}
}
}
}