NSRulerView 如何将行号与正文正确对齐

2023-12-31

我在 MacOS 中使用 NSRulerView 来显示 NSTextView 旁边的行号。 两个视图共享相同的字体和相同的字体大小,但是,在 NSTextView 中,字符串渲染是自动管理的,而在 NSRulerView 中,我需要计算正确的行号(并且这部分工作正常),然后在 drawHashMarksAndLabelsInRect 内渲染字符串。

我的问题是我无法在两个视图之间正确对齐文本。对于某些字体,它工作得很好,而对于其他字体,则存在明显的差异。

我实际使用的代码是:

#define BTF_RULER_WIDTH     40.0f
#define BTF_RULER_PADDING    5.0f

static inline void drawLineNumber(NSUInteger lineNumber, CGFloat y, NSDictionary *attributes, CGFloat ruleThickness) {
    NSString *string = [[NSNumber numberWithUnsignedInteger:lineNumber] stringValue];
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:string attributes:attributes];
    NSUInteger x = ruleThickness - BTF_RULER_PADDING - attString.size.width;

    [attString drawAtPoint:NSMakePoint(x, y)];
}

static inline NSUInteger countNewLines(NSString *s, NSUInteger location, NSUInteger length) {
    CFStringInlineBuffer inlineBuffer;
    CFStringInitInlineBuffer((__bridge CFStringRef)s, &inlineBuffer, CFRangeMake(location, length));

    NSUInteger counter = 0;
    for (CFIndex i=0; i < length; ++i) {
        UniChar c = CFStringGetCharacterFromInlineBuffer(&inlineBuffer, i);
        if (c == (UniChar)'\n') ++counter;
    }
    return counter;
}

@implementation BTFRulerView

- (instancetype)initWithBTFTextView:(BTFTextView *)textView {
    self = [super initWithScrollView:textView.enclosingScrollView orientation:NSVerticalRuler];
    if (self) {
        self.clientView = textView;

        // default settings
        self.ruleThickness = BTF_RULER_WIDTH;
        self.textColor = [NSColor grayColor];
    }
    return self;
}

- (void)drawHashMarksAndLabelsInRect:(NSRect)rect {
    // do not use drawBackgroundInRect for background color otherwise a 1px right border with a different color appears
    if (_backgroundColor) {
        [_backgroundColor set];
        [NSBezierPath fillRect:rect];
    }

    BTFTextView *textView = (BTFTextView *)self.clientView;
    if (!textView) return;

    NSLayoutManager *layoutManager = textView.layoutManager;
    if (!layoutManager) return;

    NSString *textString = textView.string;
    if ((!textString) || (textString.length == 0)) return;

    CGFloat insetHeight = textView.textContainerInset.height;
    CGPoint relativePoint = [self convertPoint:NSZeroPoint fromView:textView];
    NSDictionary *lineNumberAttributes = @{NSFontAttributeName: textView.font, NSForegroundColorAttributeName: _textColor};

    NSRange visibleGlyphRange = [layoutManager glyphRangeForBoundingRect:textView.visibleRect inTextContainer:textView.textContainer];
    NSUInteger firstVisibleGlyphCharacterIndex = [layoutManager characterIndexForGlyphAtIndex:visibleGlyphRange.location];

    // line number for the first visible line
    NSUInteger lineNumber = countNewLines(textString, 0, firstVisibleGlyphCharacterIndex)+1;
    NSUInteger glyphIndexForStringLine = visibleGlyphRange.location;

    // go through each line in the string
    while (glyphIndexForStringLine < NSMaxRange(visibleGlyphRange)) {
        // range of current line in the string
        NSRange characterRangeForStringLine = [textString lineRangeForRange:NSMakeRange([layoutManager characterIndexForGlyphAtIndex:glyphIndexForStringLine], 0)];
        NSRange glyphRangeForStringLine = [layoutManager glyphRangeForCharacterRange: characterRangeForStringLine actualCharacterRange:nil];

        NSUInteger glyphIndexForGlyphLine = glyphIndexForStringLine;
        NSUInteger glyphLineCount = 0;

        while (glyphIndexForGlyphLine < NSMaxRange(glyphRangeForStringLine)) {
            // check if the current line in the string spread across several lines of glyphs
            NSRange effectiveRange = NSMakeRange(0, 0);

            // range of current "line of glyphs". If a line is wrapped then it will have more than one "line of glyphs"
            NSRect lineRect = [layoutManager lineFragmentRectForGlyphAtIndex:glyphIndexForGlyphLine effectiveRange:&effectiveRange withoutAdditionalLayout:YES];

            // compute Y for line number
            CGFloat y = NSMinY(lineRect) + relativePoint.y + insetHeight;

            // draw line number only if string does not spread across several lines
            if (glyphLineCount == 0) {
                drawLineNumber(lineNumber, y, lineNumberAttributes, self.ruleThickness);
            }

            // move to next glyph line
            ++glyphLineCount;
            glyphIndexForGlyphLine = NSMaxRange(effectiveRange);
        }

        glyphIndexForStringLine = NSMaxRange(glyphRangeForStringLine);
        ++lineNumber;
    }

    // draw line number for the extra line at the end of the text
    if (layoutManager.extraLineFragmentTextContainer) {
        CGFloat y = NSMinY(layoutManager.extraLineFragmentRect) + relativePoint.y + insetHeight;
        drawLineNumber(lineNumber, y, lineNumberAttributes, self.ruleThickness);
    }
}

