From 5d87f533ee34928e970eccc9b1b91557208e8926 Mon Sep 17 00:00:00 2001 From: Sira Lam Date: Tue, 3 Oct 2017 17:04:34 +0800 Subject: [PATCH] SimplePagerTitleView now returns correct ContentLeft and ContentRight if text has a linebreak --- .../titles/SimplePagerTitleView.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/titles/SimplePagerTitleView.java b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/titles/SimplePagerTitleView.java index 6a1a593f..843bdb7d 100644 --- a/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/titles/SimplePagerTitleView.java +++ b/magicindicator/src/main/java/net/lucode/hackware/magicindicator/buildins/commonnavigator/titles/SimplePagerTitleView.java @@ -54,7 +54,16 @@ public void onEnter(int index, int totalCount, float enterPercent, boolean leftT @Override public int getContentLeft() { Rect bound = new Rect(); - getPaint().getTextBounds(getText().toString(), 0, getText().length(), bound); + String longestString = ""; + if (getText().toString().contains("\n")) { + String[] brokenStrings = getText().toString().split("\\n"); + for (String each : brokenStrings) { + if (each.length() > longestString.length()) longestString = each; + } + } else { + longestString = getText().toString(); + } + getPaint().getTextBounds(longestString, 0, longestString.length(), bound); int contentWidth = bound.width(); return getLeft() + getWidth() / 2 - contentWidth / 2; } @@ -69,7 +78,16 @@ public int getContentTop() { @Override public int getContentRight() { Rect bound = new Rect(); - getPaint().getTextBounds(getText().toString(), 0, getText().length(), bound); + String longestString = ""; + if (getText().toString().contains("\n")) { + String[] brokenStrings = getText().toString().split("\\n"); + for (String each : brokenStrings) { + if (each.length() > longestString.length()) longestString = each; + } + } else { + longestString = getText().toString(); + } + getPaint().getTextBounds(longestString, 0, longestString.length(), bound); int contentWidth = bound.width(); return getLeft() + getWidth() / 2 + contentWidth / 2; }