gpt4 book ai didi

cocoa - NSTextView - 将 initWithHTML 与表格一起使用

转载 作者:行者123 更新时间:2023-12-03 16:18:15 24 4
gpt4 key购买 nike

我正在尝试将 NSTextView 中的一些文本格式化为黑色背景的单行。一段文本需要左对齐,另一段文本(但仍在同一行)需要居中。

NSString *header = [NSString stringWithFormat:
@""
"<table style=\"width: 100%; background: black; "
"color: white; font-family: Arial; "
"font-size: 16px;\"><tr><td>"
"1. zadatak"
"</td><td align=\"center\" width=\"100%\">"
""
"%@"
""
"</td></tr></table>"
"", [self.problemname.stringValue uppercaseString]];

不幸的是,无论我指定什么宽度,NSTextView 似乎都会忽略表格宽度。

有任何解决方法、不同的方法或其他建议吗?

<小时/>

有关问题背景的更多信息。

我编写了一个用于编写编程竞赛任务的应用程序。每个任务作者都会以不同的格式提交内容,因此让他们将其标准化为一个简单的 bundle 将大有裨益。

我需要这个用于打印模块。我正在使用几个 NSTextView 并将它们缝合在一起。

这些竞赛具有我们应该遵循的以下文档格式 - Example PDF (GoogleDocs):

Contest header
+------------------------------------------------+
| 1st Task TITLE 20 points |
+------------------------------------------------+

Introductory text here.


Input

Explanation of input data


Output

Explanation of output data


Sample Data

Input: | Input:
abcd | bcde
|
Output: | Output:
efgh | fghi
|
More info: |
info text |

最佳答案

修改Apple's sample code for programmatically adding tables to NSAttributedString :

- (NSMutableAttributedString *) printTitleTableAttributedString
{
NSMutableAttributedString *tableString = [[NSMutableAttributedString alloc] initWithString:@"\n\n"];
NSTextTable *table = [[NSTextTable alloc] init];
[table setNumberOfColumns:3];

[tableString appendAttributedString:[self printTitleTableCellAttributedStringWithString:@"1. zadatak\n"
table:table
textAlignment:NSLeftTextAlignment
row:0
column:0]];

[tableString appendAttributedString:[self printTitleTableCellAttributedStringWithString:[self.problemname.stringValue stringByAppendingString:@"\n"]
table:table
textAlignment:NSCenterTextAlignment
row:0
column:1]];

[tableString appendAttributedString:[self printTitleTableCellAttributedStringWithString:@"20 bodova\n"
table:table
textAlignment:NSRightTextAlignment
row:0
column:2]];
[table release];
return [tableString autorelease];
}



- (NSMutableAttributedString *) printTitleTableCellAttributedStringWithString:(NSString *)string
table:(NSTextTable *)table
textAlignment:(NSTextAlignment)textAlignment
row:(int)row
column:(int)column
{
NSColor *backgroundColor = [NSColor colorWithCalibratedRed:0
green:0
blue:0x80/255.
alpha:0xFF];;
NSColor *borderColor = [NSColor whiteColor];


NSTextTableBlock *block = [[NSTextTableBlock alloc]
initWithTable:table
startingRow:row
rowSpan:1
startingColumn:column
columnSpan:1];
[block setBackgroundColor:backgroundColor];
[block setBorderColor:borderColor];
[block setWidth:0.0 type:NSTextBlockAbsoluteValueType forLayer:NSTextBlockBorder];
[block setWidth:2.0 type:NSTextBlockAbsoluteValueType forLayer:NSTextBlockPadding];

NSMutableParagraphStyle *paragraphStyle =
[[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setTextBlocks:[NSArray arrayWithObjects:block, nil]];
[paragraphStyle setAlignment:textAlignment];
[block release];

NSMutableAttributedString *cellString =
[[NSMutableAttributedString alloc] initWithString:string];
[cellString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, [cellString length])];
[cellString addAttribute:NSForegroundColorAttributeName
value:[NSColor whiteColor]
range:NSMakeRange(0, [cellString length])];
[paragraphStyle release];

return [cellString autorelease];
}

然后我将其附加到 TextView 中:

[printText.textStorage setAttributedString:[self printTitleTableAttributedString]];

关于cocoa - NSTextView - 将 initWithHTML 与表格一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3758062/

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