NSAttributedString

App官方文档

运用 NSAttributedString 的场景一般有两类

  • titleTextAttributes //等直接是接受文字的属性 字典类型参数
  • NSAttributedString(string: String, attributes: [String : AnyObject]?) //等返回 NSAttributedString 对象给某个特定的参数

常用属性

  • 文本颜色
    [NSForegroundColorAttributeName: UIColor.whiteColor()]

  • 文本字体
    [NSFontAttributeName: UIFont(name: "Times new roman", size: 20)!]

属性列表

SWIFT 声明

let NSAttachmentAttributeName: String
let NSBackgroundColorAttributeName: String
let NSBaselineOffsetAttributeName: String
let NSExpansionAttributeName: String
let NSFontAttributeName: String
let NSForegroundColorAttributeName: String
let NSKernAttributeName: String
let NSLigatureAttributeName: String
let NSLinkAttributeName: String
let NSObliquenessAttributeName: String
let NSParagraphStyleAttributeName: String 
let NSShadowAttributeName: String
let NSStrikethroughColorAttributeName: String
let NSStrikethroughStyleAttributeName: String
let NSStrokeColorAttributeName: String
let NSStrokeWidthAttributeName: String
let NSTextEffectAttributeName: String
let NSUnderlineColorAttributeName: String
let NSUnderlineStyleAttributeName: String
let NSVerticalGlyphFormAttributeName: String
let NSWritingDirectionAttributeName: String 

属性解释

  • NSAttachmentAttributeName
    The value of this attribute is an NSTextAttachment object. The default value of this property is nil, indicating no attachment.

Available in iOS 7.0 and later.

  • NSBackgroundColorAttributeName
    The value of this attribute is a UIColor object. Use this attribute to specify the color of the background area behind the text. If you do not specify this attribute, no background color is drawn.

Available in iOS 6.0 and later.

  • NSBaselineOffsetAttributeName
    The value of this attribute is an NSNumber object containing a floating point value indicating the character’s offset from the baseline, in points. The default value is 0.

Available in iOS 7.0 and later.

  • NSExpansionAttributeName
    The value of this attribute is an NSNumber object containing a floating point value indicating the log of the expansion factor to be applied to glyphs. The default value is 0, indicating no expansion.

Available in iOS 7.0 and later.

  • NSFontAttributeName
    The value of this attribute is a UIFont object. Use this attribute to change the font for a range of text. If you do not specify this attribute, the string uses a 12-point Helvetica(Neue) font by default.

Available in iOS 6.0 and later.

  • NSForegroundColorAttributeName
    The value of this attribute is a UIColor object. Use this attribute to specify the color of the text during rendering. If you do not specify this attribute, the text is rendered in black.

Available in iOS 6.0 and later.

  • NSKernAttributeName
    The value of this attribute is an NSNumber object containing a floating-point value. This value specifies the number of points by which to adjust kern-pair characters. Kerning prevents unwanted space from occurring between specific characters and depends on the font. The value 0 means kerning is disabled. The default value for this attribute is 0.

Available in iOS 6.0 and later.

  • NSLigatureAttributeName
    The value of this attribute is an NSNumber object containing an integer. Ligatures cause specific character combinations to be rendered using a single custom glyph that corresponds to those characters. The value 0 indicates no ligatures. The value 1 indicates the use of the default ligatures. The value 2 indicates the use of all ligatures. The default value for this attribute is 1. (Value 2 is unsupported on iOS.)

Available in iOS 6.0 and later.

  • NSLinkAttributeName
    The value of this attribute is and NSURL object (preferred) or an NSString object. The default value of this property is nil, indicating no link.

Available in iOS 7.0 and later.

  • NSObliquenessAttributeName
    The value of this attribute is an NSNumber object containing a floating point value indicating skew to be applied to glyphs. The default value is 0, indicating no skew.

Available in iOS 7.0 and later.

  • NSParagraphStyleAttributeName
    The value of this attribute is an NSParagraphStyle object. Use this attribute to apply multiple attributes to a range of text. If you do not specify this attribute, the string uses the default paragraph attributes, as returned by the defaultParagraphStyle method of NSParagraphStyle.

Available in iOS 6.0 and later.

  • NSShadowAttributeName
    The value of this attribute is an NSShadow object. The default value of this property is nil.

Available in iOS 6.0 and later.

  • NSStrikethroughColorAttributeName
    The value of this attribute is a UIColor object. The default value is nil, indicating same as foreground color.

Available in iOS 7.0 and later.

  • NSStrikethroughStyleAttributeName
    The value of this attribute is an NSNumber object containing an integer. This value indicates whether the text has a line through it and corresponds to one of the constants described in NSUnderlineStyle. The default value for this attribute is NSUnderlineStyleNone.

Available in iOS 6.0 and later.

  • NSStrokeColorAttributeName
    The value of this parameter is a UIColor object. If it is not defined (which is the case by default), it is assumed to be the same as the value of NSForegroundColorAttributeName; otherwise, it describes the outline color. For more details, see Drawing attributed strings that are both filled and stroked.

Available in iOS 6.0 and later.

  • NSStrokeWidthAttributeName
    The value of this attribute is an NSNumber object containing a floating-point value. This value represents the amount to change the stroke width and is specified as a percentage of the font point size. Specify 0 (the default) for no additional changes. Specify positive values to change the stroke width alone. Specify negative values to stroke and fill the text. For example, a typical value for outlined text would be 3.0.

Available in iOS 6.0 and later.

  • NSTextEffectAttributeName
    The value of this attribute is an NSString object. Use this attribute to specify a text effect, such as NSTextEffectLetterpressStyle. The default value of this property is nil, indicating no text effect.

Available in iOS 7.0 and later.

  • NSUnderlineColorAttributeName
    The value of this attribute is a UIColor object. The default value is nil, indicating same as foreground color.

Available in iOS 7.0 and later.

  • NSUnderlineStyleAttributeName
    The value of this attribute is an NSNumber object containing an integer. This value indicates whether the text is underlined and corresponds to one of the constants described in NSUnderlineStyle. The default value for this attribute is NSUnderlineStyleNone.

Available in iOS 6.0 and later.

  • NSVerticalGlyphFormAttributeName
    The value of this attribute is an NSNumber object containing an integer. The value 0 indicates horizontal text. The value 1 indicates vertical text. In iOS, horizontal text is always used and specifying a different value is undefined.

Available in iOS 6.0 and later.

  • NSWritingDirectionAttributeName
    The value of this attribute is an NSArray object containing NSNumber objects representing the nested levels of writing direction overrides, in order from outermost to innermost.

This attribute provides a means to override the default bidirectional text algorithm, equivalent to using the Unicode bidi control characters LRE, RLE, LRO, or RLO paired with PDF, but as a higher-level attribute. (See Unicode Standard Annex #9 for information about the Unicode bidi formatting codes.) The NSWritingDirectionAttributeName constant is a character-level attribute that provides a higher-level alternative to the inclusion of explicit bidirectional control characters in text. It is the NSAttributedString equivalent of the HTML markup using bdo element with the dir attribute.

The values of the NSNumber objects should be 0, 1, 2, or 3, for LRE, RLE, LRO, or RLO respectively, and combinations of NSWritingDirectionLeftToRight and NSWritingDirectionRightToLeft with NSTextWritingDirectionEmbedding or NSTextWritingDirectionOverride, as shown in Table 1.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,254评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,875评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,682评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,896评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,015评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,152评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,208评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,962评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,388评论 1 304
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,700评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,867评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,551评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,186评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,901评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,142评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,689评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,757评论 2 351

推荐阅读更多精彩内容