简体   繁体   English

如何使NSTextView随文本增长?

[英]How do I make NSTextView grow with text?

I am creating a simple word processor where it's possible for the user to add a text box to an NSView , similar to the function in Pages. 我正在创建一个简单的文字处理器,用户可以在其中向NSView添加文本框,类似于Pages中的功能。 The problem is that when it's added it will stay the same size no matter how much text the user inputs. 问题在于,无论用户输入多少文本,添加后它都将保持相同的大小。 I want it to grow as the user inputs text, I have tried with this GitHub project but when I use it the text field only expands when I have deleted all the text, as if the code doesn't react before the textDidEndEditing method. 我希望它随着用户输入文本的增长而增加,我已经尝试过该GitHub项目,但是当我使用它时,只有在删除所有文本后,文本字段才会扩展,好像代码在textDidEndEditing方法之前没有反应。 After working a bit with NSTextView I found that it would be more suitable for the task, but I still can't make it work. 在使用NSTextView进行一些操作之后,我发现它更适合该任务,但是我仍然无法使其工作。 I'm running Mavericks and Xcode 5.0.1. 我正在运行Mavericks和Xcode 5.0.1。

Hope someone can help me! 希望可以有人帮帮我!

The following uses an NSTextView subclass that must be created in code. 以下使用必须在代码中创建的NSTextView子类。 For reasons of its own Xcode won't allow you to instantiate an NSTextView in a nib without an enclosing NSScrollView instance. 由于其自身的原因,如果没有封闭的NSScrollView实例,Xcode将不允许您在笔尖中实例化NSTextView。

This class lets the only defines the text view intrinsic height - the width is left undefined which allows the view to grow with its enclosing view. 此类仅允许定义文本视图的固有高度-宽度未定义,这允许视图随其封闭视图而增长。 I used this in an NSStackView and it seemed to work well. 我在NSStackView使用了NSStackView ,它似乎运行良好。 Trying to bludgeon NSTextField so that it could wrap multiline text, edit and support Auto Layout was too messy. 试图使NSTextField笨拙,以便它可以包装多行文本,编辑并支持自动布局,这太混乱了。

Note we have support for a focus ring as I wanted my class to act like and Uber text field. 请注意,我们支持聚焦环,因为我希望我的班级表现像Uber文本字段一样。 Also note that we have no support for a border. 另请注意,我们不支持边框。 In my actual usage I create a compound view that wraps the custom text view. 在我的实际用法中,我创建了一个包含自定义文本视图的复合视图。 This wrapper view draws a border as required. 该包装视图根据需要绘制边框。

@interface BPTextViewField : NSTextView

// primitives
@property (assign, nonatomic) CGFloat borderOffsetX;
@property (assign, nonatomic) CGFloat borderOffsetY;
@end

@implementation BPTextViewField

#pragma mark -
#pragma mark Life cycle

- (instancetype)initWithFrame:(NSRect)frameRect textContainer:(nullable NSTextContainer *)container
{
    self = [super initWithFrame:frameRect textContainer:container];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (nullable instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    _borderOffsetX = 1;
    _borderOffsetY = 3;
    self.usesFontPanel = NO;
    self.usesFindPanel = NO;

}
#pragma mark -
#pragma mark Auto layout

- (NSSize)intrinsicContentSize
{
    NSTextContainer* textContainer = [self textContainer];
    NSLayoutManager* layoutManager = [self layoutManager];
    [layoutManager ensureLayoutForTextContainer: textContainer];
    NSSize size = [layoutManager usedRectForTextContainer: textContainer].size;

    return NSMakeSize(NSViewNoIntrinsicMetric, size.height);
}

#pragma mark -
#pragma mark Accessors

- (void)setString:(NSString *)string
{
    [super setString:string];
    [self invalidateIntrinsicContentSize];
}

#pragma mark -
#pragma mark Text change notifications

- (void)didChangeText
{
    [super didChangeText];
    [self invalidateIntrinsicContentSize];
}

#pragma mark -
#pragma mark Drawing

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];
}

#pragma mark -
#pragma mark Focus ring

- (void)drawFocusRingMask
{
    if (self.editable) {
        NSRectFill(self.focusRingMaskBounds);
    }
}

- (NSRect)focusRingMaskBounds {
    NSRect r = [self bounds];
    return NSMakeRect(r.origin.x - self.borderOffsetX, r.origin.y - self.borderOffsetY, r.size.width + self.borderOffsetX * 2, r.size.height + self.borderOffsetY * 2);
}


@end

This example fixed it for me: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/TextUILayer/Tasks/TextInScrollView.html 这个示例为我修复了这个问题: https : //developer.apple.com/library/mac/documentation/cocoa/conceptual/TextUILayer/Tasks/TextInScrollView.html

You have to put it into a NSScrollView. 您必须将其放入NSScrollView中。 The behavior is not the same as in UITextView (if you have a iOS background). 该行为与UITextView中的行为不同(如果您具有iOS背景)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM