欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发富文本NSAttributedString属性

程序员文章站 2022-07-23 22:29:15
// // nsattributedstring.h // uikit // // copyright (c) 2011-2015 apple inc. all r...
//  
//  nsattributedstring.h  
//  uikit  
//  
//  copyright (c) 2011-2015 apple inc. all rights reserved.  
//  
  
#import   
#import   
 
/************************ attributes ************************/  
  
/* predefined character attributes for text. if the key is not in the dictionary, then use the default values as described below. 
 * 预定义的text的字符属性,如果这个key没有在设置的dictionary中,将会使用下面介绍的。 
 */  
// 字体  
uikit_extern nsstring *const nsfontattributename ns_available_ios(6_0);                // uifont, default helvetica(neue) 12  
// 段落样式  
uikit_extern nsstring *const nsparagraphstyleattributename ns_available_ios(6_0);      // nsparagraphstyle, default defaultparagraphstyle  
// 文字颜色  
uikit_extern nsstring *const nsforegroundcolorattributename ns_available_ios(6_0);     // uicolor, default blackcolor  
// 文字背景颜色  
uikit_extern nsstring *const nsbackgroundcolorattributename ns_available_ios(6_0);     // uicolor, default nil: no background  
// 设置连体属性,取值为nsnumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符  
uikit_extern nsstring *const nsligatureattributename ns_available_ios(6_0);            // nsnumber containing integer, default 1: default ligatures, 0: no ligatures  
// 设定字符间距  
uikit_extern nsstring *const nskernattributename ns_available_ios(6_0);                // nsnumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.  
// 设置删除线  
uikit_extern nsstring *const nsstrikethroughstyleattributename ns_available_ios(6_0);  // nsnumber containing integer, default 0: no strikethrough  
// 下划线  
uikit_extern nsstring *const nsunderlinestyleattributename ns_available_ios(6_0);      // nsnumber containing integer, default 0: no underline  
// 填充部分颜色 same as foreground color  
uikit_extern nsstring *const nsstrokecolorattributename ns_available_ios(6_0);         // uicolor, default nil: same as foreground color  
// 设置笔画宽度  
uikit_extern nsstring *const nsstrokewidthattributename ns_available_ios(6_0);         // nsnumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)  
// 阴影属性  
uikit_extern nsstring *const nsshadowattributename ns_available_ios(6_0);              // nsshadow, default nil: no shadow  
// 文本特殊效果  
uikit_extern nsstring *const nstexteffectattributename ns_available_ios(7_0);          // nsstring, default nil: no text effect  
  
// 文本附件,取值为nstextattachment对象,常用于文字图片混排  
uikit_extern nsstring *const nsattachmentattributename ns_available_ios(7_0);          // nstextattachment, default nil  
// 设置链接属性,点击后调用浏览器打开指定url地址  
uikit_extern nsstring *const nslinkattributename ns_available_ios(7_0);                // nsurl (preferred) or nsstring  
uikit_extern nsstring *const nsbaselineoffsetattributename ns_available_ios(7_0);      // nsnumber containing floating point value, in points; offset from baseline, default 0  
// 设置基线偏移值,取值为 nsnumber (float),正值上偏,负值下偏  
uikit_extern nsstring *const nsunderlinecolorattributename ns_available_ios(7_0);      // uicolor, default nil: same as foreground color  
// 设置删除线颜色,取值为 uicolor 对象,默认值为黑色  
uikit_extern nsstring *const nsstrikethroughcolorattributename ns_available_ios(7_0);  // uicolor, default nil: same as foreground color  
// 设置字形倾斜度,取值为 nsnumber (float),正值右倾,负值左倾  
uikit_extern nsstring *const nsobliquenessattributename ns_available_ios(7_0);         // nsnumber containing floating point value; skew to be applied to glyphs, default 0: no skew  
// 设置文本横向拉伸属性,取值为 nsnumber (float),正值横向拉伸文本,负值横向压缩文本  
uikit_extern nsstring *const nsexpansionattributename ns_available_ios(7_0);           // nsnumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion  
  
// 设置文字书写方向,从左向右书写或者从右向左书写  
uikit_extern nsstring *const nswritingdirectionattributename ns_available_ios(7_0);    // nsarray of nsnumbers representing the nested levels of writing direction overrides as defined by unicode lre, rle, lro, and rlo characters.  the control characters can be obtained by masking nswritingdirection and nstextwritingdirection values.  lre: nswritingdirectionlefttoright|nstextwritingdirectionembedding, rle: nswritingdirectionrighttoleft|nstextwritingdirectionembedding, lro: nswritingdirectionlefttoright|nstextwritingdirectionoverride, rlo: nswritingdirectionrighttoleft|nstextwritingdirectionoverride,  
  
