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 如何将行号与正文正确对齐 的相关文章

  • Objective-C - NSNotificationCenter 放在哪里?

    我有一个 NSNotificationCenter 选择器 把它放在哪里 在委托中 如果是 那么在哪里 在控制器中 方法也放在哪里 我需要解除分配 NSNotificationCenter 吗 NSNotificationCenter de
  • 如何在 iOS 上捕获的视频中添加水印[重复]

    这个问题在这里已经有答案了 我想知道是否有人可以告诉我如何实现这一目标 如果一直在考虑几个解决方案 从捕获的视频创建单独的图像 然后将它们合并到每个图像中 然后创建一个新的 AVAsset 听起来有点复杂 您不觉得吗 合并2个视频 一个是透
  • 在覆盖 UIView 的右下角创建四分之一透明孔

    您好 我想在覆盖 UIView 的右下角创建一个四分之一透明孔 我可以使用下面的代码解决它 但它看起来不正确 因为我在视图之外创建了一个矩形 我尝试过的 implementation PartialTransparentView id in
  • didReceiveRemoteNotification 将用户带到正确的视图

    我有一个聊天应用程序 当发送新消息时 我的服务器会发送推送通知 我遇到的问题是如何将用户带到正确的视图 我正在发送一个channelID在推送通知中 但我如何检索它并将用户带入实际对话 我使用此代码来检测何时单击推送通知 void appl
  • 在java中查找OSX的版本

    我需要测试 java 中 osx 的版本是否 Try System getProperty os name and or System getProperty os version 它返回字符串 HERE https docs oracle
  • UITableView行高不变

    我创建了一个自定义单元格 我有一系列字典 对于我需要创建的字典值UILables 每个单元可能包含不同数量的UILabels 所以按照我的习惯UITableViewCell类我就是这样做的 void generateCell BOOL is
  • iOS设备和iPhone模拟器内存​​组织的差异

    我正在尝试使用 Xcode 4 3 3 和 iPhone 5 1 模拟器开发一个应用程序 当我在模拟器上运行这个应用程序时 我没有收到任何警告 并且它运行得很好 但是 当我尝试在 iOS 设备上执行此操作时 我收到一条警告消息 收到内存警告
  • 如何使用 iconutil 手动创建 icns 文件?

    当我验证我的应用程序时 出现此错误 应用程序包不包含 ICNS 格式的图标 同时包含512x512 and a 512x512 2x image 我用来制作 icns 图标Img2icns http www img2icnsapp com
  • 使用自动布局时 UIScrollview 的中心内容

    我在项目中使用自动布局 并且有一个滚动视图 其中有一个居中的按钮 我已经让滚动视图滚动 但不占据整个屏幕 我尝试按照此处的教程进行操作 https developer apple com library ios technotes tn21
  • OS X Cocoa 自动布局隐藏元素

    我正在尝试使用新的自动布局 http developer apple com library mac documentation UserExperience Conceptual AutolayoutPG Articles Introdu
  • 如何在基于视图的应用程序中添加导航控制器

    我想向基于视图的应用程序添加导航控制器 我们如何以编程方式和使用 xib 文件来完成此操作 如果您需要在 uiviewcontroller 中合并导航控制器 您需要按如下方式初始化它 UIViewController yourViewCon
  • UIAlertAction 处理程序在延迟后运行

    我正在尝试将 UIAlertViews 更改为 UIAlertControllers 我为此设置了这个操作 UIAlertAction undoStopAction UIAlertAction actionWithTitle Undo St
  • 有 Mac 版的 IE 测试器吗? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • ln: /usr/lib/libssl.dylib: OSX 不允许操作

    我正在尝试创建一个到 usr lib 的符号链接 但我似乎没有权限 包含在 root 中 系统返回不允许该操作 在 Yosemite 上我工作得很好 但在 El Capitan 上却坏了 有人知道我能做什么吗 我在使用 Sublime te
  • 为什么我无法更改 UIBarButtonItem 的标题?

    我想改变UIBarButtonItem s title 但这段代码不起作用 void viewDidLoad self smay void smay AppDelegate apd AppDelegate UIApplication sha
  • UIView 周围的虚线边框

    如何在周围添加虚线边框UIView 像这样的东西 如果您喜欢子层 还有另一种方法 在您的自定义视图的 init 中 输入以下内容 border 是 ivar border CAShapeLayer layer border strokeCo
  • Xcode 中的 NSObject 描述和自定义摘要

    我覆盖对象的 NSString description但是 Xcode 总是显示error summary string parsing error在变量视图的摘要字段中 我当前的实现如下 NSString description retu
  • 应用程序仅启用纵向,但 UIImagePickerController 在 iOS6 中旋转

    请注意 下面的答案 不适用于 iOS6 所以我仍然需要答案 我的应用程序仅启用纵向模式 但是 如果我将 UIImagePickerController 作为子视图嵌入其中 并旋转设备 则顶部和底部栏将保持在同一位置 但 UIImagePic
  • 使用 MPMoviePlayerViewController 时的 iPad 旋转错误

    问题摘要 使用 MPMoviePlayerViewController 播放视频时更改 iPad 设备或模拟器的方向会导致视频播放器关闭时旋转状态不一致 这是 iPad SDK 3 2 中的一个已知错误 记录于http www openra
  • 共享扩展程序未出现在能够在 iPhone 上共享照片的应用程序列表中

    我正在尝试创建一个共享应用程序扩展 并按照以下来源的教程进行操作 http www technetexperts com mobile share extension in ios application overview with exa

随机推荐

  • 我的 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 中 我需要计算正确