gpt4 book ai didi

ios - NSTextStorage 语法 Markdown

转载 作者:行者123 更新时间:2023-11-29 12:04:33 24 4
gpt4 key购买 nike

我正在为 iOS/osx 编写语法 Markdown 。

它是 NSTextStorage 的子类。它在 iOS 中运行良好,但在 OSX 中(将代码 UIColor 转换为 NSColor 并将 UIFont 转换为 NSFont 后)它确实运行良好。如果我在当前行的末尾书写,效果很好,但如果我将插入符号位置更改为文本中间,在键入 1 个字母后,它会将插入符号位置更改为行尾。这只发生在 OSX 中,因为在 IOS 中它运行良好。

我知道问题出在 - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range 但不知道如何解决...有什么帮助吗?

- (NSString *)string {
return [_backingStore string];
}

- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range {
return [_backingStore attributesAtIndex:location effectiveRange:range];
}

- (void)replaceCharactersInRange:(NSRange)range withString:(NSString*)str {

[self beginEditing];
[_backingStore replaceCharactersInRange:range withString:str];
[self edited:NSTextStorageEditedCharacters | NSTextStorageEditedAttributes range:range changeInLength:str.length - range.length];
[self endEditing];

}

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range {

[self beginEditing];
[_backingStore setAttributes:attrs range:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
[self endEditing];

}

- (void)processEditing {

[self performReplacementsForRange:[self editedRange]];
[super processEditing];
}

- (void)performReplacementsForRange:(NSRange)changedRange {

NSString* backingString = [_backingStore string];
NSRange extendedRange = extendedRange = NSUnionRange(changedRange, [backingString lineRangeForRange:NSMakeRange(NSMaxRange(changedRange), 0)]);
[self applyStylesToRange:extendedRange];
}

- (void)applyStylesToRange:(NSRange)searchRange {

NSDictionary* attributeDictionary = self.attributeDictionary;
NSString* backingString = [_backingStore string];
NSDictionary* bodyAttributes = self.bodyAttributes;
[self setAttributes:bodyAttributes range:searchRange];
[attributeDictionary enumerateKeysAndObjectsUsingBlock:^(NSRegularExpression* regex, NSDictionary* attributes, BOOL* stop) {
[regex enumerateMatchesInString:backingString options:0 range:searchRange
usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
NSRange matchRange = [match rangeAtIndex:1];
[self addAttributes:attributes range:matchRange];
}];
}];

}

最佳答案

我不熟悉 NSTextStorage,但我设法通过覆盖 fixAttributesInRange: 而不是 processEditing 来解决这个问题。

代替

- (void)processEditing {

[self performReplacementsForRange:[self editedRange]];
[super processEditing];
}

- (void)fixAttributesInRange:(NSRange)range {
[self performReplacementsForRange:range];
[super fixAttributesInRange:(NSRange)range];
}

顺便说一句:applyStylesToRange 不应该嵌套在 beginEditingendEditing 之间吗?

关于ios - NSTextStorage 语法 Markdown ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com