String (@NotNull String initDescription, @NotNull FontMetrics fontMetrics, int maxWidth) { int low = 0; int high = initDescription.length() - 1; while (low <= high) { int mid = low + (high - low) / 2; String iteration = initDescription.substring(0, mid); int stringWidth = fontMetrics.stringWidth(iteration); if (stringWidth > maxWidth) { high = mid - 1; } else { low = mid + 1; } } int lastSpaceIndex = initDescription.lastIndexOf(" ", low - 1); return lastSpaceIndex == -1 ? initDescription : initDescription.substring(0, lastSpaceIndex); }