我认为问题在于 y 计算然后传递给 drawLineNumber 函数。关于如何正确计算它有什么想法吗?


我找到了一个解决方案,我认为它对其他人非常有用:

#define BTF_RULER_WIDTH     40.0f
#define BTF_RULER_PADDING    5.0f

static inline void drawLineNumberInRect(NSUInteger lineNumber, NSRect lineRect, NSDictionary *attributes, CGFloat ruleThickness) {
    NSString *string = [[NSNumber numberWithUnsignedInteger:lineNumber] stringValue];
    NSAttributedString *attString = [[NSAttributedString alloc] initWithString:string attributes:attributes];
    NSUInteger x = ruleThickness - BTF_RULER_PADDING - attString.size.width;

    // Offetting the drawing keeping into account the ascender (because we draw it without NSStringDrawingUsesLineFragmentOrigin)
    NSFont *font = attributes[NSFontAttributeName];
    lineRect.origin.x = x;
    lineRect.origin.y += font.ascender;

    [attString drawWithRect:lineRect options:0 context:nil];
}

static inline NSUInteger countNewLines(NSString *s, NSUInteger location, NSUInteger length) {
    CFStringInlineBuffer inlineBuffer;
    CFStringInitInlineBuffer((__bridge CFStringRef)s, &inlineBuffer, CFRangeMake(location, length));

    NSUInteger counter = 0;
    for (CFIndex i=0; i < length; ++i) {
        UniChar c = CFStringGetCharacterFromInlineBuffer(&inlineBuffer, i);
        if (c == (UniChar)'\n') ++counter;
    }
    return counter;
}

@implementation BTFRulerView

- (instancetype)initWithBTFTextView:(BTFTextView *)textView {
    self = [super initWithScrollView:textView.enclosingScrollView orientation:NSVerticalRuler];
    if (self) {
        self.clientView = textView;

        // default settings
        self.ruleThickness = BTF_RULER_WIDTH;
        self.textColor = [NSColor grayColor];
    }
    return self;
}

