NSAttributedString的bo​​undingRectWithSize返回错误的大小 [英] boundingRectWithSize for NSAttributedString returning wrong size

查看:69
本文介绍了NSAttributedString的bo​​undingRectWithSize返回错误的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取属性字符串的rect,但是boundingRectWithSize调用不考虑我传入的大小,而是返回具有单行高度而不是大高度的rect(这是一个长字符串) .我通过传递一个很大的高度值和0来进行实验,如下面的代码所示,但是返回的rect始终是相同的.

I am trying to get the rect for an attributed string, but the boundingRectWithSize call is not respecting the size I pass in and is returning a rect with a single line height as opposed to a large height (it is a long string). I have experimented by passing in a very large value for the height and also 0 as in the code below, but the rect returned is always the same.

CGRect paragraphRect = [attributedText boundingRectWithSize:CGSizeMake(300,0.0)
  options:NSStringDrawingUsesDeviceMetrics
  context:nil];

这是损坏的吗,还是我需要做其他事情才能使它返回一个用于包装文本的矩形?

Is this broken, or do I need to do something else to have it returned a rect for wrapped text?

推荐答案

好像没有提供正确的选项.对于包装标签,请至少提供:

Looks like you weren't providing the correct options. For wrapping labels, provide at least:

CGRect paragraphRect =
  [attributedText boundingRectWithSize:CGSizeMake(300.f, CGFLOAT_MAX)
  options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
  context:nil];

注意:如果原始文本宽度小于300.f,则不会出现换行符,因此请确保装订大小正确,否则仍然会得到错误的结果.

Note: if the original text width is under 300.f there won't be line wrapping, so make sure the bound size is correct, otherwise you will still get wrong results.

这篇关于NSAttributedString的bo​​undingRectWithSize返回错误的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