EyrisCrafts / wordSelectableText

BSD 3-Clause "New" or "Revised" License
15 stars 2 forks source link

text wrap #5

Closed walidbou6 closed 1 year ago

walidbou6 commented 1 year ago

first thank for your hard work, second, I have a question I'm using your package but noticed that the text wrapping split's the words (by the way I'm working with a Japanese text) for example: the input text: hello world output: hello wo\nrld (when the word at the end of the line it's split's half above and rest at bottom) I hope you get me and if you have any idea on how to solve this, please let me know.

EyrisCrafts commented 1 year ago

Hey ! Can you give me a sample input that split the word? Thanks

walidbou6 commented 1 year ago
String text = """
      むかしむかし にんげんも うまれていない おおむかしの あるとしの くれの ことです。
      かみさまが どうぶつたちに いいました。 もうすぐ おしょうがつだ。 がんたんには みんな わたしのところに 
      きなさい。 そして さきに きたものから 12ばんめまでを そのとしの たいしょうと しよう」ところが 
      うっかりものの ねこは  あつまるひを わすれたので ともだちのねずみに ききました。""";

note: I have tested with the english text it works fine I think the problem is with the Japanese text,,

walidbou6 commented 1 year ago

the issue is at the flutter Text widget can't split the Japanese string into words, so to fix this issue I need to figure out how to update String's components method to split by space or something like that.

walidbou6 commented 1 year ago

I couldn't find any solution except of building my own wrapper I have used this function (I will post it here may someone in the future need it)

just one issue I can't real know the exact max length to set for each line, I have tested and found 17 is the ideal one for me but that may change based on the phone screen size and the font size,


String wrapper(String text) {
List<String> stext = text.split(' ');
List<String> rtext = [];
String wrap = '';
while (true) {
  if (stext.length != 0) {
    var txt = stext[0];
    if ((wrap.length + txt.length + 1) < 17) {
      wrap += txt + '  ';
      stext.removeAt(0);
    } else {
      rtext.add(wrap);
      wrap = txt + '  ';
      stext.removeAt(0);
    }
  } else {
    rtext.add(wrap);
    break;
  }
}
var fullText = StringBuffer();
fullText.writeAll(rtext, '\n');
return fullText.toString();

}