Last active
April 26, 2017 17:13
-
-
Save chajka/e1b5327b6ec52f037811361df8610a42 to your computer and use it in GitHub Desktop.
NSTextCellViewの高さをselfのtextfiledから求めるmethod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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