- (void)drawHashMarksAndLabelsInRect:(NSRect)rect {
    // do not use drawBackgroundInRect for background color otherwise a 1px right border with a different color appears
    if (_backgroundColor) {
        [_backgroundColor set];
        [NSBezierPath fillRect:rect];
    }

    BTFTextView *textView = (BTFTextView *)self.clientView;
    if (!textView) return;

    NSLayoutManager *layoutManager = textView.layoutManager;
    if (!layoutManager) return;

    NSString *textString = textView.string;
    if ((!textString) || (textString.length == 0)) return;

    CGFloat insetHeight = textView.textContainerInset.height;
    CGPoint relativePoint = [self convertPoint:NSZeroPoint fromView:textView];

    // Gettign text attributes from the textview
    NSMutableDictionary *lineNumberAttributes = [[textView.textStorage attributesAtIndex:0 effectiveRange:NULL] mutableCopy];
    lineNumberAttributes[NSForegroundColorAttributeName] = self.textColor;

    NSRange visibleGlyphRange = [layoutManager glyphRangeForBoundingRect:textView.visibleRect inTextContainer:textView.textContainer];
    NSUInteger firstVisibleGlyphCharacterIndex = [layoutManager characterIndexForGlyphAtIndex:visibleGlyphRange.location];

    // line number for the first visible line
    NSUInteger lineNumber = countNewLines(textString, 0, firstVisibleGlyphCharacterIndex)+1;
    NSUInteger glyphIndexForStringLine = visibleGlyphRange.location;

    // go through each line in the string
    while (glyphIndexForStringLine < NSMaxRange(visibleGlyphRange)) {
        // range of current line in the string
        NSRange characterRangeForStringLine = [textString lineRangeForRange:NSMakeRange([layoutManager characterIndexForGlyphAtIndex:glyphIndexForStringLine], 0)];
        NSRange glyphRangeForStringLine = [layoutManager glyphRangeForCharacterRange: characterRangeForStringLine actualCharacterRange:nil];

        NSUInteger glyphIndexForGlyphLine = glyphIndexForStringLine;
        NSUInteger glyphLineCount = 0;

        while (glyphIndexForGlyphLine < NSMaxRange(glyphRangeForStringLine)) {
            // check if the current line in the string spread across several lines of glyphs
            NSRange effectiveRange = NSMakeRange(0, 0);

            // range of current "line of glyphs". If a line is wrapped then it will have more than one "line of glyphs"
            NSRect lineRect = [layoutManager lineFragmentRectForGlyphAtIndex:glyphIndexForGlyphLine effectiveRange:&effectiveRange withoutAdditionalLayout:YES];

            // compute Y for line number
            CGFloat y = ceil(NSMinY(lineRect) + relativePoint.y + insetHeight);
            lineRect.origin.y = y;

            // draw line number only if string does not spread across several lines
            if (glyphLineCount == 0) {
                drawLineNumberInRect(lineNumber, lineRect, lineNumberAttributes, self.ruleThickness);
            }

            // move to next glyph line
            ++glyphLineCount;
            glyphIndexForGlyphLine = NSMaxRange(effectiveRange);
        }

        glyphIndexForStringLine = NSMaxRange(glyphRangeForStringLine);
        ++lineNumber;
    }

    // draw line number for the extra line at the end of the text
    if (layoutManager.extraLineFragmentTextContainer) {
        NSRect lineRect = layoutManager.extraLineFragmentRect;
        CGFloat y = ceil(NSMinY(lineRect) + relativePoint.y + insetHeight);
        lineRect.origin.y = y;
        drawLineNumberInRect(lineNumber, lineRect, lineNumberAttributes, self.ruleThickness);
    }
}

