3

for example:

import Foundation
import UIKit

var str = NSString(string: "saldkjaskldjhf")

var font = UIFont.systemFontOfSize(14.0)
var attributes:[String:AnyObject]   = [NSFontAttributeName: font]
var attriStrWithoutParagraph = NSAttributedString(string: str, attributes: attributes)

var size = attriStrWithoutParagraph.boundingRectWithSize(CGSize(width: CGFloat.max, height: CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)

var paragraphstyle = NSMutableParagraphStyle()
paragraphstyle.firstLineHeadIndent = CGFloat(20)
attributes[NSParagraphStyleAttributeName] = paragraphstyle
attriStrWithoutParagraph = NSAttributedString(string: str, attributes: attributes)
size = attriStrWithoutParagraph.boundingRectWithSize(CGSize(width: CGFloat.max, height: CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, context: nil)

here is the output:

(0.0,0.0,87.276,16.702)
(0.0,0.0,87.276,16.702)

we can see the result is the same, so the firstlineindent is not considered in why it works like this???

0oneo
  • 695
  • 7
  • 10
  • Indenting the first line doesn't necessarily change the bounding rect. It would only change it if it forced the last line of the paragraph to wrap to another line. – Ken Thomases Sep 27 '14 at 05:40
  • @KenThomases how to force the last line to wrap to another line? – 0oneo Sep 27 '14 at 05:46

1 Answers1

4

You're specifying very large (effectively infinite) values (CGFloat.max) for the size that you're passing to -boundingRectWithSize:options:. So, the text will never wrap. It will always be laid out in one long line.

Furthermore, the docs for -boundingRectWithSize:options: say:

The origin of the rectangle returned from this method is the first glyph origin.

So, the result is always relative to where the first glyph is placed. You're basically measuring the size of the line. The indent doesn't change the size of the line. It changes where the first glyph is placed, but the result is relative to the first glyph, so it doesn't change the result.

It would change the result if you were providing a real limit for the width and making the paragraph wrap. In that case, the second line would be "outdented" relative to the first line (and the first glyph), so the bounding rectangle would change as you change the firstLineHeadIndent.

You can simply apply the desired indent yourself. That is, after you get the bounding rect, add the indent distance to the X coordinate of the origin (edit: or to the width, if you want a rect encompassing the indent and not just the text positioned by the indent). (Although it's not clear to me what it could mean to indent text in an "infinite" space.)

You could also provide an actual bounding size for your desired destination for the text.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • boundingRect(with:options) is deprecated in favor of boundingRect(with:options:context:), which doesn't mention the first glyph as the origin, but the issue seems to be the same. – Alexander Smith Aug 19 '18 at 05:15