Creating Attributed Strings in Cocoa

Creating Attributed Strings in Cocoa

You create an NSAttributedString object in a number of different ways:

  • 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:

    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.

    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’ssetAttributes:range: and related methods to change attribute values. See “Changing an Attributed String” for more details.

  • 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:

    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: andinitWithHTML: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.

    Multicore considerations: Since Mac 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 Mac 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 viaperformSelectorOnMainThread: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 defaultNSRunWebKitOnAppKitThread to either YES (to obtain the new behavior regardless of linkage) or NO (to obtain the old behavior regardless of linkage).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值