-
Notifications
You must be signed in to change notification settings - Fork 1
Hi #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Hi #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
@@ -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 += BOTTOM_MARGIN; | ||
| 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]; | ||
|
|
||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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.