Browse Source

[공통][New] 2줄 로직 수정

hyodong.min 7 years ago
parent
commit
0c257e8ea2
1 changed files with 64 additions and 17 deletions
  1. 64 17
      app/src/main/java/kr/co/zumo/app/lifeplus/util/StringUtil.java

+ 64 - 17
app/src/main/java/kr/co/zumo/app/lifeplus/util/StringUtil.java

@@ -8,6 +8,7 @@ import android.support.annotation.Nullable;
 import android.telephony.PhoneNumberUtils;
 import android.text.Html;
 import android.text.Spanned;
+import android.util.Log;
 
 import java.io.UnsupportedEncodingException;
 import java.security.MessageDigest;
@@ -323,6 +324,10 @@ public class StringUtil {
    * @return
    */
   public static String twoLine(String str) {
+    return twoLine(str, 13);
+  }
+
+  public static String twoLine(String str, int oneLineMax) {
 
     // 타이틀 줄바꿈이 사라져서 강제로 줄바꿈 해줌
     if (StringUtil.isEmpty(str)) {
@@ -330,32 +335,74 @@ public class StringUtil {
     }
 
     // \\n 문자를 줄바꿈 문자로 변경
-    str = str.replaceAll("\\\\n", System.getProperty("line.separator"));
+    str = str.replaceAll("\\\\n", "\n");
+    str = str.replaceAll("\r\n", "\n").replaceAll("\n", " ").replaceAll("  ", " ");
 
     // 줄바꿈이 있으면 그대로 전달
-    if (str.contains("\n")) {
+    /*if (str.contains("\n")) {
       return str;
     }
-    else {
-      if (str.length() < 13) {
+    else*/
+    {
+      if (str.length() < oneLineMax) {
         return str;
       }
-      String[] titles = str.split(" ");
-      int len = titles.length;
-      // 줄바꿈 위치 지정 6단어 이상이면 4번째부터, 그 이하는 3번째 단어에서 줄바꿈 해줌
-      int target = len > 5 ? 3 : 2;
-      // 줄바꿈 할 위치보다 단어가 적으면 하지 않음
-      if (target >= len) {
-        target = -1;
-      }
-      StringBuilder stringBuilder = new StringBuilder();
+      /**
+       * 빈 칸 순으로 잘라서 앞 뒤를 대조하여 비슷한 길이로 맞춘다.
+       */
+      int len = str.length();
+      float halfLen = len / 2;
+      int fromIndex = 0;
+      int splitIndex;
+      int targetIndex = -1;
+      float targetDistance = Integer.MAX_VALUE;
       for (int i = 0; i < len; ++i) {
-        if (i == target) {
-          stringBuilder.append("\n");
+        splitIndex = str.indexOf(" ", fromIndex + 1);
+//        Log.i("APP# StringUtil | twoLine", "|" + "str: " + str + ", split: " + splitIndex);
+        if (splitIndex > -1) {
+          float currentDistance = Math.abs(halfLen - splitIndex);
+//          Log.i("APP# StringUtil | twoLine", "|" + "cur: " + currentDistance + ", tar: " + targetDistance);
+          if (halfLen < splitIndex) {
+            // 절반 이전
+            if (currentDistance < targetDistance) {
+              // 이전보다 중심에 가까우면
+              targetDistance = currentDistance;
+              targetIndex = splitIndex; //i;
+            }
+          }
+          else {
+            // 절반 이후
+            if (currentDistance < targetDistance) {
+              // 이전보다 중심에 가까우면
+              targetDistance = currentDistance;
+              targetIndex = splitIndex; //i;
+            }
+          }
+          fromIndex = splitIndex;
+        }
+        else {
+          break;
         }
-        stringBuilder.append(titles[i]).append(" ");
       }
-      return stringBuilder.toString();
+      Log.i("APP# StringUtil | twoLine", "|" + "str: " + str + ", target: " + targetIndex);
+      return str.substring(0, targetIndex) + "\n" + str.substring(targetIndex + 1);
+
+//      String[] titles = str.split(" ");
+//      len = titles.length;
+//      // 줄바꿈 위치 지정 6단어 이상이면 4번째부터, 그 이하는 3번째 단어에서 줄바꿈 해줌
+//      int target = targetIndex + 1;
+//      // 줄바꿈 할 위치보다 단어가 적으면 하지 않음
+//      if (target >= len) {
+//        target = -1;
+//      }
+//      StringBuilder stringBuilder = new StringBuilder();
+//      for (int i = 0; i < len; ++i) {
+//        if (i == target) {
+//          stringBuilder.append("\n");
+//        }
+//        stringBuilder.append(titles[i]).append(" ");
+//      }
+//      return stringBuilder.toString();
     }
   }
 }