-
[Android] TextView 부분 글자 바꾸기 (Span)Android 2020. 12. 14. 16:59반응형
* 부분 글자 색상 바꾸기
fun TextView.textColorSomeChange(changeText: String, @ColorRes color: Int) { val index = text.toString().indexOf(changeText) if (index != -1) { val ssb = SpannableString(text) ssb.setSpan( ForegroundColorSpan( ContextCompat.getColor( App.instance.context(), color ) ), index, index + changeText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ) text = ssb } }
* 부분 글자 사이즈 바꾸기
fun TextView.textSizeSomeChange(changeText: String,size : Int) { val index = text.toString().indexOf(changeText) if (index != -1) { val ssb = SpannableString(text) ssb.setSpan( AbsoluteSizeSpan(size,true), index, index+changeText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ) text = ssb } }
* 부분 글자 스타일 바꾸기
fun TextView.textPartTypeface(changeText: String, @StyleRes style: Int, isLast: Boolean = false) { val index = if (isLast) { text.toString().lastIndexOf(changeText) } else { text.toString().indexOf(changeText) } if (index != -1) { val ssb = SpannableString(text) ssb.setSpan( StyleSpan(style), index, index + changeText.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ) text = ssb } }
반응형'Android' 카테고리의 다른 글
Android App Bundle이 서명되지 않았습니다 (0) 2021.01.14 [Android] 카카오 로그인 v2 API with kotlin - 2 (0) 2020.12.24 Retrofit2 + RxJava 에서 Header 가져오기 (0) 2020.12.06 [Android] 파일 공유 대응 방법(File Provider) (0) 2020.11.13 [Android] 카카오 로그인 v2 API with kotlin - 1 (0) 2020.10.16