常用屬性
key | value | 說明 |
---|---|---|
NSFontAttributeName | UIFont對象 | 字體大蟹硇:默認(rèn)Helvetica(Neue) 12 |
NSParagraphStyleAttributeName | NSParagraphStyle對象 | 段落設(shè)置 |
NSForegroundColorAttributeName | UIColor對象 | 字體顏色羔飞,默認(rèn)blackColor |
NSBackgroundColorAttributeName | UIColor對象 | 背景色,默認(rèn)nil(無背景色) |
NSLigatureAttributeName | 包含整數(shù)的NSNumber對象 | 連字符:默認(rèn)為1:默認(rèn)連接,0:不連接 |
NSKernAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對象 | 字符間距:默認(rèn)0(禁用) |
NSTrackingAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對象 | 修改默認(rèn)跟蹤的數(shù)量嫁艇。0表示禁用跟蹤攀芯。iOS14及以后系統(tǒng)可用。 |
NSStrikethroughStyleAttributeName | 包含整數(shù)的NSNumber對象 | 刪除線:默認(rèn)0(無刪除線) |
NSUnderlineStyleAttributeName | 包含整數(shù)的NSNumber對象 | 下劃線:默認(rèn)0(無下劃線) |
NSStrokeColorAttributeName | UIColor對象 | 描邊顏色:nil(和文字的 foregroundColor一致) |
NSStrokeWidthAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對象 | 描邊寬度:正值空心描邊轨奄,負(fù)值實(shí)心描邊孟害,默認(rèn)0(不描邊) |
NSShadowAttributeName | NSShadow對象 | 文本陰影,默認(rèn)為nil:無陰影 |
NSTextEffectAttributeName | NSString對象 | 文字效果:默認(rèn)nil(沒有文字效果) |
NSAttachmentAttributeName | NSTextAttachment對象 | 附件(常用作圖文混排) :默認(rèn)nil(沒有附件) |
NSLinkAttributeName | NSURL (優(yōu)先) 或 NSString對象 | 鏈接 |
NSBaselineOffsetAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對象 | 基線偏移量,默認(rèn)為0;正值向上偏移挪拟,負(fù)值向下偏移挨务,默認(rèn)0(不偏移) |
NSUnderlineColorAttributeName | UIColor對象 | 下劃線顏色:默認(rèn)nil(和文字的 foregroundColor一致) |
NSStrikethroughColorAttributeName | UIColor對象 | 刪除線顏色:默認(rèn) nil(和文字的 foregroundColor一致) |
NSObliquenessAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對象 | 字體傾斜 :正值向右傾斜,負(fù)值向左傾斜玉组, 默認(rèn)0(不傾斜) |
NSExpansionAttributeName | 包含浮點(diǎn)數(shù)的NSNumber對象 | 文本扁平化:正值橫向拉伸谎柄,負(fù)值橫向壓縮,默認(rèn)0(不拉伸) |
NSWritingDirectionAttributeName | 存儲NSNumber類型的NSArray對象 | 書寫方向惯雳〕祝控制字符可以通過屏蔽NSWritingDirection和NSWritingDirectionFormatType值來獲得。 LRE: NSWritingDirectionLeftToRight\NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft\NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight\NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft\NSWritingDirectionOverride |
NSVerticalGlyphFormAttributeName | 包含整數(shù)的NSNumber對象 | 0表示水平文本石景。1表示垂直文本劈猿。如果沒有指定,它可以遵循更高級別的垂直方向設(shè)置潮孽。目前在iOS上揪荣,它總是水平的。任何其他值的行為是未定義的往史。 |
示例
NSLigatureAttributeName-連字符
UIFont *font = [UIFont fontWithName:@"futura" size:18];
NSString *text = @"逗號前面的我是一個沒有連字符樣式的fl仗颈,逗號后面的你是一個帶連字符樣式的fl(你看后半句的漢字連字符樣式好難體現(xiàn)出來哦)";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
// 設(shè)置文本前半句無連字符效果
[att addAttribute:NSLigatureAttributeName value:[NSNumber numberWithInt:0] range:NSMakeRange(0, 19)];
// 設(shè)置文本后半句有連字符效果
[att addAttribute:NSLigatureAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(19, text.length - 19)];
NSKernAttributeName - 字符間距
注意: 正值間距加寬,負(fù)值間距變窄椎例,0表示默認(rèn)效果
UIFont *font = [UIFont fontWithName:@"futura" size:18];
NSString *text = @"設(shè)置我的字間距為正值20有拉大效果挨决,中間的你是正常效果,設(shè)置他的字間距為負(fù)值-5有減少效果";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[att addAttribute:NSKernAttributeName value:@20 range:NSMakeRange(0, 18)];
[att addAttribute:NSKernAttributeName value:@(-5) range:NSMakeRange(28, text.length - 28)];
文字描邊
NSStrokeColorAttributeName - 描邊顏色
NSStrokeWidthAttributeName - 描邊寬度
- 描邊顏色要搭配非0的描邊寬度才會生效订歪,如果只設(shè)置了描邊顏色脖祈,描邊寬度為0,則沒有描邊效果
- 描邊寬度是正數(shù)刷晋,會對文字進(jìn)行描邊撒犀,但文字中心不填充( 一種經(jīng)典的空心文本樣式是在該值為3.0)
- 描邊寬度是負(fù)數(shù)福压,會對文字進(jìn)行描邊,而且會同時對文字中心進(jìn)行填充(填充的顏色為文字本來的字體顏色)
UIFont *font = [UIFont fontWithName:@"futura" size:18];
NSString *text = @"只設(shè)置描邊顏色或舞,沒有設(shè)置描邊寬度(默認(rèn)為0)荆姆,沒有效果";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[att addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, text.length)];
UIFont *font = [UIFont fontWithName:@"futura" size:18];
NSString *text = @"將描邊寬度設(shè)置為正數(shù)3,無描邊顏色映凳,具有空心效果哦胆筒,此時描邊顏色默認(rèn)成字體本來的顏色!";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[att addAttribute:NSStrokeWidthAttributeName value:@(3) range:NSMakeRange(0, text.length)];
UIFont *font = [UIFont fontWithName:@"futura" size:18];
NSString *text = @"將描邊寬度設(shè)置為正數(shù)3诈豌,描邊顏色為紅色仆救,具有空心效果哦,因為正數(shù)不對文字內(nèi)部進(jìn)行填充矫渔!";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[att addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, text.length)];
[att addAttribute:NSStrokeWidthAttributeName value:@(3) range:NSMakeRange(0, text.length)];
UIFont *font = [UIFont fontWithName:@"futura" size:18];
NSString *text = @"將描邊寬度設(shè)置為負(fù)數(shù)-3彤蔽,又設(shè)置描邊顏色,無空心效果庙洼,因為負(fù)數(shù)會對文字內(nèi)部進(jìn)行填充顿痪!";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[att addAttribute:NSStrokeColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(0, text.length)];
[att addAttribute:NSStrokeWidthAttributeName value:@(-3) range:NSMakeRange(0, text.length)];
NSShadowAttributeName-陰影
UIFont *font = [UIFont fontWithName:@"futura" size:18];
NSString *text = @"一個有陰影的文本!";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
// 創(chuàng)建NSShadow實(shí)例
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor purpleColor];
shadow.shadowBlurRadius = 3.0;
shadow.shadowOffset = CGSizeMake(0, 0.8);
// 添加屬性
[att addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, text.length)];
NSTextEffectAttributeName-文字效果
UIFont *font = [UIFont fontWithName:@"futura" size:18];
NSString *text = @"我是沒有文字效果的油够,你是有文字效果的蚁袭!";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
// [attributes setObject:[UIColor darkGrayColor] forKey:NSBackgroundColorAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[att addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(10, text.length - 10)];
NSLinkAttributeName - 鏈接
注意:UILabel無法使用該屬性, 但UITextView 控件可以使用,所以下面關(guān)于 NSLinkAttributeName 屬性的代碼也是使用 UITextView 來測試的石咬。
// 注意:跳轉(zhuǎn)鏈接要實(shí)現(xiàn)UITextView的這個委托方法
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange {
return YES;
}
- (void)linkAttributeTest {
NSString *text = @"點(diǎn)我跳轉(zhuǎn)到百度哦揩悄!";
NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
[attributeStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(0, text.length)];
self.textView.attributedText = attributeStr;
}
NSBaselineOffsetAttributeName-基準(zhǔn)線
注意:正值向上偏移,負(fù)值向下偏移鬼悠,默認(rèn)0(不偏移)
UIFont *font = [UIFont fontWithName:@"Savoye Let" size:18];
NSString *text = @"負(fù)值向下偏移";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[att addAttribute:NSBaselineOffsetAttributeName value:@(-10) range:NSMakeRange(0, text.length)];
NSExpansionAttributeName -文本扁平化(橫向拉伸)
注意:正值橫向拉伸删性,負(fù)值橫向壓縮,默認(rèn)0(不拉伸)
UIFont *font = [UIFont fontWithName:@"Savoye Let" size:18];
NSString *text = @"正值橫向拉伸焕窝,無扁平效果蹬挺,負(fù)值橫向壓縮!";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[att addAttribute:NSExpansionAttributeName value:@(0.8) range:NSMakeRange(0, 7)];
[att addAttribute:NSExpansionAttributeName value:@(-0.8) range:NSMakeRange(13, text.length - 13)];
NSTrackingAttributeName
類似NSKernAttributeName袜啃。
UIFont *font = [UIFont fontWithName:@"Savoye Let" size:18];
NSString *text = @"NSTrackingAttributeName,NSTrackingAttributeName幸缕,NSTrackingAttributeName";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[att addAttribute:NSTrackingAttributeName value:@(8.8) range:NSMakeRange(0, 23)];
[att addAttribute:NSTrackingAttributeName value:@(-8.8) range:NSMakeRange(46, 23)];
段落設(shè)置
NSMutableParagraphStyle
屬性 | 說明 |
---|---|
lineSpacing | 行間距 |
paragraphSpacing | 段落間距 |
alignment | 對齊方式 |
firstLineHeadIndent | 首行縮進(jìn) |
headIndent | 整段縮進(jìn) |
tailIndent | 右側(cè)縮進(jìn) |
lineBreakMode | 截斷方式 |
minimumLineHeight | 最小行高 |
maximumLineHeight | 最大行高 |
baseWritingDirection | 書寫方向 |
lineHeightMultiple | 行間距倍數(shù) |
paragraphSpacingBefore | 段首行空白空間 |
hyphenationFactor | 連字屬性 在iOS群发,唯一支持的值分別為0和1 |
tabStops | 制表符 |
allowsDefaultTighteningForTruncation | 收縮字符間距允許截斷 |
UIFont *font = [UIFont fontWithName:@"Savoye Let" size:18];
NSString *text = @"段落\n右對齊\n行間距20";
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
style.lineBreakMode = NSLineBreakByWordWrapping;
style.alignment = NSTextAlignmentRight;
style.lineSpacing = 20;
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:style forKey:NSParagraphStyleAttributeName];
[attributes setObject:font forKey:NSFontAttributeName];
NSAttributedString *att = [[NSAttributedString alloc] initWithString:text attributes:attributes];
不同大小的字體對齊
默認(rèn)基線對齊。iOS UIFont簡介與文本行數(shù)判斷
UIFont *font = [UIFont systemFontOfSize:21];
UIFont *font2 = [UIFont systemFontOfSize:14];
NSString *text = @"1??不同大小的字體底部對齊??樣式(默認(rèn))\n2??不同大小的字體頂部對齊??樣式\n3??不同大小的字體中間對齊??樣式";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
[att addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(10, 6)];
[att addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(10, 6)];
[att addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(33, 6)];
[att addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(33, 6)];
[att addAttribute:NSBaselineOffsetAttributeName value:@(font.ascender - font2.ascender) range:NSMakeRange(33, 6)];
[att addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(52, 6)];
[att addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(52, 6)];
[att addAttribute:NSBaselineOffsetAttributeName value:@((font.lineHeight - font2.lineHeight)/2 + ((font.descender - font2.descender))) range:NSMakeRange(52, 6)];
圖片
圖片
UIFont *font = [UIFont systemFontOfSize:24];
NSString *text = @"富文本圖片";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
[attributes setObject:[UIColor yellowColor] forKey:NSBackgroundColorAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
NSLog(@"純文字:%ld",att.length);
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"1"];
attachment.bounds = CGRectMake(0,0,30,20);
[att appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
NSLog(@"文字+圖片:%ld",att.length);
圖片長度
UIFont *font = [UIFont systemFontOfSize:24];
NSString *text = @"富文本圖片";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
[attributes setObject:[UIColor yellowColor] forKey:NSBackgroundColorAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
NSLog(@"純文字:%ld",att.length);
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = nil;
attachment.bounds = CGRectMake(0,0,30,20);
[att appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
NSLog(@"文字+圖片:%ld",att.length);
純文字:5
文字+圖片:6
圖片添加至NSTextAttachment生成富文本后发乔,占一個字節(jié)長度(無論圖片大小或是否存在)熟妓。
圖片對齊
NSTextAttachment默認(rèn)也是基線對齊,attachment.bounds的坐標(biāo)原點(diǎn)Y軸是和基線持平栏尚,是CoreGraphics的坐標(biāo)系起愈。
原理同上述文字對齊。
UIFont *font = [UIFont systemFontOfSize:24];
NSString *text = @"富文本圖片";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
[attributes setObject:[UIColor yellowColor] forKey:NSBackgroundColorAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
NSLog(@"純文字:%ld",att.length);
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"1"];
attachment.bounds = CGRectMake(0,0,30,20);
[att appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
[att addAttribute:NSBaselineOffsetAttributeName value:@((font.lineHeight - 20)/2 + (font.descender)) range:NSMakeRange(5, 1)];
NSLog(@"文字+圖片:%ld",att.length);
UIFont *font = [UIFont systemFontOfSize:24];
NSString *text = @"文本\n富文本圖片\n文本";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
[attributes setObject:[UIColor yellowColor] forKey:NSBackgroundColorAttributeName];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];
NSLog(@"純文字:%ld",att.length);
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"1"];
attachment.bounds = CGRectMake(0,0,30,20);
NSAttributedString *textAtt = [NSAttributedString attributedStringWithAttachment:attachment];
[att insertAttributedString:textAtt atIndex:6];
[att addAttribute:NSBaselineOffsetAttributeName value:@((font.lineHeight - 20)/2 + (font.descender)) range:NSMakeRange(6, 1)];
NSLog(@"文字+圖片:%ld",att.length);
文字圖片間隔
NSKernAttributeName屬性對圖片與文字的間距設(shè)置不生效。
網(wǎng)絡(luò)圖片
判斷網(wǎng)絡(luò)圖片是否存在抬虽,不存在先試用占位圖官觅,開啟異步下載,下載完成后重新生成富文本替換阐污。
參考文檔
iOS富文本NSAttributedString垂直對齊
iOS--NSAttributedString超全屬性詳解及應(yīng)用(富文本休涤、圖文混排)