Re: Attributed strings - and bounding rects
Re: Attributed strings - and bounding rects
- Subject: Re: Attributed strings - and bounding rects
- From: Jonathan Mitchell <email@hidden>
- Date: Fri, 03 Mar 2017 14:23:40 +0000
Hi
I needed to do this for my pdf builder implementation:
Using the NSLayoutManager directly gives the best level of control.
https://github.com/ThesaurusSoftware/PDFPageBuilder/blob/master/PDFPageBuilder/TSPageTextItem.m#L95
NSRect boundingRect = self.containerRect;
// if no rect height defined then use default
BOOL containerHasExplicitHeight = YES;
if (boundingRect.size.height == 0) {
boundingRect.size.height = self.pageHeight - self.containerRect.origin.y ;
containerHasExplicitHeight = NO;
}
// allocate text container and layout manager
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:boundingRect.size];
textContainer.lineFragmentPadding = 0; // defaults to non zero
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[layoutManager addTextContainer:textContainer];
layoutManager.usesFontLeading = YES;
// allocate text storage and assign layout manager
self.textStorage = [[NSTextStorage alloc] initWithAttributedString:self.attributedString];
[self.textStorage addLayoutManager:layoutManager];
// do glyph layout
// NOTE: cannot quite get glyphRangeForBoundingRect configured correctly here
//self.glyphRange = [layoutManager glyphRangeForBoundingRect:boundingRect inTextContainer:textContainer];
self.glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
// get rect used for actual glyph layout
self.usedTextRect = [layoutManager usedRectForTextContainer:textContainer];
Jonathan
> On 3 Mar 2017, at 13:28, Igor Ranieri <email@hidden> wrote:
>
> Hi Peter.
>
>
> Have you tried also passing `usesLineFragmentOrigin` as one of the options? It works here.
>
> Here’s some code I’m currently using to achieve a similar effect:
>
> let sizeLimit = CGSize(width: 80.0, height: CGFloat.greatestFiniteMagnitude)
> let size = attrString.boundingRect(with: sizeLimit, options: [.usesLineFragmentOrigin, .usesFontLeading], context: nil).integral.size
>
> And it returns the expected size.
>
>
> best,
> elland
>
>
>> On 3. Mar 2017, at 14:18, Peter Hudson <email@hidden> wrote:
>>
>> Hi All
>>
>> I have done the following to try to determine the rect required to draw an attributed string( and thus the height of a row in a table view - on MacOS )
>>
>> I am trying to constrain my column width to 60 - and let the possible height of the row be a max of 1000.
>>
>>
>> let aString = anAttributedString
>> let constrainedSize = NSMakeSize(60.0, 1000.0)
>> let optns = NSStringDrawingOptions.usesFontLeading
>> let cntxt = NSStringDrawingContext()
>> cntxt.minimumScaleFactor = 1.0
>>
>> let aRect = aString?.boundingRect(with: constrainedSize, options: optns, context: cntxt)
>>
>>
>>
>> After the call to boundingRect, aRect has its width set to 60 and its height set to 18.
>>
>> This is not however correct for the contents of the attributed string - I would expect the height to be at least 200 or so with the width constrained to 60.
>>
>> Any help gratefully received.
>>
>> Peter
>>
>>
>>
>> _______________________________________________
>>
>> 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
>
>
> _______________________________________________
>
> 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
_______________________________________________
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