List of usage examples for java.text BreakIterator getWordInstance
public static BreakIterator getWordInstance()
BreakIterator
instance for word breaks for the Locale#getDefault() default locale . From source file:com.amazon.android.ui.widget.EllipsizedTextView.java
/** * Sets ellipsis properties./*from w w w.j a v a 2 s .c o m*/ * * @param widthMeasureSpec Ellipsis width. * @param heightMeasureSpec Ellipsis height. * @param layout Layout for ellipsis. * @param lastLine Last line length for ellipsis. * @param maxLines Max lines in ellipsis. */ private void setEllipsis(int widthMeasureSpec, int heightMeasureSpec, Layout layout, int lastLine, int maxLines) { mIsEllipsized = true; setFocusable(true); setClickable(true); final SpannableString ss = new SpannableString(mCharSequence); String visibleText = mCharSequence.toString(); mEllipsisImage = new StateImageSpan(getContext(), mGuillemetDrawableId, ImageSpan.ALIGN_BASELINE); final SpannableStringBuilder spannedText = new SpannableStringBuilder(); int ellipsisIndex = layout.getLineStart(Math.min(lastLine + 1, maxLines)); // Keep chopping words off until the ellipsis is on a visible line or there is only one // line left. do { // Only truncate the last line for long description. if (lastLine >= maxLines) { // Getting the first word break index before the last index of maxline. int safeBreakIndex = breakBefore(visibleText, ellipsisIndex, BreakIterator.getWordInstance()); final int maxLineStart = layout.getLineStart(maxLines - 1); // If this check pass, it means we just truncated a word that is longer than a line. if (safeBreakIndex < maxLineStart) { // Need to check character by character and break in the middle now. Checking // word by word should cover most cases, only do this if a word is longer than // line width. safeBreakIndex = breakBefore(visibleText, ellipsisIndex, BreakIterator.getCharacterInstance()); } ellipsisIndex = safeBreakIndex; } visibleText = visibleText.substring(0, ellipsisIndex); final CharSequence charOutput = ss.subSequence(0, ellipsisIndex); // Re-add ellipsis and convert to image spannedText.replace(0, spannedText.length(), charOutput); spannedText.append(ELLIPSIS); spannedText.setSpan(mEllipsisImage, ellipsisIndex, ellipsisIndex + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE); // Reset text and re-measure. super.setText(spannedText, BufferType.SPANNABLE); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } while (getLineCount() > getMaxLines() && getLineCount() > 1); requestFocus(); }
From source file:org.jivesoftware.util.StringUtils.java
/** * Converts a line of text into an array of lower case words using a * BreakIterator.wordInstance().<p> * * This method is under the Jive Open Source Software License and was * written by Mark Imbriaco./*from w ww . j a va 2 s . com*/ * * @param text a String of text to convert into an array of words * @return text broken up into an array of words. */ public static String[] toLowerCaseWordArray(String text) { if (text == null || text.length() == 0) { return new String[0]; } List<String> wordList = new ArrayList<>(); BreakIterator boundary = BreakIterator.getWordInstance(); boundary.setText(text); int start = 0; for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) { String tmp = text.substring(start, end).trim(); // Remove characters that are not needed. tmp = replace(tmp, "+", ""); tmp = replace(tmp, "/", ""); tmp = replace(tmp, "\\", ""); tmp = replace(tmp, "#", ""); tmp = replace(tmp, "*", ""); tmp = replace(tmp, ")", ""); tmp = replace(tmp, "(", ""); tmp = replace(tmp, "&", ""); if (tmp.length() > 0) { wordList.add(tmp); } } return wordList.toArray(new String[wordList.size()]); }
From source file:org.jivesoftware.community.util.StringUtils.java
public static String[] toLowerCaseWordArray(String text) { if (text == null || text.length() == 0) return new String[0]; ArrayList wordList = new ArrayList(); BreakIterator boundary = BreakIterator.getWordInstance(); boundary.setText(text);// ww w .ja v a 2 s. c om int start = 0; for (int end = boundary.next(); end != -1; end = boundary.next()) { String tmp = text.substring(start, end).trim(); tmp = replace(tmp, "+", ""); tmp = replace(tmp, "/", ""); tmp = replace(tmp, "\\", ""); tmp = replace(tmp, "#", ""); tmp = replace(tmp, "*", ""); tmp = replace(tmp, ")", ""); tmp = replace(tmp, "(", ""); tmp = replace(tmp, "&", ""); if (tmp.length() > 0) wordList.add(tmp); start = end; } return (String[]) wordList.toArray(new String[wordList.size()]); }
From source file:org.lnicholls.galleon.util.Tools.java
public static String[] layout(int width, FontMetrics metrics, String text) { ArrayList lines = new ArrayList(); if (text != null) { String line = ""; BreakIterator boundary = BreakIterator.getWordInstance(); boundary.setText(text);/*from w w w. j av a 2s . com*/ int start = boundary.first(); for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) { String word = text.substring(start, end); String trimmed = word.replaceAll(" ", ""); int metricsWidth = (line + word).length() * 20; if (metrics != null) metricsWidth = metrics.stringWidth(line + word); if (trimmed.equals("\n") || trimmed.equals("\r") || trimmed.equals("\r\n")) { lines.add(line.trim()); line = ""; } else if (metricsWidth > width) { lines.add(line.trim()); line = word; } else line = line + word; } if (line.trim().length() > 0) lines.add(line.trim()); } return (String[]) lines.toArray(new String[0]); }
From source file:com.tao.realweb.util.StringUtil.java
/** * Converts a line of text into an array of lower case words using a * BreakIterator.wordInstance().<p> * * This method is under the Jive Open Source Software License and was * written by Mark Imbriaco./*from w w w .j ava 2s. co m*/ * * @param text a String of text to convert into an array of words * @return text broken up into an array of words. */ public static String[] toLowerCaseWordArray(String text) { if (text == null || text.length() == 0) { return new String[0]; } List<String> wordList = new ArrayList<String>(); BreakIterator boundary = BreakIterator.getWordInstance(); boundary.setText(text); int start = 0; for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) { String tmp = text.substring(start, end).trim(); // Remove characters that are not needed. tmp = replace(tmp, "+", ""); tmp = replace(tmp, "/", ""); tmp = replace(tmp, "\\", ""); tmp = replace(tmp, "#", ""); tmp = replace(tmp, "*", ""); tmp = replace(tmp, ")", ""); tmp = replace(tmp, "(", ""); tmp = replace(tmp, "&", ""); if (tmp.length() > 0) { wordList.add(tmp); } } return wordList.toArray(new String[wordList.size()]); }
From source file:com.glaf.core.util.StringTools.java
public static String[] toLowerCaseWordArray(String text) { if (text == null || text.length() == 0) { return new String[0]; }// ww w.j a va2s . co m List<String> wordList = new java.util.ArrayList<String>(); BreakIterator boundary = BreakIterator.getWordInstance(); boundary.setText(text); int start = 0; for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) { String tmp = text.substring(start, end).trim(); tmp = replace(tmp, "+", ""); tmp = replace(tmp, "/", ""); tmp = replace(tmp, "\\", ""); tmp = replace(tmp, "#", ""); tmp = replace(tmp, "*", ""); tmp = replace(tmp, ")", ""); tmp = replace(tmp, "(", ""); tmp = replace(tmp, "&", ""); if (tmp.length() > 0) { wordList.add(tmp); } } return wordList.toArray(new String[wordList.size()]); }
From source file:com.juick.android.Utils.java
public static String getWordAtOffset(final String text, final int offset) { BreakIterator wordIterator = BreakIterator.getWordInstance(); wordIterator.setText(text);//from w ww . j a va2 s. co m int start = wordIterator.first(); for (int end = wordIterator.next(); end != BreakIterator.DONE; start = end, end = wordIterator.next()) { if ((end >= offset) && (end - start > 1)) { return text.substring(start, end); } } return null; }