【iOS】文字处理之Character Attributes

Character Attributes

作用:可用来做iOS的一些特效文字 扩展到图文混排和文字自适应


  1. NSAttachmentAttributeName

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

图文混排效果
- (void)viewDidLoad {
  [super viewDidLoad];
  self.view.backgroundColor = [UIColor greenColor];    
  //图片
  NSTextAttachment *txa = [[NSTextAttachment alloc] init];
  txa.image = [UIImage imageNamed:@"k2"];
  txa.bounds = CGRectMake(0, -20, 50, 50);//默认以文字底部线为中心 而不是文字的中心
  NSAttributedString *imageA = [NSAttributedString attributedStringWithAttachment:txa];
  //文字
  NSString *testStr = @"感觉自己萌萌哒- -";
  NSAttributedString *att = [[NSAttributedString alloc] initWithString:testStr attributes:nil];
  
  NSMutableAttributedString *mu = [[NSMutableAttributedString alloc] init];
  [mu appendAttributedString:att];
  [mu insertAttributedString:imageA atIndex:7];
  UILabel *textLable = [[UILabel alloc] init];
  textLable.attributedText = mu;
  textLable.backgroundColor = [UIColor whiteColor];
  [self.view addSubview:textLable];
  textLable.center = self.view.center;
  textLable.bounds = CGRectMake(0, 0, 500, 500);
}

然而我却不知道这个Key值到底是干嘛用的 因为图文混排的话不能像字体等通过字典传给字符串转换成NSAttributedString

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

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

  1. NSCursorAttributeName

The value of this attribute is an NSCursor object. The default value is the cursor returned by the IBeamCursor method

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

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

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

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

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

  1. NSLinkAttributeName

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

  1. NSMarkedClauseSegmentAttributeName

The value of this attribute is an NSNumber object containing an integer, as an index in marked text indicating clause segments

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

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

  1. NSShadowAttributeName

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

  1. NSStrikethroughColorAttributeName

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

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

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

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

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

  1. NSUnderlineColorAttributeName

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

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

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

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

Table 1
Values of NSWritingDirectionAttributeName and equivalent markup

Array NSNumber Values Unicode Control Characters Writing Direction Constants
0 LRE NSWritingDirectionLeftToRight / NSTextWritingDirectionEmbedding
1 RLE NSWritingDirectionRightToLeft / NSTextWritingDirectionEmbedding
2 LRO NSWritingDirectionLeftToRight / NSTextWritingDirectionOverride
3 RLO NSWritingDirectionRightToLeft / NSTextWritingDirectionOverride

mac os
NSToolTipAttributeName

The value of this attribute is an NSString object containing the tooltip text. The default value is nil, indicating no tooltip is available.

NSTextAlternativesAttributeName

The value of this attribute is an NSTextAlternatives object representing alternatives for a string that may be presented to the user.

NSSpellingStateAttributeName

This key is available in macOS 10.2 and later, but its interpretation changed in OS X v10.5. Previously, any non-zero value caused the spelling indicator to be displayed. For macOS 10.5 and later, the (integer) value is treated as being composed of the spelling and grammar flags. See NSSpellingStateAttributeName Flags for possible values.
The value of this attribute is an integer. Defaults to 0, indicating no grammar or spelling error.

NSSuperscriptAttributeName

The value of this attribute is an NSNumber object containing an integer. The default value is 0.

参考: http://www.jianshu.com/p/3a0016b4c3de

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

推荐阅读更多精彩内容