我使用drawWithRect而不是drawAtPoint,并且直接使用连接的textView中的属性。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NSRulerView 如何将行号与正文正确对齐 的相关文章

  • 方向改变时重新定位控件

    我知道自动布局可用于在方向改变时使尺寸和位置保持一致 当方向改变时 是否可以完全改变布局 例如 请查看下面的纵向模式下简单登录屏幕的线框 现在 如果我旋转设备 我想完全重新定位控件 这种事情可以用自动布局来完成吗 如果没有 我该怎么办 谢谢
  • 如何在我的应用程序中添加应用内购买

    我想在我的应用程序中使用应用程序购买应用程序 但我不知道如何执行此操作 任何人都可以帮助我如何做以及源代码在哪里 给我链接或代码 这里有一堆链接 应用内购买编程指南 http developer apple com iphone libra
  • 如何在 UITableView 中显示零行的表格

    我正在动态地将内容加载到 UITableView 中 如果有数据 表格需要显示数据 如果没有数据 表格应显示普通页面 但在我的应用程序中 表格显示带有两条分隔线的普通页面 我需要删除此分隔线并显示纯白色页面 请建议 任何帮助 将不胜感激 如
  • 为什么在尝试编译此代码时会收到错误“错误:未知类型名称'虚拟'”?

    Code struct IRenderingEngine virtual void Initialize int width int height 0 virtual void Render const 0 virtual void Upd
  • 处理过时的 NSURL 书签的正确方法是什么?

    从安全范围的书签解析 NSURL 时 如果用户已重命名或移动该文件或文件夹 则该书签将过时 苹果的文档对于陈旧性有这样的描述 isStale 返回时 如果是 则书签数据已过时 你的应用程序应该 使用返回的 URL 创建一个新书签并用它代替
  • UILocalNotification 在后台 10 分钟后不提示

    In didFinishLaunchingWithOptions调用函数的定时器循环httpRequest每 1 分钟间隔一次 BOOL application UIApplication application didFinishLaun
  • 是否可以扩展现有的 Objective-C 块?

    我有一个使用标头中定义的块的类 如下所示 property readwrite copy RequestSucceededBlock succeededBlock 该物业succeededBlock已经设置了一个块 有没有办法用另一个仍然调
  • NSPoint/NSRect 来自 NSTextView 中的字符

    所以我试图获取与 NSTextView 中特定字符的位置相对应的 NSPoint 或 NSRect 这就是我到目前为止所拥有的 效果不是很好 结果似乎有点不可预测 NSRange theTextRange theTextView layou
  • 安装 python 3 的包

    我倾向于在 Jupyter 笔记本中运行我的代码 这些代码在 python 3 中运行 我的计算机上也有 python 2 我安装了pip3所以我可以专门为 python 3 安装软件包 但这似乎不适合我 mba pip3 install
  • NSUInteger 的奇怪行为 - 无法正确转换为浮动

    这是我的情况 这让我发疯 我有一个计数值为 517 的 NSMutableArray 我有一个双精度值 它是我的乘数 double multiplier 0 1223 double result myArray count multipli
  • 生成具有固定数字长度的随机数?

    我正在生成随机数 int randomID arc4random 3000 但我想生成至少 4 位数字的随机数 如 1000 2400 1122 我想知道 Objective C 的代码 请尝试 生成数字 1000 9999 int ran
  • (Kiss)XML xpath 和默认命名空间

    我正在开发一个 iPhone 项目 需要解析一些 xml xml 可能包含也可能不包含默认名称空间 我需要知道如何解析 xml 以防它使用默认命名空间 由于我需要读取和写入 xml 因此我倾向于使用 KissXML 但我愿意接受建议 这是我
  • 如何使用 iPhone 将照片上传到服务器?

    我正在编写一个 iPhone 应用程序 它可以拍摄照片然后将其上传到服务器 如何使用 Cocoa 将照片上传到服务器 我想我在某处使用 NSUrl Thanks Header interface EPUploader NSObject NS
  • NSTextField 字体样式在选择时重置

    Context 在 Interface Builder 中 我有一个不可编辑的标签 NSTextField 标签的内容是使用 Cocoa Bindings 创建的 绑定的值是NSAttributedString 使用数值转换器创建 看图片
  • watchOS 错误:控制器接口描述中的未知属性

    我将 WKInterfacePicker 添加到情节提要中 并将其连接到界面控制器中的 IBOutlet 运行应用程序时 它在控制台中显示一条错误消息 控制器的接口描述 watchPicker 中的未知属性 Code interface I
  • UIPickerView selectRow 未按预期工作

    我创建了一个UIPickerView它有两个组件 第一个组件 A 的行数固定为 13 另一个组件 B 的行数可变 具体取决于 A 中选择的行 加载时UIPickerView我调用以下命令 以便我可以在两个组件中默认选择 但是我遇到的问题是只
  • UITableViewCell显示多种字体

    我想在 uitableviewcell 中以类似于 iPhone 地址簿的不同字体显示两个单词 例如 约翰Buchanan 您应该使用两个 UILable 或者您可以使用OH属性标签 https github com AliSoftware
  • TableViewController 的 viewDidLoad 未触发

    我一直在关注这个tutorial http www appcoda com ios programming sidebar navigation menu 有一个滑出式菜单 我添加了一个 TableViewController 它将显示文章
  • 在实例化对象之前是否可以检查故事板中是否存在标识符?

    在我的代码中我有这一行 但我想知道是否有办法检查是否 一些控制器 在我将它与 一起使用之前就存在实例化ViewControllerWithIdentifier 方法 如果标识符不存在 则应用程序崩溃 如果没有好的方法 这并不是一个大问题 我
  • 像 TraceGL 一样分析 Objective C 中的代码路径?

    TraceGL 是一个非常简洁的项目 它允许 JS 程序员跟踪 Javascript 中的代码路径 它看起来像这样 我想为 Objective C 构建类似的东西 我知道运行时使跟踪方法调用变得相当容易 但是我如何跟踪控制流 例如 在上面的

