From 8f671ffcaa6f2962bee94c9c7a31237878ac6d64 Mon Sep 17 00:00:00 2001 From: Sam Stigler Date: Thu, 1 Mar 2012 19:49:52 -0800 Subject: [PATCH 1/2] Corrected for Lion Layout manager now works on Lion. There is still a slight bug with the sample code, but the text is now wrapping correctly. Signed-off-by: Sam Stigler --- AppDelegate.m | 6 +++--- HeightForWidthLayoutManager.m | 34 ++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/AppDelegate.m b/AppDelegate.m index 3c7d63b..2a52267 100644 --- a/AppDelegate.m +++ b/AppDelegate.m @@ -66,9 +66,9 @@ - (void)awakeFromNib relativeTo:@"text" attribute:kCAConstraintMidY]]; [textBackgroundLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintHeight - relativeTo:@"text" - attribute:kCAConstraintHeight - offset:2 * PADDING]]; + relativeTo:@"text" + attribute:kCAConstraintHeight + offset:2 * PADDING]]; [backgroundLayer addSublayer:textBackgroundLayer]; diff --git a/HeightForWidthLayoutManager.m b/HeightForWidthLayoutManager.m index bcae00b..d3903d4 100644 --- a/HeightForWidthLayoutManager.m +++ b/HeightForWidthLayoutManager.m @@ -2,17 +2,18 @@ // HeightForWidthManager.m // HeightForWidth // -// Created by Richard Hult on 2009-10-14. +// Created by Richard Hult on 2009-10-14, with revisions by Sam Stigler on 2012-03-01. // Copyright 2009 Richard Hult. All rights reserved. // #import "HeightForWidthLayoutManager.h" +#define BOTTOM_MARGIN 0; @implementation HeightForWidthLayoutManager - (NSFont *)fontForTextLayer:(CATextLayer *)layer -{ +{ NSFont *font = nil; // Convert the separate font and font size to an NSFont. There are four different ways @@ -45,14 +46,19 @@ - (NSAttributedString *)attributedStringForTextLayer:(CATextLayer *)layer NSDictionary *attributes = [NSDictionary dictionaryWithObject:[self fontForTextLayer:layer] forKey:NSFontAttributeName]; - return [[[NSAttributedString alloc] initWithString:layer.string - attributes:attributes] autorelease]; + return [[NSAttributedString alloc] initWithString:layer.string + attributes:attributes]; } +// verified 2/23/2012 that this returns the correct size for sstigler user. - (CGSize)frameSizeForTextLayer:(CATextLayer *)layer { + if ([layer string] == nil) { + return CGSizeZero; + } + NSAttributedString *string = [self attributedStringForTextLayer:layer]; - CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((CFAttributedStringRef)string); + CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)string); CGFloat width = layer.bounds.size.width; CFIndex offset = 0, length; @@ -71,7 +77,6 @@ - (CGSize)frameSizeForTextLayer:(CATextLayer *)layer } while (offset < [string length]); CFRelease(typesetter); - return CGSizeMake(width, ceil(y)); } @@ -80,6 +85,10 @@ - (CGSize)preferredSizeOfLayer:(CALayer *)layer if ([layer isKindOfClass:[CATextLayer class]] && ((CATextLayer *)layer).wrapped) { CGRect bounds = layer.bounds; bounds.size = [self frameSizeForTextLayer:(CATextLayer *)layer]; + if (bounds.origin.y < 0) { + bounds.origin.y -= bounds.origin.y; + } + bounds.origin.y += 3; layer.bounds = bounds; } @@ -90,16 +99,21 @@ - (void)layoutSublayersOfLayer:(CALayer *)layer { // First let the regular constraints kick in to set the width of text layers. [super layoutSublayersOfLayer:layer]; - + // Now adjust the height of any wrapped text layers, as their widths are known. for (CALayer *child in [layer sublayers]) { if ([child isKindOfClass:[CATextLayer class]]) { - [self preferredSizeOfLayer:child]; + [self preferredSizeOfLayer:(CATextLayer *)child]; + CGRect frame = child.frame; + if (frame.origin.y < 0) { + frame.origin.y -= child.frame.origin.y; + } + frame.origin.y += BOTTOM_MARGIN; // bottom margin + child.frame = frame; } } - + // Then let the regular constraints adjust any values that depend on heights. - [super layoutSublayersOfLayer:layer]; } @end From 16b4c21a4be317bfb512e7cb08f4d3ef2368d53e Mon Sep 17 00:00:00 2001 From: Sam Stigler Date: Thu, 1 Mar 2012 19:53:20 -0800 Subject: [PATCH 2/2] Corrected minor bug Corrected minor bug in usage of constant BOTTOM_MARGIN. Signed-off-by: Sam Stigler --- HeightForWidthLayoutManager.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HeightForWidthLayoutManager.m b/HeightForWidthLayoutManager.m index d3903d4..9a4a6f5 100644 --- a/HeightForWidthLayoutManager.m +++ b/HeightForWidthLayoutManager.m @@ -88,7 +88,7 @@ - (CGSize)preferredSizeOfLayer:(CALayer *)layer if (bounds.origin.y < 0) { bounds.origin.y -= bounds.origin.y; } - bounds.origin.y += 3; + bounds.origin.y += BOTTOM_MARGIN; layer.bounds = bounds; }