@@ -727,6 +727,7 @@ eventRectF.top < getHeight() &&
727
727
*/
728
728
private void drawEventTitle (WeekViewEvent event , RectF rect , Canvas canvas , float originalTop , float originalLeft ) {
729
729
if (rect .right - rect .left - mEventPadding * 2 < 0 ) return ;
730
+ if (rect .bottom - rect .top - mEventPadding * 2 < 0 ) return ;
730
731
731
732
SpannableStringBuilder bob = new SpannableStringBuilder ();
732
733
if (event .getName () != null ) {
@@ -738,28 +739,31 @@ private void drawEventTitle(WeekViewEvent event, RectF rect, Canvas canvas, floa
738
739
bob .append (event .getLocation ());
739
740
}
740
741
742
+ int availableHeight = (int ) (rect .bottom - originalTop - mEventPadding * 2 );
743
+ int availableWidth = (int ) (rect .right - originalLeft - mEventPadding * 2 );
744
+
741
745
// 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 );
743
747
744
- // Crop height
745
- int availableHeight = (int ) (rect .bottom - originalTop - mEventPadding * 2 );
746
748
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
- }
757
749
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
+ }
763
767
}
764
768
765
769
0 commit comments