2000calories / flutter_easy_rich_text

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

Dynamic target string #17

Closed vishal-android-freak closed 2 years ago

vishal-android-freak commented 3 years ago

Hey, I want the users to define a bold ord like similar to how whatsapp does it. How can I define a dynamic target string because users can type any word between and I want the asterisks to get replaced and the words between them should become bold.

Any idea how we can achieve this?

2000calories commented 3 years ago

try matchBuilder

    EasyRichText(
      "TEST *bold font*. test *boldfont*.",
      patternList: [
        EasyRichTextPattern(
          targetString: '(\\*)(.*?)(\\*)',
          matchBuilder: (BuildContext context, RegExpMatch match) {
            return TextSpan(
              text: match[0].replaceAll('*', ''),
              style: TextStyle(fontWeight: FontWeight.bold),
            );
          },
        ),
      ],
    ),
developer-farhan commented 2 years ago

im trying to get bold and italic and underline formatting like WhatsApp and im using the following but im getting no result

patternList: [ ///bold font EasyRichTextPattern( targetString: '(\)(.?)(\)', matchBuilder: (BuildContext context, RegExpMatch match) { print(match[0]); return TextSpan( text: match[0].replaceAll('', ''), style: TextStyle(fontWeight: FontWeight.bold), ); }, ),

                          ///italic font
                          EasyRichTextPattern(
                            targetString: '(\_)(.*?)(\_)',
                            matchBuilder:
                                (BuildContext context, RegExpMatch match) {
                              print(match[0]);
                              return TextSpan(
                                text: match[0].replaceAll('_', ''),
                                style:
                                    TextStyle(fontStyle: FontStyle.italic),
                              );
                            },
                          ),
                        ],
Screenshot 2021-10-13 at 13 37 02

the other text turns black idk why when default color is white could you help me out

mselmanyildirim commented 8 months ago

try matchBuilder

    EasyRichText(
      "TEST *bold font*. test *boldfont*.",
      patternList: [
        EasyRichTextPattern(
          targetString: '(\\*)(.*?)(\\*)',
          matchBuilder: (BuildContext context, RegExpMatch match) {
            return TextSpan(
              text: match[0].replaceAll('*', ''),
              style: TextStyle(fontWeight: FontWeight.bold),
            );
          },
        ),
      ],
    ),

If your target string can be multiline, you can use this pattern:

(\\*)((.|\\n)*?)(\\*)

Only difference is that this one also includes new line character between your marks *