随机推荐

  • 我的 VBA Excel 宏中的防病毒误报

    我刚刚遇到了一个更烦人的问题 https stackoverflow com questions 3339136 antivirus false positive in my executable 突然 Windows Defender 开
  • Netbeans7.1 和 JavaFX 2.0 - FXML 代码完成不起作用

    我开始学习 JavaFX 2 0 并安装了 Netbeans 7 1 java 7 02 SDK 其中包含 JavaFX 2 一切似乎都正常 示例项目编译并运行良好 我的问题是 代码完成不适用于 FXML 文件 我按 ctrl space
  • Matlab 快速傅立叶变换 / fft 用于时间和速度

    我有一个 2 列向量 其中包含数据子集的时间和速度 如下所示 5 40 10 37 15 34 20 39 等等 我想要对速度进行傅立叶变换以获得频率 我将如何使用快速傅里叶变换 fft 来做到这一点 如果我的矢量名称是sampleData
  • Python - 处理混合编码文件

    我有一个文件 大部分是 UTF 8 但也有一些 Windows 1252 字符 我创建了一个表来将 Windows 1252 cp1252 字符映射到其 Unicode 对应字符 并希望使用它来修复错误编码的字符 例如 cp1252 to
  • 通过使其成为包装器来优化斐波那契数列递归函数

    斐波那契数列的递归定义在效率方面存在问题 它的定义如下 private fib int n if n lt 2 return n else return fib n 1 fib n 2 假设我们调用 fib 5 这使得 1 次调用 fib
  • 如何在 Amazon OpsWorks 上设置 Chef 的日志输出级别?

    我的问题类似于 如何在控制台中显示 Opscode Chef bash 命令的输出 https stackoverflow com questions 17813592 how can i display the output of a o
  • 在android中动态地将字体添加到textview中

    我是安卓新手 我有一个文本视图 想为其分配自定义字体 我的字体文件 ttf 位于服务器上 我必须在代码中使用该文件来动态设置字体 即时 我不想将文件放在资产文件夹或任何原始文件夹中 如何实施 从服务器下载字体 保存到SD卡 Use Type
  • 修复翻译错误

    liferay 门户中有很多地方翻译成我的语言 sk SK 是错误的 是否可以用 hook 重写那些不好的翻译 任何其他想法都欢迎 多谢 是的 你可以做到 in your liferay hook xml文件添加要覆盖的语言文件的条目 就像
  • 嵌套在结构中的 LINQ 和分组依据数据

    我的结构大致如下 List
  • android.view.WindowManager$BadTokenException:无法在 Toast 处添加窗口

    当我在我的 Android 应用程序上频繁执行某些操作 我的假设是由于 Toast 消息 时 出现以下错误 我没有得到此问题的确切位置 我可以从某人那里获得帮助来解决相同问题吗 beginning of crash 10 04 16 13
  • C# 通用约束问题

    我收到以下错误 类型 Test ICacheProvider 不能用作类型参数 泛型类型或方法中的 TStorageProvider StorageManager Test IFileInfo 没有 隐式引用转换自 StorageManag
  • 将 blob 转换为图像流并将其分配给 jLabel

    我只是想将数据库中的 blob 字符串转换为字节数组 然后在转换后将其转换为缓冲图像 然后将其分配给标签 这是我的代码 package ims project import java sql import javax swing impor
  • jQuery 仅获取此元素的父同级元素

    我不知道如何写这个 请参阅我的标记结构 该结构在页面上重复多次 div class module div class archive info span class archive meta open span div div class
  • Google 地图 fitBounds 无法正常工作

    我对 googlemaps fitBounds 函数有疑问 for var i 0 i lt countries length i var country countries i var latlng new google maps Lat
  • JavaScript 中去除字符串中的所有非数字字符

    考虑一个非 DOM 场景 您希望使用 JavaScript ECMAScript 从字符串中删除所有非数字字符 范围内的任何字符0 9应该保留 var myString abc123 8
  • 如何高效解析固定宽度文件?

    我正在尝试找到一种有效的方法来解析包含固定宽度行的文件 例如 前 20 个字符代表一列 从 21 30 开始代表另一列 依此类推 假设该行包含 100 个字符 将一行解析为多个组成部分的有效方法是什么 我可以对每行使用字符串切片 但如果行很
  • 具有多个条件的布尔索引[重复]

    这个问题在这里已经有答案了 我有一个熊猫DF我需要去哪里filter输出一些包含特征 a 和特征 b 的值 0 的行 为了检查这些值 我运行以下命令 DF1 DF DF a 0 它返回正确的值 同样 通过这样做 DF2 DF DF b 0
  • 来自 Pyspark ArrayType 列的随机样本

    我在 Pyspark 数据框中有一列 其结构如下 Column1 a b c d e c b d f g h i p l m 我想返回另一列 其中随机选择每行中的每个数组 以及函数中指定的数量 所以像data withColumn samp
  • 在套接字上多次调用listen——预期的行为?

    我在使用简单的基于 C 的服务器时注意到一些奇怪的事情 我的 Linux 4 10 3 系统上的程序 我不小心打通了电话listen 我在套接字上两次 来自服务器进程 被称为bind 早些时候 我注意到两个监听电话 成功 没有任何错误 事实
  • NSRulerView 如何将行号与正文正确对齐

    我在 MacOS 中使用 NSRulerView 来显示 NSTextView 旁边的行号 两个视图共享相同的字体和相同的字体大小 但是 在 NSTextView 中 字符串渲染是自动管理的 而在 NSRulerView 中 我需要计算正确