Creating Attributed Strings in Cocoa

转载 2011年10月16日 10:51:45

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
    NSAttributedString *attrString =
        [[NSAttributedString alloc] initWithString:@"strigil"

    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).

Attributed Strings

属性字符串(Attributed Strings)可以为zi fu c
  • conpgy
  • conpgy
  • 2014年04月25日 17:15
  • 937

Using attributed strings in iOS6

Using attributed strings in iOS6 Hi this is Marin - the author of Touch Code Magazine, I hope y...
  • pjk1129
  • pjk1129
  • 2012年12月26日 22:06
  • 1773


有的时候我们可能会需要给AttributedString也就是属性字符串设置属性,然后在另外一个地方进行处理,我们也可以通过这个方式进行传值,具体操作可以如下所示 这里我们是先创建一个数组,创建完毕之...
  • 2017年11月19日 13:39
  • 201


[iOS]xib中UILabel的Attributed属性介绍 xib中的Label,可以在属性板上选择无格式(Plain)或富文本格式(Attributed)。如下: Attributed面板...
  • u012881779
  • u012881779
  • 2017年08月30日 17:03
  • 405


1.磁盘总空间大小+ (CGFloat)diskOfAllSizeMBytes { CGFloat size = 0.0; NSError *error; NSDictiona...
  • args_
  • args_
  • 2016年06月30日 10:02
  • 500


欢迎交流学习,邮箱号 本次只摘抄了其中的几页。
  • sparkexpert
  • sparkexpert
  • 2015年10月14日 10:07
  • 591

WCF 学习笔记之异常处理

1:WCF异常在配置文件 "serviceDebuBehavior"> "true" /> ...
  • dyllove98
  • dyllove98
  • 2013年07月10日 10:25
  • 818

Attributed String学习笔记

在实习中要实现下图中NavigationBar上的效果,经过考虑后认为使用NSAttributedString最为合适 根据官方文档介绍,Attributed string objects可以...
  • kuyeluofan
  • kuyeluofan
  • 2015年08月31日 14:13
  • 793

《Attributed Social Network Embedding》论文学习笔记

  • shoulunzhui6949
  • shoulunzhui6949
  • 2017年09月22日 17:53
  • 72


最近由于项目需求,特意研究了一下mac端app的相关开发,一开始就想着在网上搜搜资料,后来才发现网上资料的太少了!也曾在cocoa China 那个osx 开发那个论坛上混了好久,但是效果一直不怎么好...
  • u012890071
  • u012890071
  • 2016年03月04日 16:46
  • 4278
您举报文章:Creating Attributed Strings in Cocoa