diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt new file mode 100644 index 000000000..32e4dc66f --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/page/ChapterProvider.kt @@ -0,0 +1,19 @@ +package io.legado.app.ui.widget.page + +import android.widget.TextView + + +class ChapterProvider { + + + fun getTextChapter(textView: TextView, content: String) { + + textView.text = content + val layout = textView.layout + val topOfLastLine = textView.height - textView.paddingTop - textView.paddingBottom - textView.lineHeight + val lineNum = layout.getLineForVertical(topOfLastLine) + val lastCharNum = layout.getLineEnd(lineNum) + } + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/widget/page/ContentTextView.kt new file mode 100644 index 000000000..4e6c22138 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/page/ContentTextView.kt @@ -0,0 +1,41 @@ +package io.legado.app.ui.widget.page + +import android.content.Context +import android.util.AttributeSet +import androidx.appcompat.widget.AppCompatTextView + + +class ContentTextView : AppCompatTextView { + constructor(context: Context) : super(context) + + constructor(context: Context, attrs: AttributeSet) : super(context, attrs) + + constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) + + /** + * 去除当前页无法显示的字 + * @return 去掉的字数 + */ + fun resize(): Int { + val oldContent = text + val newContent = oldContent.subSequence(0, getCharNum()) + text = newContent + return oldContent.length - newContent.length + } + + /** + * 获取当前页总字数 + */ + fun getCharNum(): Int { + return layout.getLineEnd(getLineNum()) + } + + /** + * 获取当前页总行数 + */ + fun getLineNum(): Int { + val layout = layout + val topOfLastLine = height - paddingTop - paddingBottom - lineHeight + return layout.getLineForVertical(topOfLastLine) + } +}