Calculating intrinsicContentSize for a NSTextField
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