本项目是基于开源项目GSYRickText 进行harmonyos化的移植和开发的。
移植版本:v2.0.2
支持类似微博的文本效果,表情、@某人、话题、url链接等。 DEMO同时演示了不同输入框输入效果的使用。
//核心引入
implementation project(':draweetext')
implementation project(':xedittext')
项目根目录的build.gradle中的repositories添加:
mavenCentral()
module目录的build.gradle中dependencies添加:
implementation 'com.gitee.ts_ohos:draweetext:1.0.0'
implementation 'com.gitee.ts_ohos:xedittext:1.0.0'
RichTextFactory类,用来控制富文本的样式(字体颜色,大小等)以及如何添加富文本。
/**
* RichTextFactory Constructor
*
* @param context context
*/
public RichTextFactory(Context context) {
this.richTextBuilder = new RichTextBuilder();
this.textFormClickable = new TextForm();
this.textFormNormal = new TextForm();
RgbColor colorBlue = new RgbColor(COLOR_R_CLICKABLE_TEXT, COLOR_G_CLICKABLE_TEXT, COLOR_B_CLICKABLE_TEXT);
RgbColor colorRed = new RgbColor(255, 0, 0);
RgbColor colorWhite = new RgbColor(255, 255, 255);
RgbColor colorYellow = new RgbColor(255, 255, 0);
textFormClickable.setTextColor(colorYellow.asArgbInt());
textFormClickable.setTextSize(AttrHelper.fp2px(FP_TEXT_SIZE, context));
textFormClickable.setTextFont(Font.DEFAULT_BOLD);
textFormNormal.setTextSize(AttrHelper.fp2px(FP_TEXT_SIZE, context));
textFormNormal.setTextColor(colorWhite.asArgbInt());
// textFormNormal.setTextColor(ResourceTable.Color_grey_background);
}
... ...
/**
* Add default clickable style text to RichTextBuilder
*
* @param text text string
*/
public void addClickableText(String text) {
richTextBuilder.mergeForm(textFormClickable);
richTextBuilder.addText(text);
richTextBuilder.revertForm();
}
/**
* Add normal style text to RichTextBuilder
*
* @param text text string
*/
public void addNormalText(String text) {
richTextBuilder.mergeForm(textFormNormal);
richTextBuilder.addText(text);
richTextBuilder.revertForm();
}
普通富文本效果展示,使用自带API。
RichText richText = new RichTextBuilder()
.mergeForm(new TextForm().setUnderline(true).setTextColor(colorWhite.asArgbInt())).addText("这是测试文本: ")
.revertForm().mergeForm(new TextForm().setSubscript(true).setTextColor(colorYellow.asArgbInt())).addText("#话题话题#")
.revertForm().mergeForm(new TextForm().setTextColor(colorWhite.asArgbInt())).addText(" , @某个人 ")
.revertForm().mergeForm(new TextForm().setTextColor(colorRed.asArgbInt())).addText("@22222 @kkkk")
.revertForm().addText("\n").mergeForm(new TextForm().setStrikethrough(true).setTextColor(colorWhite.asArgbInt())).addText("\n添加电话号码:186 0000 1111")
.build();
自定义富文本效果展示,点击效果,连续添加文字
//添加富文本
RichTextFactory addRichTextFactory = new RichTextFactory(getContext());
addRichTextFactory.addNormalText("添加链接:");
addRichTextFactory.addClickableText("www.baidu.com");
addRichTextFactory.addNormalText(" 添加人物:");
addRichTextFactory.addClickableText("@22222 ");
addRichTextFactory.addNormalText("添加话题:");
addRichTextFactory.addClickableText("@啦啦啦啦啦");
RichText protocolPrivacyText = addRichTextFactory.getRichText();
DraweeTextView protocolPrivacyTextContainer = (DraweeTextView) findComponentById(ResourceTable.Id_protocolPrivacyText);
protocolPrivacyTextContainer.setRichText(protocolPrivacyText);
//添加点击效果
protocolPrivacyTextContainer.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
}
});
}
插入文字表情图片
PixelMapElement pixelMapElement = getPixelMapDrawable(MyMainAbilitySlice.this, ResourceTable.Media_e1);
DraweeSpan span = new DraweeSpan.Builder(
"https://i0.hdslb.com/bfs/vip/7a4cb0b644214d476ce198ddf6a7a0aa31311199.png").setLayout(WIDTH, HEIGHT)
.setPlaceHolderImage(pixelMapElement)
.build();
PixelMapElement pixelMapElement2 = getPixelMapDrawable(MyMainAbilitySlice.this, ResourceTable.Media_e2);
DraweeSpan span2 = new DraweeSpan.Builder(
"https://i0.hdslb.com/bfs/vip/7a4cb0b644214d476ce198ddf6a7a0aa31311199.png").setLayout(WIDTH, HEIGHT)
.setPlaceHolderImage(pixelMapElement2)
.build();
... ...
DraweeTextView draweeTextView1 = new DraweeTextView(MyMainAbilitySlice.this);
draweeTextView1.setText("O_O");
draweeTextView1.setTextSize(50);
draweeTextView1.setAroundElements(span, null, span2, null);
draweeTextView1.setTextColor(Color.WHITE);
draweeTextView1.setPadding(8, 8, 8, 8);
MIT License
Copyright (c) 2017 Shuyu Guo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型