implementation 'cn.yc:YCCustomTextLib:2.1.3'
在布局中引用,HyperTextEditor是编辑富文本,HyperTextView是预览富文本
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MissingDefaultResource">
<com.ns.yc.yccustomtextlib.edit.view.HyperTextEditor
android:id="@+id/hte_content"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:textSize="16sp"
app:editor_text_line_space="6dp"
app:editor_image_height="500"
app:editor_image_bottom="10"
app:editor_text_init_hint="在这里输入内容"
app:editor_text_size="16sp"
app:editor_text_color="@android:color/black"/>
<com.ns.yc.yccustomtextlib.edit.view.HyperTextView
android:id="@+id/htv_content"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:ht_view_text_line_space="6dp"
app:ht_view_image_height="0"
app:ht_view_image_bottom="10"
app:ht_view_text_size="16sp"
app:ht_view_text_color="@android:color/black"/>
</LinearLayout>
在编辑富文本状态时,HyperTextEditor控件
//插入图片
hte_content.insertImage(imagePath);
//对外提供的接口, 生成编辑数据上传
List<HyperEditData> editList = hte_content.buildEditData();
hte_content.setOnHyperListener(new OnHyperEditListener() {
@Override
public void onImageClick(View view, String imagePath) {
//图片点击事件
ToastUtils.showRoundRectToast("图片点击"+imagePath);
}
@Override
public void onRtImageDelete(String imagePath) {
//图片删除成功事件
ToastUtils.showRoundRectToast("图片删除成功");
}
@Override
public void onImageCloseClick(final View view) {
//图片删除图片点击事件
}
});
hte_content.setOnHyperChangeListener(new OnHyperChangeListener() {
@Override
public void onImageClick(int contentLength, int imageLength) {
//富文本的文字数量,图片数量统计
tv_length.setText("文字共"+contentLength+"个字,图片共"+imageLength+"张");
}
});
在预览富文本状态时,HyperTextView控件
//清除所有文本
htv_content.clearAllLayout();
//将html数据转化成集合
List<String> textList = HyperLibUtils.cutStringByImgTag(html);
//省略部分代码,具体看demo
if (text.contains("<img") && text.contains("src=")) {
//imagePath可能是本地路径,也可能是网络地址
String imagePath = HyperLibUtils.getImgSrc(text);
//在特定位置添加ImageView
htv_content.addImageViewAtIndex(htv_content.getLastIndex(), imagePath);
} else {
//在特定位置插入TextView
htv_content.addTextViewAtIndex(htv_content.getLastIndex(), text);
}
htv_content.setOnHyperTextListener(new OnHyperTextListener() {
@Override
public void onImageClick(View view, String imagePath) {
//图片点击事件
}
});
<declare-styleable name="HyperTextEditor" >
<!--父控件的左和右padding-->
<attr name="editor_layout_right_left" format="integer" />
<!--父控件的上和下padding-->
<attr name="editor_layout_top_bottom" format="integer" />
<!--插入的图片显示高度-->
<attr name="editor_image_height" format="integer" />
<!--两张相邻图片间距-->
<attr name="editor_image_bottom" format="integer" />
<!--文字相关属性,初始提示信息-->
<attr name="editor_text_init_hint" format="string" />
<!--文字大小-->
<attr name="editor_text_size" format="dimension" />
<!--文字颜色-->
<attr name="editor_text_color" format="color" />
<!--文字行间距-->
<attr name="editor_text_line_space" format="dimension" />
<!--删除图片图标的位置-->
<attr name="editor_del_icon_location" format="enum">
<enum name="top_left" value="1" />
<enum name="top_right" value="2" />
<enum name="bottom_left" value="3" />
<enum name="bottom_right" value="4" />
</attr>
</declare-styleable>
Copyright 2017 yangchong211(github.com/yangchong211)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.