Created
January 22, 2019 16:25
-
-
Save ZhangDezhi/e8e0287cbf77aad9b05f3f8df566cf20 to your computer and use it in GitHub Desktop.
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
//参考: | |
https://stackoverflow.com/questions/10463680/how-to-let-nstextfield-grow-with-the-text-in-auto-layout | |
//未看完: | |
https://stackoverflow.com/questions/24618703/automatically-wrap-nstextfield-using-auto-layout | |
//自定义视图 | |
#import "YMTextField.h" | |
@implementation YMTextField | |
- (void)drawRect:(NSRect)dirtyRect { | |
[super drawRect:dirtyRect]; | |
// Drawing code here. | |
} | |
//NSView 返回视图本身认为的内在内容大小。 | |
-(NSSize)intrinsicContentSize | |
{ | |
if ( ![self.cell wraps] ) { | |
return [super intrinsicContentSize]; | |
} | |
NSRect frame = [self frame]; | |
CGFloat width = frame.size.width; | |
//使框架非常高,同时保持宽度 | |
frame.size.height = CGFLOAT_MAX; | |
// 计算框架内的新高度 | |
// 几乎无限的高度。 | |
CGFloat height = [self.cell cellSizeForBounds: frame].height; | |
NSLog(@"%f,%f",width,height); | |
return NSMakeSize(width, height); | |
} | |
// you need to invalidate the layout on text change, else it wouldn't grow by changing the text | |
- (void)textDidChange:(NSNotification *)notification | |
{ | |
[super textDidChange:notification]; | |
[self invalidateIntrinsicContentSize]; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment