react-native-navigation的迁移库

TextViewUtils.java 1.1KB

123456789101112131415161718192021222324252627282930
  1. package com.reactnativenavigation.utils;
  2. import android.text.SpannableString;
  3. import android.text.Spanned;
  4. import android.text.SpannedString;
  5. import android.text.style.AbsoluteSizeSpan;
  6. import android.text.style.ForegroundColorSpan;
  7. import android.widget.TextView;
  8. import androidx.annotation.ColorInt;
  9. public class TextViewUtils {
  10. @ColorInt
  11. public static int getTextColor(TextView view) {
  12. SpannedString text = new SpannedString(view.getText());
  13. ForegroundColorSpan[] spans = text.getSpans(0, text.length(), ForegroundColorSpan.class);
  14. return spans.length == 0 ? view.getCurrentTextColor() : spans[0].getForegroundColor();
  15. }
  16. public static float getTextSize(TextView view) {
  17. SpannedString text = new SpannedString(view.getText());
  18. AbsoluteSizeSpan[] spans = text.getSpans(0, text.length(), AbsoluteSizeSpan.class);
  19. return spans.length == 0 ? -1 : spans[0].getSize();
  20. }
  21. public static void setColor(SpannableString span, int color) {
  22. span.setSpan(new ForegroundColorSpan(color), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  23. }
  24. }