2000calories / flutter_easy_rich_text

The EasyRichText widget provides an easy way to use RichText.
https://pub.dev/packages/easy_rich_text
MIT License
80 stars 33 forks source link

Cannot match Chinese characters #3

Closed tasselx closed 4 years ago

tasselx commented 4 years ago

19F3C999-90E3-4874-96E9-6F6439E255D6 67559EF4-941D-4B50-AD1B-B4CBD3F95B78

I'm glad to see the library you published on pub dev, but it is a bit imperfect because it cannot support Chinese characters. Would you fix this bug?

2000calories commented 4 years ago

Sure. I am working on it. The reason is that the word boundary between Chinese characters is not a space. I need to find a way to make it friendly for both English and Chinese.

2000calories commented 4 years ago

Please try the develop branch first. I will publish a new version later. config in pubspec.yaml

easy_rich_text:
    git:
      url: https://github.com/2000calories/flutter_easy_rich_text.git
      ref: develop

If you have content mixed with Chinese and English like the example you show above, you need to add a space to stringBeforeTarget

  EasyRichText(
    "I want 世界你好 here. I want not 世界你好 font here.",
    patternList: [
      EasyRichTextPattern(
        targetString: '世界',
        stringBeforeTarget: 'want ',
        style: TextStyle(color: Colors.blue),
      ),
    ],
  ),
tasselx commented 4 years ago

Thank you for your quick response, it seems to solve some problems of Chinese characters, but I found some other problems, can't do some styles on the characters, paste some of my code

372607A6-F0F1-474E-83A4-814F091C37C7


          "left is right 《世界你好》thi",
           defaultStyle: TextStyle(
             color: Colors.red
           ),
           patternList: [
             EasyRichTextPattern(
               targetString: 'ht',
               style: TextStyle(color: Colors.orange)
             ),
             EasyRichTextPattern(
               targetString: '你好',
               style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue),
             ),
             EasyRichTextPattern(
               targetString: '《',
               style: TextStyle(fontWeight: FontWeight.bold,color: Colors.lightBlue),
             ),
             EasyRichTextPattern(
               targetString: '》',
               style: TextStyle(fontWeight: FontWeight.bold,color: Colors.lightGreenAccent),
             )
           ],
        ),```
2000calories commented 4 years ago

Try the updated develop branch and set matchWordBoundaries: false. See the comments in the following code for explanation.

  EasyRichText(
    "left is right 《世界你好》thi",
    defaultStyle: TextStyle(color: Colors.red),
    patternList: [
      EasyRichTextPattern(
          // by default a complete word matches the targetString will be styled
          // to match the targetString as a part of a word, set matchWordBoundaries: false
          targetString: 'ht',
          matchWordBoundaries: false,
          style: TextStyle(color: Colors.orange)),
      EasyRichTextPattern(
        targetString: '你好',
        style: TextStyle(
            fontWeight: FontWeight.bold, color: Colors.blue),
      ),
      EasyRichTextPattern(
        //《 is not recognized as Chinese, but it works by setting matchWordBoundaries: false
        targetString: '《',
        matchWordBoundaries: false,
        style: TextStyle(
            fontWeight: FontWeight.bold, color: Colors.lightBlue),
      ),
      EasyRichTextPattern(
        targetString: '》',
        matchWordBoundaries: false,
        style: TextStyle(
            fontWeight: FontWeight.bold,
            color: Colors.lightGreenAccent),
      )
    ],
  ),
tasselx commented 4 years ago

I tested the code and it works. If I have some text with the same content and I only match the first one, or one of them, or all of them, how do I do it? , but this is not a bug, maybe a requirement . In addition to these authors, is there any plan to support gesture response and some regular matches such as url, phone number, email, etc.

2000calories commented 4 years ago

--If I have some text with the same content and I only match the first one, or one of them, or all of them, how do I do it? Please open another issue to request this function and I will follow you up once it is ready.

--In addition to these authors, is there any plan to support gesture response and some regular matches such as url, phone number, email, etc. Integration with url_launcher is in my plan. Gesture response may be too much for this widget.

2000calories commented 4 years ago

Just a reminder, please switch to the lastest version instead of the develop branch.

dependencies:
  easy_rich_text: '^0.3.2'