NSPoint/NSRect 来自 NSTextView 中的字符

发布于 2024-10-19 16:37:56 字数 388 浏览 2 评论 0原文

所以我试图获取与 NSTextView 中特定字符的位置相对应的 NSPoint 或 NSRect 。这就是我到目前为止所拥有的(效果不是很好,结果似乎有点不可预测。

NSRange theTextRange = [[theTextView layoutManager] glyphRangeForCharacterRange:[theTextStorage editedRange] actualCharacterRange:NULL];
NSRect theTextRect = [[theTextView layoutManager] boundingRectForGlyphRange:theTextRange inTextContainer:[theTextView textContainer]];

So I am trying to get either an NSPoint or NSRect corresponding with the location of a specific character in an NSTextView. This is what I have so far (that isn't working very well, the results seem kind of unpredictable.

NSRange theTextRange = [[theTextView layoutManager] glyphRangeForCharacterRange:[theTextStorage editedRange] actualCharacterRange:NULL];
NSRect theTextRect = [[theTextView layoutManager] boundingRectForGlyphRange:theTextRange inTextContainer:[theTextView textContainer]];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好听的两个字的网名 2024-10-26 16:37:56

boundingRectForGlyphRange:inTextContainer: 返回的矩形位于容器坐标中。如果你想获得相对于文本视图的矩形,你需要对此进行调整:

NSRange theTextRange = [[textView layoutManager] glyphRangeForCharacterRange:textRange actualCharacterRange:NULL];
NSRect layoutRect = [[textView layoutManager] boundingRectForGlyphRange:theTextRange inTextContainer:[textView textContainer]];
NSPoint containerOrigin = [textView textContainerOrigin];
layoutRect.origin.x += containerOrigin.x;
layoutRect.origin.y += containerOrigin.y;

The rect returned by boundingRectForGlyphRange:inTextContainer: is in container coordinates. You need to adjust for this if you want to get the rect relative to the text view:

NSRange theTextRange = [[textView layoutManager] glyphRangeForCharacterRange:textRange actualCharacterRange:NULL];
NSRect layoutRect = [[textView layoutManager] boundingRectForGlyphRange:theTextRange inTextContainer:[textView textContainer]];
NSPoint containerOrigin = [textView textContainerOrigin];
layoutRect.origin.x += containerOrigin.x;
layoutRect.origin.y += containerOrigin.y;
樱娆 2024-10-26 16:37:56

您可以通过 NSPoint & 使用鼠标按下事件NS矩形

#import <Cocoa/Cocoa.h>

@interface ClickTextView : NSView {
    NSMutableArray *texts;
    NSPoint currentLocation;
    NSCell *cell;
}

@end

#import "ClickTextView.h"
@interface TextClip : NSObject
{

@public
    NSString *text;
    NSPoint location;
}

@end

@implementation TextClip @end


@implementation ClickTextView
- (void)awakeFromNib
{
    texts = [NSMutableArray new];
    cell = [[NSTextFieldCell alloc] initTextCell: @""];
    [cell setEditable: YES];
}

- (void)drawRect:(NSRect)rect 
{
    NSRect frame = rect;
    [[NSColor whiteColor] set];
    [NSBezierPath fillRect: rect];
    for (TextClip *clip in texts)
    {
        [cell setStringValue: clip->text];
        frame.origin = clip->location;
        [cell drawWithFrame: frame inView: self];
    }
}

- (void)mouseDown: (NSEvent*)theEvent
{
    currentLocation = [self convertPoint: [theEvent locationInWindow]
                                fromView: nil];
    NSText *fieldEditor = [[self window] fieldEditor: YES
                                           forObject: self];
    [cell endEditing: fieldEditor];
    [fieldEditor setString: @""];
    [cell setStringValue: @""];
    NSRect frame = {currentLocation, {400, 400}};
    [cell editWithFrame: frame
                 inView: self
                 editor: fieldEditor
               delegate: self
                  event: theEvent];
}
- (BOOL)isFlipped
{
    return YES;
}
- (void)textDidEndEditing: (NSNotification*)aNotification
{
    NSText *text = [aNotification object];
    TextClip *clip = [[TextClip alloc] init];
    clip->text = [[text string] copy];
    clip->location = currentLocation;
    [texts addObject: clip];
    [cell endEditing: text];
    [self setNeedsDisplay: YES];
}
@end

You can use mouse down event by NSPoint & NSRect

#import <Cocoa/Cocoa.h>

@interface ClickTextView : NSView {
    NSMutableArray *texts;
    NSPoint currentLocation;
    NSCell *cell;
}

@end

#import "ClickTextView.h"
@interface TextClip : NSObject
{

@public
    NSString *text;
    NSPoint location;
}

@end

@implementation TextClip @end


@implementation ClickTextView
- (void)awakeFromNib
{
    texts = [NSMutableArray new];
    cell = [[NSTextFieldCell alloc] initTextCell: @""];
    [cell setEditable: YES];
}

- (void)drawRect:(NSRect)rect 
{
    NSRect frame = rect;
    [[NSColor whiteColor] set];
    [NSBezierPath fillRect: rect];
    for (TextClip *clip in texts)
    {
        [cell setStringValue: clip->text];
        frame.origin = clip->location;
        [cell drawWithFrame: frame inView: self];
    }
}

- (void)mouseDown: (NSEvent*)theEvent
{
    currentLocation = [self convertPoint: [theEvent locationInWindow]
                                fromView: nil];
    NSText *fieldEditor = [[self window] fieldEditor: YES
                                           forObject: self];
    [cell endEditing: fieldEditor];
    [fieldEditor setString: @""];
    [cell setStringValue: @""];
    NSRect frame = {currentLocation, {400, 400}};
    [cell editWithFrame: frame
                 inView: self
                 editor: fieldEditor
               delegate: self
                  event: theEvent];
}
- (BOOL)isFlipped
{
    return YES;
}
- (void)textDidEndEditing: (NSNotification*)aNotification
{
    NSText *text = [aNotification object];
    TextClip *clip = [[TextClip alloc] init];
    clip->text = [[text string] copy];
    clip->location = currentLocation;
    [texts addObject: clip];
    [cell endEditing: text];
    [self setNeedsDisplay: YES];
}
@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文