• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Calculating intrinsicContentSize for a NSTextField
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Calculating intrinsicContentSize for a NSTextField


  • Subject: Calculating intrinsicContentSize for a NSTextField
  • From: Jeremy Hughes <email@hidden>
  • Date: Fri, 17 Mar 2017 17:18:34 +0000

I have a text field that I want to grow and shrink while it is being edited.

In order to do that, I have overridden intrinsicContentSize and textDidChange in a subclass of NSTextField:

	override var intrinsicContentSize: NSSize
	{
		if let cell = cell, cell.wraps
		{
			let size = CGSize(width: bounds.size.width, height: CGFloat.greatestFiniteMagnitude)

			let _ = attributedStringValue

			let cellSize = cell.cellSize(forBounds: CGRect(origin: CGPoint(), size: size))

			return cellSize
		}
		else
		{
			return super.intrinsicContentSize
		}
	}

	override func textDidChange(_ notification: Notification)
	{
		super.textDidChange(notification)

		invalidateIntrinsicContentSize()
	}

This works, but if I remove the call to attributedStringValue (whose result is discarded) the result of the following line fails to reflect changes in the bounds of the text, and the field fails to grow or shrink.

Presumably, calling attributedStringValue has a side effect which changes the way that cellSize:forBounds: is calculated.

Is there a way that I can get cellSize:forBounds: to return the correct value without relying on this side effect?

An alternative way of calculating the intrinsic content size would be to use:

attributedStringValue.boundingRect(with: size, options: .usesLineFragmentOrigin)

but this fails to take account of cell insets.

Jeremy


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Calculating intrinsicContentSize for a NSTextField
      • From: Daryle Walker <email@hidden>
  • Prev by Date: programmatically tell when spotlight/mds is indexing
  • Next by Date: Re: Auto-sized table cells, for macOS
  • Previous by thread: Re: programmatically tell when spotlight/mds is indexing
  • Next by thread: Re: Calculating intrinsicContentSize for a NSTextField
  • Index(es):
    • Date
    • Thread