Skip to content
Open

Hi #2

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why GitHub's saying something changed here... all I did was try (unsuccessfully) to get the text background layer to work correctly on Lion.


[backgroundLayer addSublayer:textBackgroundLayer];

Expand Down
34 changes: 24 additions & 10 deletions HeightForWidthLayoutManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought I'd give myself a little credit (-:

// Copyright 2009 Richard Hult. All rights reserved.
//

#import "HeightForWidthLayoutManager.h"

#define BOTTOM_MARGIN 0;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to make the class a little more suitable for general-use


@implementation HeightForWidthLayoutManager

- (NSFont *)fontForTextLayer:(CATextLayer *)layer
{
{
NSFont *font = nil;

// Convert the separate font and font size to an NSFont. There are four different ways
Expand Down Expand Up @@ -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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ARC prompted me to add the __bridge keyword in. Hopefully this won't mess things up for non-ARC projects.

CGFloat width = layer.bounds.size.width;

CFIndex offset = 0, length;
Expand All @@ -71,7 +77,6 @@ - (CGSize)frameSizeForTextLayer:(CATextLayer *)layer
} while (offset < [string length]);

CFRelease(typesetter);

return CGSizeMake(width, ceil(y));
}

Expand All @@ -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 += BOTTOM_MARGIN;
layer.bounds = bounds;
}

Expand All @@ -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];

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently this messes things up on Lion -- if I remember correctly, it causes super to discard all changes made by the current layout manager (self).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what made the actual "height for width" part work though, that is that the constraints could adjust the height of the box as needed when the width was changed. I'm not sure how to get that part fixed in Lion.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about for the background gradient, but in my app I have just a standard CATextLayer with a semi-transparent black background. It works for that; I've NSLog'd the layer sizes to be sure. As I recall, leaving this line in would basically mean that the text layer would have a height of 1, or 0.

// 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;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little brute-force correction.

if (frame.origin.y < 0) {
frame.origin.y -= child.frame.origin.y;
}
frame.origin.y += BOTTOM_MARGIN; // bottom margin
child.frame = frame;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above shouldn't be needed, it's already done in preferreedSizeOfLayer:, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, my mistake. The code for adjusting the origin only has to be in one of the two methods, not both. The copy in preferredSizeOfLayer: is just left-over from some debugging I was trying to do in my app.

}

// Then let the regular constraints adjust any values that depend on heights.
[super layoutSublayersOfLayer:layer];
}

@end