怎样手动计算多行字符串的高度?

How to calculate multi-lines String’s height ?

使用的具体方法

extension NSString {
    @available(iOS 7.0, *)
    open func draw(with rect: CGRect, options: NSStringDrawingOptions = [], attributes: [NSAttributedString.Key : Any]? = nil, context: NSStringDrawingContext?)

    @available(iOS 7.0, *)
    open func boundingRect(with size: CGSize, options: NSStringDrawingOptions = [], attributes: [NSAttributedString.Key : Any]? = nil, context: NSStringDrawingContext?) -> CGRect
}

参数说明

size

The size of the rectangle to draw in. 绘制String的矩形区域大小

options

String drawing options. 字符串绘制选项

具体Enum值如下(Objective-C中是以枚举来实现)

@available(iOS 6.0, *)
public struct NSStringDrawingOptions : OptionSet {

    public init(rawValue: Int)

    // Uses the line fragment origin instead of the baseline origin.
    public static var usesLineFragmentOrigin: NSStringDrawingOptions { get }
    // Uses the font leading for calculating line heights.
    public static var usesFontLeading: NSStringDrawingOptions { get }
    // Uses image glyph bounds instead of typographic bounds.
    public static var usesDeviceMetrics: NSStringDrawingOptions { get }
    // Truncates and adds the ellipsis character to the last visible line if the text doesn't fit into the specified bounds.
    @available(iOS 6.0, *)
    public static var truncatesLastVisibleLine: NSStringDrawingOptions { get }
}

想要理解上面4个值的含义决非易事,文档好像什么都说了,又好像什么也没说。。。

attributes

A dictionary of text attributes to be applied to the string. These are the same attributes that can be applied to an NSAttributedString object, but in the case of NSString objects, the attributes apply to the entire string, rather than ranges within the string. 同属性字符串

context

The string drawing context to use for the receiver, specifying minimum scale factor and tracking adjustments. 从来没用过,传nil.

注意点

官方说明

To correctly draw and size multi-line text, pass usesLineFragmentOrigin in the options parameter.
This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function.
This method returns the actual bounds of the glyphs in the string. Some of the glyphs (spaces, for example) are allowed to overlap the layout constraints specified by the size passed in, so in some cases the width value of the size component of the returned CGRect can exceed the width value of the size parameter.

  1. 计算多行String, 一定要添加usesLineFragmentOrigin选项;
  2. 计算绘制String所需的高度,一定要用ceil包裹,也就是需要向上取整;

show a demo code


let floatHeight = "hello boundingRect".boundingRect(with: CGSize(width: UIScreen.main.bounds.size.width - 32, height: CGFloat(MAXFLOAT)), options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [.font: UIFont(name: "PingFangSC-Light", size: 20)!], context: nil).size.height
let height = ceil(floatHeight)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

依旧风轻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值