NSTextField And AutoLayout: Autogrow height -> Programmatically
来源:互联网 发布:网络摄像头ip扫描工具 编辑:程序博客网 时间:2024/05/21 07:10
【solution1】
Hence, a custom subclass of NSTextField
can override this method to return a better value, such as the one provided by the cell's cellSizeForBounds:
method:
- (NSSize) intrinsicContentSize;{ const CGFloat magic = -4; NSSize rv; if ([[self cell] wraps] && self.frame.size.height > 1) rv = [[self cell] cellSizeForBounds:NSMakeRect(0, 0, self.bounds.size.width + magic, 20000)]; else rv = [super intrinsicContentSize]; return rv;}- (void) layout;{ [super layout]; [self invalidateWordWrappedContentSizeIfNeeded];}- (void) setFrameSize:(NSSize)newSize;{ [super setFrameSize:newSize]; [self invalidateWordWrappedContentSizeIfNeeded];}- (void) invalidateWordWrappedContentSizeIfNeeded;{ NSSize a = m_previousIntrinsicContentSize; NSSize b = self.intrinsicContentSize; if (!NSEqualSizes(a, b)) { [self invalidateIntrinsicContentSize]; } m_previousIntrinsicContentSize = b;}The reason I didn’t use
cellSizeForBounds
as you originally suggested was I couldn’t solve your problem – even when invalidating the intrinsic content size of the cell, cellSizeForBounds:
continued to return the old size.【solution2】
https://stackoverflow.com/questions/14107385/getting-a-nstextfield-to-grow-with-the-text-in-auto-layout
@interface TSTTextGrowth(){ BOOL _hasLastIntrinsicSize; BOOL _isEditing; NSSize _lastIntrinsicSize;}@end@implementation TSTTextGrowth- (void)textDidBeginEditing:(NSNotification *)notification{ [super textDidBeginEditing:notification]; _isEditing = YES;}- (void)textDidEndEditing:(NSNotification *)notification{ [super textDidEndEditing:notification]; _isEditing = NO;}- (void)textDidChange:(NSNotification *)notification{ [super textDidChange:notification]; [self invalidateIntrinsicContentSize];}-(NSSize)intrinsicContentSize{ NSSize intrinsicSize = _lastIntrinsicSize; // Only update the size if we’re editing the text, or if we’ve not set it yet // If we try and update it while another text field is selected, it may shrink back down to only the size of one line (for some reason?) if(_isEditing || !_hasLastIntrinsicSize) { intrinsicSize = [super intrinsicContentSize]; // If we’re being edited, get the shared NSTextView field editor, so we can get more info NSText *fieldEditor = [self.window fieldEditor:NO forObject:self]; if([fieldEditor isKindOfClass:[NSTextView class]]) { NSTextView *textView = (NSTextView *)fieldEditor; NSRect usedRect = [textView.textContainer.layoutManager usedRectForTextContainer:textView.textContainer]; usedRect.size.height += 5.0; // magic number! (the field editor TextView is offset within the NSTextField. It’s easy to get the space above (it’s origin), but it’s difficult to get the default spacing for the bottom, as we may be changing the height intrinsicSize.height = usedRect.size.height; } _lastIntrinsicSize = intrinsicSize; _hasLastIntrinsicSize = YES; } return intrinsicSize;}@endAs a last note, I’ve never actually used auto layout myself – the demos look amazing, but whenever I actually try it myself, I can’t get it to work quite right and it makes things more complicated. However, in this case, I think it actually did save a bunch of work – without it,
-intrinsicContentSize
wouldn’t exist, and you’d possibly have to set the frame yourself, calculating the new origin as well as the new size (not too difficult, but just more code).[solution3] height and width will change...
@implementation AutosizingTextField- (void)textDidBeginEditing:(NSNotification *)notification{ [super textDidBeginEditing:notification]; isEditing = YES;}- (void)textDidEndEditing:(NSNotification *)notification{ [super textDidEndEditing:notification]; isEditing = NO;}- (void)textDidChange:(NSNotification *)notification{ [super textDidChange:notification]; [self invalidateIntrinsicContentSize];}-(NSSize)intrinsicContentSize{ if(isEditing) { NSText *fieldEditor = [self.window fieldEditor:NO forObject:self]; if(fieldEditor) { NSTextFieldCell *cellCopy = [self.cell copy]; cellCopy.stringValue = fieldEditor.string; return [cellCopy cellSize]; } } return [self.cell cellSize];}@end
阅读全文
0 0
- NSTextField And AutoLayout: Autogrow height -> Programmatically
- NSButton And NSTextField
- Programmatically create and display Notifications
- Programmatically using ADO.NET and XML
- autogrow failed
- AutoLayout And SizeClass
- Xcode -- Autolayout and SizeClass
- UIScrollView and Autolayout
- CreationName for SSIS 2005 and adding components programmatically
- CreationName for SSIS 2008 and adding components programmatically
- screen.availWidth and screen.height
- 限制NSTextField的输入
- 设置NSTextField行间距
- 计算NSTextField的高度
- NSTextField与NSTextView
- Win32 Resources: Using C++ to Programmatically Retrieve a Global Cursor's Shape and ID
- Set Height and Width Pie Chart
- Synchronize the height of iframe and parent
- mybatis查询列表
- (1)
- 【数据库-MySql】函数
- 帆软发布大数据直连引擎FineDirect,对焦大数据BI
- C#中HttpWebRequest与HttpWebResponse的使用方法
- NSTextField And AutoLayout: Autogrow height -> Programmatically
- Scipy库
- Hellocharts与scrollview滑动冲突
- redis的认识
- 基于Netty4的HttpServer和HttpClient的简单实现
- android res与assets资源的区别
- github
- Leetcode——14. Longest Common Prefix
- Redis jedis对数据类型的操作