LoginSignup
0
1

More than 1 year has passed since last update.

Android, iOSで文字の幅を取得する方法

Last updated at Posted at 2021-05-12

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")
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1