// 设置文字排版方向,取值为 nsnumber 对象(整数),0 表示横排文本,1 表示竖排文本  
uikit_extern nsstring *const nsverticalglyphformattributename ns_available_ios(6_0);   // an nsnumber containing an integer value.  0 means horizontal text.  1 indicates vertical text.  if not specified, it could follow higher-level vertical orientation settings.  currently on ios, it's always horizontal.  the behavior for any other value is undefined.  
  
/* this defines currently supported values for nsunderlinestyleattributename and nsstrikethroughstyleattributename. 
 * 下面定义了下划线、删除线支持的样式 
 */  
typedef ns_enum(nsinteger, nsunderlinestyle) {  
    nsunderlinestylenone                                = 0x00,   // 不设置下划线  
    nsunderlinestylesingle                              = 0x01,   // 设置删除线为细单实线  
    nsunderlinestylethick ns_enum_available_ios(7_0)    = 0x02,   // 设置删除线为粗单实线  
    nsunderlinestyledouble ns_enum_available_ios(7_0)   = 0x09,   // 设置删除线为细双实线  
  
    // https://blog.csdn.net/harvic880925/article/details/9028823 参考  
    nsunderlinepatternsolid ns_enum_available_ios(7_0)      = 0x0000,  // 实线  
    nsunderlinepatterndot ns_enum_available_ios(7_0)        = 0x0100,  // 点线  
    nsunderlinepatterndash ns_enum_available_ios(7_0)       = 0x0200,  // 虚线  
    nsunderlinepatterndashdot ns_enum_available_ios(7_0)    = 0x0300,  
    nsunderlinepatterndashdotdot ns_enum_available_ios(7_0) = 0x0400,  
  
    nsunderlinebyword ns_enum_available_ios(7_0) = 0x8000  
} ns_enum_available_ios(6_0);  
  
/* nstextwritingdirection values used by nswritingdirectionattributename. can specify the formatting controls defined by unicode bidirectional algorithm. 
 */  
typedef ns_enum(nsinteger, nstextwritingdirection) {  
    nstextwritingdirectionembedding     = (0 << 1),  
    nstextwritingdirectionoverride      = (1 << 1)  
} ns_enum_available_ios(7_0);  
  
/* this defines the currently supported value for nstexteffectattributename as of ios 7.0 
 */  
uikit_extern nsstring *const nstexteffectletterpressstyle ns_available_ios(7_0);  
  
/************************ attribute fixing ************************/  
  
@interface nsmutableattributedstring (nsmutableattributedstringkitadditions)  
// this method fixes attribute inconsistencies inside range.  it ensures nsfontattributename covers the characters, nsparagraphstyleattributename is only changing at paragraph boundaries, and nstextattachmentattributename is assigned to nstextattachmentcharacter.  nstextstorage automatically invokes this method via -ensureattributesarefixedinrange:.  
- (void)fixattributesinrange:(nsrange)range ns_available_ios(7_0);  
  
@end  
  
  
/************************ document formats ************************/  
  
// supported document types for the nsdocumenttypedocumentattribute key in the document attributes dictionary.  
uikit_extern nsstring *const nsplaintextdocumenttype ns_available_ios(7_0); // 普通文本  
uikit_extern nsstring *const nsrtftextdocumenttype ns_available_ios(7_0);  
uikit_extern nsstring *const nsrtfdtextdocumenttype ns_available_ios(7_0);  
uikit_extern nsstring *const nshtmltextdocumenttype ns_available_ios(7_0);  // html  
  
  
// keys for nslayoutorientationsectionsattribute.  
// 文字展示的方向  
uikit_extern nsstring *const nstextlayoutsectionorientation ns_available_ios(7_0); // nsnumber containing nstextlayoutorientation value. default: nstextlayoutorientationhorizontal  
// 字符的范围  
uikit_extern nsstring *const nstextlayoutsectionrange ns_available_ios(7_0); // nsvalue containing nsrange representing a character range. default: a range covering the whole document  
  
  
// keys for options and document attributes dictionaries.  they are in and out document properties used by both read/write methods.  
  
uikit_extern nsstring *const nsdocumenttypedocumentattribute ns_available_ios(7_0);  // @"documenttype", one of the document types declared above.  for reader methods, this key in options can specify the document type for interpreting the contents.  upon return, the document attributes can contain this key for indicating the actual format used to read the contents.  for write methods, this key specifies the format for generating the data.  
  
  
// nsplaintextdocumenttype document attributes  
// 普通文本的编码  
uikit_extern nsstring *const nscharacterencodingdocumentattribute ns_available_ios(7_0);  // @"characterencoding", nsnumber containing integer specifying nsstringencoding for the file; default for plain text is the default encoding.  this key in options can specify the string encoding for reading the data.  upon return, the document attributes can contain the actual encoding used.  for writing methods, this value is used for generating the plain text data.  
uikit_extern nsstring *const nsdefaultattributesdocumentattribute ns_available_ios(7_0);  // @"defaultattributes", nsdictionary containing attributes to be applied to plain files.  used by reader methods.  this key in options can specify the default attributes applied to the entire document contents.  the document attributes can contain this key indicating the actual attributes used.  
  
  
// nsrtftextdocumenttype and nsrtfdtextdocumenttype document attributes  
// document dimension  
// they are document attributes used by read/write methods.  
uikit_extern nsstring *const nspapersizedocumentattribute ns_available_ios(7_0);  // @"papersize", nsvalue containing cgsize (in points)  
uikit_extern nsstring *const nspapermargindocumentattribute ns_available_ios(7_0);  // @"papermargin", nsvalue containing uiedgeinsets  
  
