Skip to content

Commit 3396a81

Browse files
marunjarJens Claes
authored andcommitted
Fix ellipsize bug
Fixes #206
1 parent 853ca78 commit 3396a81

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

library/src/main/java/com/alamkanak/weekview/WeekView.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ eventRectF.top < getHeight() &&
727727
*/
728728
private void drawEventTitle(WeekViewEvent event, RectF rect, Canvas canvas, float originalTop, float originalLeft) {
729729
if (rect.right - rect.left - mEventPadding * 2 < 0) return;
730+
if (rect.bottom - rect.top - mEventPadding * 2 < 0) return;
730731

731732
SpannableStringBuilder bob = new SpannableStringBuilder();
732733
if (event.getName() != null) {
@@ -738,28 +739,31 @@ private void drawEventTitle(WeekViewEvent event, RectF rect, Canvas canvas, floa
738739
bob.append(event.getLocation());
739740
}
740741

742+
int availableHeight = (int) (rect.bottom - originalTop - mEventPadding * 2);
743+
int availableWidth = (int) (rect.right - originalLeft - mEventPadding * 2);
744+
741745
// Get text dimensions
742-
StaticLayout textLayout = new StaticLayout(bob, mEventTextPaint, (int) (rect.right - originalLeft - mEventPadding * 2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
746+
StaticLayout textLayout = new StaticLayout(bob, mEventTextPaint, availableWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
743747

744-
// Crop height
745-
int availableHeight = (int) (rect.bottom - originalTop - mEventPadding * 2);
746748
int lineHeight = textLayout.getHeight() / textLayout.getLineCount();
747-
if (lineHeight < availableHeight && textLayout.getHeight() > rect.height() - mEventPadding * 2) {
748-
int lineCount = textLayout.getLineCount();
749-
int availableLineCount = (int) Math.floor(lineCount * availableHeight / textLayout.getHeight());
750-
float widthAvailable = (rect.right - originalLeft - mEventPadding * 2) * availableLineCount;
751-
textLayout = new StaticLayout(TextUtils.ellipsize(bob, mEventTextPaint, widthAvailable, TextUtils.TruncateAt.END), mEventTextPaint, (int) (rect.right - originalLeft - mEventPadding * 2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
752-
}
753-
else if (lineHeight >= availableHeight) {
754-
int width = (int) (rect.right - originalLeft - mEventPadding * 2);
755-
textLayout = new StaticLayout(TextUtils.ellipsize(bob, mEventTextPaint, width, TextUtils.TruncateAt.END), mEventTextPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 1.0f, false);
756-
}
757749

758-
// Draw text
759-
canvas.save();
760-
canvas.translate(originalLeft + mEventPadding, originalTop + mEventPadding);
761-
textLayout.draw(canvas);
762-
canvas.restore();
750+
if (availableHeight >= lineHeight) {
751+
// calculate available lines
752+
int availableLineCount = availableHeight / lineHeight;
753+
do {
754+
// ellipsize text to fit into event rect
755+
textLayout = new StaticLayout(TextUtils.ellipsize(bob, mEventTextPaint, availableLineCount * availableWidth, TextUtils.TruncateAt.END), mEventTextPaint, (int) (rect.right - originalLeft - mEventPadding * 2), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
756+
// reduce line count
757+
availableLineCount--;
758+
// and repeat until text is short enough
759+
} while (textLayout.getHeight() > availableHeight);
760+
761+
// Draw text
762+
canvas.save();
763+
canvas.translate(originalLeft + mEventPadding, originalTop + mEventPadding);
764+
textLayout.draw(canvas);
765+
canvas.restore();
766+
}
763767
}
764768

765769

0 commit comments

Comments
 (0)