美文网首页
03. Creating Attributed Strings

03. Creating Attributed Strings

作者: ngugg | 来源:发表于2018-10-29 16:52 被阅读0次

You create an NSAttributedString object in a number of different ways:
您可以通过多种不同方式创建NSAttributedString对象:

  • You can create a new string with the initWithString:, initWithString:attributes:, or initWithAttributedString: method. These methods initialize an attributed string with data you provide, as illustrated in the following example:
    • 您可以使用initWithString:,initWithString:attributes:或initWithAttributedString:方法创建新字符串。 这些方法使用您提供的数据初始化属性字符串,如以下示例所示:
NSFont *font = [NSFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary =
        [NSDictionary dictionaryWithObject:font
                                    forKey:NSFontAttributeName];
NSAttributedString *attrString =
    [[NSAttributedString alloc] initWithString:@"strigil"
            attributes:attrsDictionary];
  • For a list of attributes provided by the Application Kit framework see the Constants section in NSAttributedString Application Kit Additions Reference.

    • 有关Application Kit框架提供的属性列表,请参阅NSAttributedString Application Kit Additions Reference中的Constants部分。

    The attribute values assigned to an attributed string become the property of that string, and should not be modified “behind the attributed string” by other objects. Doing so can render inconsistent the attributed string’s internal state. Always use NSMutableAttributedString’s setAttributes:range: and related methods to change attribute values. See Changing an Attributed String for more details.

    • 分配给属性字符串的属性值将成为该字符串的属性,不应由其他对象“在属性字符串后面”进行修改。 这样做会导致属性字符串的内部状态不一致。 始终使用NSMutableAttributedString的setAttributes:range:和相关方法来更改属性值。 有关更多详细信息,请参阅更改归属字符串。
  • You can create an attributed string from rich text (RTF) or rich text with attachments (RTFD) data using the initialization methods, initWithRTF:documentAttributes:, initWithRTFD:documentAttributes:, and initWithRTFDFileWrapper:documentAttributes:, as illustrated in the following example:

    • 您可以使用初始化方法initWithRTF:documentAttributes:,initWithRTFD:documentAttributes:和initWithRTFDFileWrapper:documentAttributes:,从富文本(RTF)或带附件(RTFD)数据的富文本创建属性字符串,如以下示例所示:
NSData *rtfData = ...;  // assume rtfData is an NSData object containing valid RTF data
NSDictionary *docAttributes;
NSSize paperSize;
 
NSAttributedString *attrString;
 
if ((attrString = [[NSAttributedString alloc]
        initWithRTF: rtfData documentAttributes: &docAttributes])) {
 
    NSValue *value = [docAttrs objectForKey:@"PaperSize"];
    paperSize = [value sizeValue];
    // implementation continues...
  • You can create an attributed string from HTML data using the initialization methods initWithHTML:documentAttributes: and initWithHTML:baseURL:documentAttributes:. The methods return text attributes defined by the HTML as the attributes of the string. They return document-level attributes defined by the HTML, such as paper and margin sizes, by reference to an NSDictionary object, as described in RTF Files and Attributed Strings. The methods translate HTML as well as possible into structures of the Cocoa text system, but the Application Kit does not provide complete, true rendering of arbitrary HTML.

    • 您可以使用初始化方法initWithHTML:documentAttributes:和initWithHTML:baseURL:documentAttributes:从HTML数据创建属性字符串。 这些方法返回HTML定义的文本属性作为字符串的属性。 它们通过引用NSDictionary对象返回HTML定义的文档级属性,例如纸张和边距大小,如RTF文件和归属字符串中所述。 这些方法尽可能地将HTML转换为Cocoa文本系统的结构,但Application Kit不能提供任意HTML的完整,真实的呈现。

    Multicore considerations: Since OS X v10.4, NSAttributedString has used WebKit for all import (but not for export) of HTML documents. Because WebKit document loading is not thread safe, this has not been safe to use on background threads. For applications linked on OS X v10.5 and later, if NSAttributedString imports HTML documents on any but the main thread, the use of WebKit is transferred to the main thread via performSelectorOnMainThread:withObject:waitUntilDone:. This makes the operation thread safe, but it requires that the main thread be executing the run loop in one of the common modes. This behavior can be overridden by setting the value of the standard user default NSRunWebKitOnAppKitThread to either YES (to obtain the new behavior regardless of linkage) or NO (to obtain the old behavior regardless of linkage).

    • 多核注意事项:自OS X v10.4起,NSAttributedString已将WebKit用于HTML文档的所有导入(但不用于导出)。 因为WebKit文档加载不是线程安全的,所以在后台线程上使用它并不安全。 对于在OS X v10.5及更高版本上链接的应用程序,如果NSAttributedString在除主线程之外的任何文件上导入HTML文档,则通过performSelectorOnMainThread:withObject:waitUntilDone:将WebKit的使用传输到主线程。 这使得操作线程安全,但它要求主线程以其中一种常见模式执行运行循环。 通过将标准用户默认NSRunWebKitOnAppKitThread的值设置为YES(无论链接如何获取新行为)或NO(无论链接如何获取旧行为),都可以覆盖此行为。

相关文章

网友评论

      本文标题:03. Creating Attributed Strings

      本文链接:https://www.haomeiwen.com/subject/ffzbtqtx.html