Skip to content

Instantly share code, notes, and snippets.

@chajka
Last active April 26, 2017 17:13
Show Gist options
  • Save chajka/e1b5327b6ec52f037811361df8610a42 to your computer and use it in GitHub Desktop.
Save chajka/e1b5327b6ec52f037811361df8610a42 to your computer and use it in GitHub Desktop.
NSTextCellViewの高さをselfのtextfiledから求めるmethod
- (CGFloat) heightOfView:(CGFloat)width
{
CGFloat width = view.bounds.size.width;
NSTextStorage *storage = [[NSTextStorage alloc] initWithString:comment.comment];
NSTextContainer *container = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(width, FLT_MAX)];
NSLayoutManager *manager = [[NSLayoutManager alloc] init];
[container setLineBreakMode:NSLineBreakByCharWrapping];
[manager addTextContainer:container];
[storage addLayoutManager:manager];
NSRange allText = NSMakeRange(0, storage.length);
[storage addAttribute:NSFontAttributeName value:view.textField.font range:allText];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:-2.2];
[storage addAttribute:NSParagraphStyleAttributeName value:style range:allText];
[manager glyphRangeForTextContainer:container];
CGRect rect = [manager usedRectForTextContainer:container];
CGFloat height = (rect.size.height);
return height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment