Tôi đang chỉnh sửa tập con HTML trong NSTextView [1] và tôi muốn mô phỏng thẻ < giờ >.NSTextAttachmentCell là một dặm cao
Tôi đã tìm ra cách để thực hiện với NSTextAttachment và NSTextAttachmentCell tùy chỉnh, và có mã được viết để chèn phần đính kèm và ô. Vấn đề là, có một lượng lớn khoảng trống bên dưới ô.
Không gian này không phải là một phần của ô — nếu tôi vẽ toàn bộ vùng của ô đỏ, nó chính xác với kích thước phù hợp, nhưng chế độ xem văn bản đang đặt dòng văn bản tiếp theo ở phía dưới màu đỏ. Số tiền dường như phụ thuộc vào số lượng văn bản là ở trên ô; thật không may, tôi đang làm việc với các tài liệu dài, trong đó các thẻ < giờ > rất quan trọng và điều này gây ra các sự cố lớn với ứng dụng.
Chuyện quái gì đang xảy ra?
Các bộ phận tiền của lớp tế bào của tôi:
- (NSRect)cellFrameForTextContainer:(NSTextContainer *)textContainer
proposedLineFragment:(NSRect)lineFrag glyphPosition:(NSPoint)position
characterIndex:(NSUInteger)charIndex {
lineFrag.size.width = textContainer.containerSize.width;
lineFrag.size.height = topMargin + TsStyleBaseFontSize *
heightFontSizeMultiplier + bottomMargin;
return lineFrag;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
characterIndex:(NSUInteger)charIndex
layoutManager:(NSLayoutManager *)layoutManager {
NSRect frame = cellFrame;
frame.size.height -= bottomMargin;
frame.size.height -= topMargin;
frame.origin.y += topMargin;
frame.size.width *= widthPercentage;
frame.origin.x += (cellFrame.size.width - frame.size.width)/2;
[color set];
NSRectFill(frame);
}
[1] Tôi đã thử một WebView với IsEditable bộ và đánh dấu nó được sản xuất là unusably bẩn-in đặc biệt, tôi không thể tìm thấy một cách để bọc văn bản độc đáo trong các thẻ <p>.
Để trả lời yêu cầu Rob Keniger cho mã mà chèn tập tin đính kèm ngang quy tắc:
- (void)insertHorizontalRule:(id)sender {
NSAttributedString * rule = [TsPage newHorizontalRuleAttributedStringWithStylebook:self.book.stylebook];
NSUInteger loc = self.textView.rangeForUserTextChange.location;
if(loc == NSNotFound) {
NSBeep();
return;
}
if(loc > 0 && [self.textView.textStorage.string characterAtIndex:loc - 1] != '\n') {
NSMutableAttributedString * workspace = rule.mutableCopy;
[workspace.mutableString insertString:@"\n" atIndex:0];
rule = workspace;
}
if([self.textView shouldChangeTextInRange:self.textView.rangeForUserTextChange replacementString:rule.string]) {
[self.textView.textStorage beginEditing];
[self.textView.textStorage replaceCharactersInRange:self.textView.rangeForUserTextChange withAttributedString:rule];
[self.textView.textStorage endEditing];
[self.textView didChangeText];
}
[self.textView scrollRangeToVisible:self.textView.rangeForUserTextChange];
[self reloadPreview:sender];
}
Và phương pháp trong TsPage rằng cấu trúc chuỗi tập tin đính kèm:
+ (NSAttributedString *)newHorizontalRuleAttributedStringWithStylebook:(TsStylebook*)stylebook {
TsHorizontalRuleCell * cell = [[TsHorizontalRuleCell alloc] initTextCell:@"—"];
cell.widthPercentage = 0.33;
cell.heightFontSizeMultiplier = 0.25;
cell.topMargin = 12.0;
cell.bottomMargin = 12.0;
cell.color = [NSColor blackColor];
NSTextAttachment * attachment = [[NSTextAttachment alloc] initWithFileWrapper:nil];
attachment.attachmentCell = cell;
cell.attachment = attachment;
NSAttributedString * attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@""];
[str appendAttributedString:attachmentString];
[str.mutableString appendString:@"\n"];
return str;
}
Can bạn đăng mã hiển thị cách bạn chèn phần đính kèm văn bản vào 'NSTextView' của mình? –
Rob, tôi đã thêm mã bạn muốn xem. Cảm ơn bạn đã xem cái này! –