【iOS】文字处理之Character Attributes
作用:可用来做iOS的一些特效文字 扩展到图文混排和文字自适应
- 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
- 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.
- 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.
- NSCursorAttributeName
The value of this attribute is an NSCursor object. The default value is the cursor returned by the IBeamCursor method
- 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.
- 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.
- 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.
- 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.
- 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.)
- 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.
- NSMarkedClauseSegmentAttributeName
The value of this attribute is an NSNumber object containing an integer, as an index in marked text indicating clause segments
- 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.
- 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.
- NSShadowAttributeName
The value of this attribute is an NSShadow object. The default value of this property is nil.
- NSStrikethroughColorAttributeName
The value of this attribute is a UIColor object. The default value is nil, indicating same as foreground color.
- 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.
- 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.
- 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.
- 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.
- NSUnderlineColorAttributeName
The value of this attribute is a UIColor object. The default value is nil, indicating same as foreground color.
- 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.
- 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.
- 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.