swift 计算字体的宽度

2018-09-15  本文已影响6人  孤雁_南飞

/**

 func labelWidth(_ text: String, _ height: CGFloat) -> CGFloat {
    let size = CGSize(width: 2000, height: height)
    let font = UIFont(name: "PingFang-SC-Regular", size: height)!
    let attributes = [NSAttributedStringKey.font: font]
    let labelSize = NSString(string: text).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
    return labelSize.width
}
上一篇 下一篇

猜你喜欢

热点阅读