cocoa - 如何获取 NSTextView 中光标所在位置的当前段落的范围?

标签 cocoa nstextview paragraph

现在我需要更改 nstextview 中段落的对齐方式而不选择它,所以我需要知道段落当前范围的范围?

最佳答案

我有一个子类 NSTextView所以你需要访问textStorageselectedRange不同于[self textStorage][self selectedRange] .

NSTextStorage *textStorage = [self textStorage];
NSString *string = [textStorage string];

NSUInteger editEnd = [self selectedRange].location;
NSUInteger editStart = editEnd-[textStorage editedRange].length;
NSUInteger maxLength = [string length];


while (editStart > 0) {
    unichar theChr = [string characterAtIndex:editStart-1];
    if( theChr == '\n' || theChr == '\r' ) {
        break;
    }
    --editStart;
}
while (editEnd < maxLength) {
    unichar theChr = [string characterAtIndex:editEnd];
    if( theChr == '\n' || theChr == '\r' ) {
        break;
    }
    ++editEnd;
}

NSRange paragraphRange = NSMakeRange(editStart, editEnd-editStart);

关于cocoa - 如何获取 NSTextView 中光标所在位置的当前段落的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5427906/

相关文章:

objective-c - 用于密集型科学应用的 NSDecimalNumber?

cocoa - 如何使用 NSCoding 对 unichar 数组进行编码

javascript - 在 Javascript 中构建动态链接

python - 如何使用 NLTK nltk.tokenize.texttiling 将文本拆分为段落?

macos - NSSplitView 起始大小的问题( Storyboard和 Swift)

macos - 如何以编程方式确定 OS X 上的磁盘是否已加密?

macos - NSTextView : how to subclass, 覆盖 -mouseDown 并且仍然能够选择文本?

objective-c - NSTextField 事件和辞职第一响应者

macos - 更改 NStextView 的颜色和字体

java - 使用iText java将字体设置为pdf中的段落