https://gist.github.com/SyllaJay/18674eab213b2fe90a2a
textview에서 자간을 사용하고싶었는데 letterSpacing은 21이상부터 적용되는 터라
커스텀한 걸 찾기로...
그리고 심심해서 위 링크에 있는 걸 코틀린으로 해봄
class BPLetterSpacingTextView : TextView {
private var spacing: Float = Spacing.NORAMAL
private var originalText: CharSequence = ""
constructor(context: Context) : super(context)
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
constructor(context: Context, attributeSet: AttributeSet, defStyle: Int) : super(context, attributeSet, defStyle)
fun getSpacing(): Float {
return this.spacing
}
fun setSpacing(spacing: Float) {
this.spacing = spacing
applySpacing()
}
override fun setText(text: CharSequence?, type: BufferType?) {
originalText = text!!
applySpacing()
}
override fun getText(): CharSequence {
return originalText
}
private fun applySpacing() {
if (false) return
val builder = StringBuilder()
for (i in 0..originalText.length - 1) {
builder.append(originalText[i])
if (i + 1 < originalText.length) {
builder.append("\u00A0")
}
}
val finalText = SpannableString(builder.toString())
if (builder.toString().length > 1) {
var i = 1
while (i < builder.toString().length) {
finalText.setSpan(ScaleXSpan(((spacing + 1) / 10)), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
i += 2
}
}
super.setText(finalText, TextView.BufferType.SPANNABLE)
}
object Spacing {
const val NORAMAL: Float = 0F
}
}
multiple constructor를 할 때 super처리도 꽤나 간결해서 좋은 듯
재밌는 점은 ChatSequence부분인데 반복문 사용시 이전에 charAt(i)로 했다면
굳이 그렇게 안하고 그냥 orginalText[i]로 처리한다는 점
'Programming > Android' 카테고리의 다른 글
안드로이드 Flavor 적용 (0) | 2017.08.02 |
---|---|
Glide 관련 이슈(centerCrop, context) (1) | 2017.06.15 |
Kotlin 사용시 주의사항? (0) | 2017.05.30 |
Kotlin 사용 (1) - singleton (Rx EventBus용) (0) | 2017.05.26 |
Kotlin 설치 (0) | 2017.05.20 |