The method to parse rich text is something like recursively descending. I'm open to PR that could improve performance, fix bugs and anything make this library better.
You can see default tags in this. Try to customize your own tags by the series of set***Labels(String... labels) methods.
For image labels, \w represents width ,\h represents height and \u represents the url of image.
For attachment labels, \s repersents attachment id.
For color labels, \s represents color (e.g. red).
For url labels, \s represents url.
For quote labels, \p represents the id of the quoted content, \m represents the name of the person quoted.
For example, if you customize image label as <img height=\h width=\w>\u</img>, the text <img height=200 width=100>https://example.img</img> will be treated as a image, its height is 200, width is 100 and its url is https://example.img . And of course you can just customize image label as <img>\u</img>, then the original size of image will be respected.
Attachment
This library support attachment. If there are attachments to show, create a class and inherit abstract class Attachment, then invoke flexibleRichTextView.setText(String richText, List<Attachment> attachments).
If attachment.isImage() is true, the attachment will be shown as image, or it will be shown as a link (please implement getText() method whose return value is the link text, and getUrl() method whose return value is the url of image, see Callbacks for more information). Both images and links produced from attachments will be shown at the bottom of view.
If you need the attachment embedded in text, please implement getAttachmentId() method and provide the id in attachment tag.
For example, the default attachment tag is [attachment:\s], where \s represents attachment id. So you should invoke
String textWithAttachment = "This is an attachment\n" +
"[attachment:3918dbe1ac]\n" +
"The attachment is above";
flexibleRichTextView.setText(textWithAttachment, attachmentList);
If the list attachmentList contains an attachment whose id equals 3918dbe1ac, it will be shown embedded in text.
Quote
You can not only customize the tags of quote, but also the layout of QuoteView.
The root element of layout must be a QuoteView and the first child of it must be a FrameLayout. You can specify a button that takes the role of expanding or collapsing the QuoteView on click by adding app:buttonId=your_button_id in QuoteView. When the button is clicked, the onButtonClick(View view, boolean collapsed) method of OnViewListener will be invoked, you could modify the text of button or do other things according to the status of QuoteView.
LaTeX
See this for LaTeX tags. They cannot be customized temporarily.
Table
The grammer for table is the same as GitHub, except for the pipes on either end of the table cannot be omitted.
Callbacks
In interface FlexibleRichTextView.OnViewClickListener, three callbacks are declared, including OnImgClick(ImageView imageView), OnAttClick(Attachment attachment) and OnQuoteButtonClick(View view, boolean collapsed).
For attachments, if an attachment is shown as image, OnImgClick will be called when clicking the attachment, OnAttClick is only called when an attachment shown as link is clicked.
Lisence
This library is licensed under Apache 2.0. The full license text can be found in the LICENSE file.
请发表评论