Android
test.kt
val paint = TextPaint().also {
it.textSize = 10
if (!isInEditMode) {
it.typeface = ResourcesCompat.getFont(context, R.font.your_font)
}
}
paint.measureText("hoge")
iOS
UIFont+.swift
import UIKit
extension UIFont {
/// テキストのwidthを取得する
func measureText(text: String) -> CGFloat {
let str = NSString(string: text)
return str.size(withAttributes: [.font: self]).width
}
}
test.swift
let font = UIFont(name:"your_font", size: 10.0)
font.measureText("hoge")