Skip to content

Instantly share code, notes, and snippets.

@swillits
Last active December 2, 2016 01:11
Show Gist options
  • Save swillits/acac337218775b29776ca94c3932e770 to your computer and use it in GitHub Desktop.
Save swillits/acac337218775b29776ca94c3932e770 to your computer and use it in GitHub Desktop.
NSAttributedString drawing squeezing broken -- allowsDefaultTighteningForTruncation
Natural attrStr.size is (width = 1578, height = 15)

NSRect stringRect = ...
[attrString drawWithRect:stringRect options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine];

Drawn with size {500, 60} works as expected (wraps, no squeezing):

Drawn with size {500, 15} squeezes which is probably wrong, but truncates which is right:

Drawn with Monaco font and size {500, 15} it starts to squeeze ridiculously:

{500, 15} but no NSStringDrawingTruncatesLastVisibleLine, and it's better:

But I want the truncated line…

Answer!

Set allowsDefaultTighteningForTruncation to NO on the paragraph style applied to the whole attributed string.

See: https://developer.apple.com/library/prerelease/content/releasenotes/AppKit/RN-AppKitOlderNotes/index.html#10_11DynamicTracking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment