ForegroundColorSpan is not applied to ReplacementSpan

后端 未结 1 503
情歌与酒
情歌与酒 2020-12-19 12:34

I\'m trying to utilize ReplacementSpans to format the input in a EditText Field (without modifying the content):

public class SpacerSpan extends ReplacementS         


        
相关标签:
1条回答
  • 2020-12-19 13:08

    CommonWare pointed me in the right direction. It seems like ReplacementSpans are rendered before any CharacterStyleSpan are consulted [1]

    A possible (but ugly) fix is to implement a custom ForegroundColorSpan that extends MetricAffectingSpan (MetricAffectingSpans are consulted before ReplacementSpans are drawn [1]).

    public class FontColorSpan extends MetricAffectingSpan {
    
        private int mColor;
    
        public FontColorSpan(int color) {
            mColor = color;
        }
    
        @Override
        public void updateMeasureState(TextPaint textPaint) {
            textPaint.setColor(mColor);
        }
        @Override
        public void updateDrawState(TextPaint textPaint) {
            textPaint.setColor(mColor);
        }
    } 
    

    I guess this is a bug that should be reported?

    [1]http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.2_r1/android/text/TextLine.java#936

    0 讨论(0)
提交回复
热议问题