【iOS】文字處理之Character Attributes

Character Attributes

作用:可用來(lái)做iOS的一些特效文字 擴(kuò)展到圖文混排和文字自適應(yīng)


  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);//默認(rèn)以文字底部線(xiàn)為中心 而不是文字的中心
  NSAttributedString *imageA = [NSAttributedString attributedStringWithAttachment:txa];
  //文字
  NSString *testStr = @"感覺(jué)自己萌萌噠- -";
  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);
}

然而我卻不知道這個(gè)Key值到底是干嘛用的 因?yàn)閳D文混排的話(huà)不能像字體等通過(guò)字典傳給字符串轉(zhuǎn)換成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.reibang.com/p/3a0016b4c3de

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市享幽,隨后出現(xiàn)的幾起案子铲掐,更是在濱河造成了極大的恐慌,老刑警劉巖值桩,帶你破解...
    沈念sama閱讀 222,464評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件摆霉,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)携栋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)搭盾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人刻两,你說(shuō)我怎么就攤上這事增蹭。” “怎么了磅摹?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,078評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵滋迈,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我户誓,道長(zhǎng)饼灿,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,979評(píng)論 1 299
  • 正文 為了忘掉前任帝美,我火速辦了婚禮碍彭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘悼潭。我一直安慰自己庇忌,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布舰褪。 她就那樣靜靜地躺著皆疹,像睡著了一般。 火紅的嫁衣襯著肌膚如雪占拍。 梳的紋絲不亂的頭發(fā)上略就,一...
    開(kāi)封第一講書(shū)人閱讀 52,584評(píng)論 1 312
  • 那天,我揣著相機(jī)與錄音晃酒,去河邊找鬼表牢。 笑死,一個(gè)胖子當(dāng)著我的面吹牛贝次,可吹牛的內(nèi)容都是我干的崔兴。 我是一名探鬼主播,決...
    沈念sama閱讀 41,085評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼蛔翅,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼恼布!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起搁宾,我...
    開(kāi)封第一講書(shū)人閱讀 40,023評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤折汞,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后盖腿,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體爽待,經(jīng)...
    沈念sama閱讀 46,555評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡损同,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鸟款。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片膏燃。...
    茶點(diǎn)故事閱讀 40,769評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖何什,靈堂內(nèi)的尸體忽然破棺而出组哩,到底是詐尸還是另有隱情,我是刑警寧澤处渣,帶...
    沈念sama閱讀 36,439評(píng)論 5 351
  • 正文 年R本政府宣布伶贰,位于F島的核電站,受9級(jí)特大地震影響罐栈,放射性物質(zhì)發(fā)生泄漏黍衙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評(píng)論 3 335
  • 文/蒙蒙 一荠诬、第九天 我趴在偏房一處隱蔽的房頂上張望琅翻。 院中可真熱鬧,春花似錦柑贞、人聲如沸方椎。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,601評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)棠众。三九已至,卻和暖如春康辑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背轿亮。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,702評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工疮薇, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人我注。 一個(gè)月前我還...
    沈念sama閱讀 49,191評(píng)論 3 378
  • 正文 我出身青樓按咒,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親但骨。 傳聞我的和親對(duì)象是個(gè)殘疾皇子励七,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容