uikit_extern nsstring *const nsviewsizedocumentattribute ns_available_ios(7_0);  // @"viewsize", nsvalue containing cgsize (in points)  
uikit_extern nsstring *const nsviewzoomdocumentattribute ns_available_ios(7_0);  // @"viewzoom", nsnumber containing floating point value (100 == 100% zoom)  
uikit_extern nsstring *const nsviewmodedocumentattribute ns_available_ios(7_0);  // @"viewmode", nsnumber containing integer; 0 = normal; 1 = page layout  
  
// document settings  
// they are document attributes used by read/write methods.  
uikit_extern nsstring *const nsreadonlydocumentattribute ns_available_ios(7_0);  // @"readonly", nsnumber containing integer; if missing, or 0 or negative, not readonly; 1 or more, readonly. note that this has nothing to do with the file system protection on the file, but instead, on how the file should be displayed to the user  
uikit_extern nsstring *const nsbackgroundcolordocumentattribute ns_available_ios(7_0);  // @"backgroundcolor", uicolor, representing the document-wide page background color  
uikit_extern nsstring *const nshyphenationfactordocumentattribute ns_available_ios(7_0);  // @"hyphenationfactor", nsnumber containing floating point value (0=off, 1=full hyphenation)  
uikit_extern nsstring *const nsdefaulttabintervaldocumentattribute ns_available_ios(7_0);  // @"defaulttabinterval", nsnumber containing floating point value, representing the document-wide default tab stop interval, in points  
uikit_extern nsstring *const nstextlayoutsectionsattribute ns_available_ios(7_0);  // nsarray of dictionaries.  each dictionary describing a layout orientation section.  the dictionary can have two attributes: nstextlayoutsectionorientation and nstextlayoutsectionrange.  when there is a gap between sections, it's assumed to have nstextlayoutorientationhorizontal.  
  
  
@interface nsattributedstring (nsattributedstringdocumentformats)  
// methods initializing the receiver contents with an external document data.  options specify document attributes for interpreting the document contents.  nsdocumenttypedocumentattribute, nscharacterencodingdocumentattribute, and nsdefaultattributesdocumentattribute are supported options key.  when they are not specified, these methods will examine the data and do their best to detect the appropriate attributes.  if dict is non-null, it will return a dictionary with various document-wide attributes accessible via ns...documentattribute keys.  
- (instancetype)initwithfileurl:(nsurl *)url options:(nsdictionary *)options documentattributes:(nsdictionary **)dict error:(nserror **)error ns_available_ios(7_0);  
- (instancetype)initwithdata:(nsdata *)data options:(nsdictionary *)options documentattributes:(nsdictionary **)dict error:(nserror **)error ns_available_ios(7_0);  
  
// generates an nsdata object for the receiver contents in range.  it requires a document attributes dict specifying at least the nsdocumenttypedocumentattribute to determine the format to be written.  
- (nsdata *)datafromrange:(nsrange)range documentattributes:(nsdictionary *)dict error:(nserror **)error ns_available_ios(7_0);  
  
// returns an nsfilewrapper object for the receiver contents in range.  it requires a document attributes dict specifying at least the nsdocumenttypedocumentattribute to determine the format to be written.  the method returns a directory file wrapper for those document types represented by a file package such as nsrtfdtextdocumenttype; otherwise, it returns a regular-file file wrapper.  
- (nsfilewrapper *)filewrapperfromrange:(nsrange)range documentattributes:(nsdictionary *)dict error:(nserror **)error ns_available_ios(7_0);  
  
@end  
  
@interface nsmutableattributedstring (nsmutableattributedstringdocumentformats)  
// methods replacing the receiver contents with an external document data.  options specify document attributes for interpreting the document contents.  nsdocumenttypedocumentattribute, nscharacterencodingdocumentattribute, and nsdefaultattributesdocumentattribute are supported options key.  when they are not specified, these methods will examine the data and do their best to detect the appropriate attributes.  if dict is non-null, it will return a dictionary with various document-wide attributes accessible via ns...documentattribute keys.  
- (bool)readfromfileurl:(nsurl *)url options:(nsdictionary *)opts documentattributes:(nsdictionary **)dict error:(nserror **)error ns_available_ios(7_0);  
- (bool)readfromdata:(nsdata *)data options:(nsdictionary *)opts documentattributes:(nsdictionary **)dict error:(nserror **)error ns_available_ios(7_0);  
@end