代碼下載
富文本的屬性介紹
NSFontAttributeName 設(shè)置字體屬性,默認(rèn)值:字體:Helvetica(Neue) 字號(hào):12
NSForegroundColorAttributeNam 設(shè)置字體顏色,取值為 UIColor對(duì)象,默認(rèn)值為黑色
NSBackgroundColorAttributeName 設(shè)置字體所在區(qū)域背景顏色,取值為 UIColor對(duì)象,默認(rèn)值為nil, 透明色
NSLigatureAttributeName 設(shè)置連體屬性,取值為NSNumber 對(duì)象(整數(shù)),0 表示沒(méi)有連體字符,1 表示使用默認(rèn)的連體字符
NSKernAttributeName 設(shè)定字符間距,取值為 NSNumber 對(duì)象(整數(shù)),正值間距加寬,負(fù)值間距變窄
NSStrikethroughStyleAttributeName 設(shè)置刪除線,取值為 NSNumber 對(duì)象(整數(shù))
NSStrikethroughColorAttributeName 設(shè)置刪除線顏色,取值為 UIColor 對(duì)象,默認(rèn)值為黑色
NSUnderlineStyleAttributeName 設(shè)置下劃線,取值為 NSNumber 對(duì)象(整數(shù)),枚舉常量 NSUnderlineStyle中的值,與刪除線類似
NSUnderlineColorAttributeName 設(shè)置下劃線顏色,取值為 UIColor 對(duì)象,默認(rèn)值為黑色
NSStrokeWidthAttributeName 設(shè)置筆畫寬度,取值為 NSNumber 對(duì)象(整數(shù)),負(fù)值填充效果,正值中空效果
NSStrokeColorAttributeName 填充部分顏色,不是字體顏色,取值為 UIColor 對(duì)象
NSShadowAttributeName 設(shè)置陰影屬性,取值為 NSShadow 對(duì)象
NSTextEffectAttributeName 設(shè)置文本特殊效果,取值為 NSString 對(duì)象,目前只有圖版印刷效果可用:
NSBaselineOffsetAttributeName 設(shè)置基線偏移值,取值為 NSNumber (float),正值上偏,負(fù)值下偏
NSObliquenessAttributeName 設(shè)置字形傾斜度,取值為 NSNumber (float),正值右傾,負(fù)值左傾
NSExpansionAttributeName 設(shè)置文本橫向拉伸屬性,取值為 NSNumber (float),正值橫向拉伸文本,負(fù)值橫向壓縮文本
NSWritingDirectionAttributeName 設(shè)置文字書寫方向,從左向右書寫或者從右向左書寫
NSVerticalGlyphFormAttributeName 設(shè)置文字排版方向,取值為 NSNumber 對(duì)象(整數(shù)),0 表示橫排文本,1 表示豎排文本
NSLinkAttributeName 設(shè)置鏈接屬性,點(diǎn)擊后調(diào)用瀏覽器打開指定URL地址
NSAttachmentAttributeName 設(shè)置文本附件,取值為NSTextAttachment對(duì)象,常用于文字圖片混排
NSParagraphStyleAttributeName 設(shè)置文本段落排版格式,取值為 NSParagraphStyle 對(duì)象
富文本段落排版格式屬性介紹
lineSpacing 字體的行間距
firstLineHeadIndent 首行縮進(jìn)
alignment (兩端對(duì)齊的)文本對(duì)齊方式:(左,中,右,兩端對(duì)齊,自然)
lineBreakMode 結(jié)尾部分的內(nèi)容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
headIndent 整體縮進(jìn)(首行除外)
tailIndent
minimumLineHeight 最低行高
maximumLineHeight 最大行高
paragraphSpacing 段與段之間的間距
paragraphSpacingBefore 段首行空白空間
baseWritingDirection 書寫方向(一共三種)
lineHeightMultiple
hyphenationFactor 連字屬性 在iOS,唯一支持的值分別為0和1
上幾張效果圖

Simulator Screen Shot 2017年1月12日 下午3.13.04.png

Simulator Screen Shot 2017年1月12日 下午3.13.49.png

Simulator Screen Shot 2017年1月12日 下午3.14.08.png
編碼使用舉例
說(shuō)明:由于富文本的屬性太多,使用也相當(dāng)簡(jiǎn)單,就不一一舉例,用法都相差不多,示例代碼都有相應(yīng)的例子,可以去下載。
富文本一般是給控件的attributedText賦值的,也可以繪制到控件上。
1.最普遍的屬性使用(如文本顏色)
NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:@"我是紅色文字" attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
self.label.attributedText = attributedStr;
2.文本附件和鏈接
先為textView設(shè)置富文本
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
UIImage *image = [UIImage imageNamed:@"Weizhifu"];
attachment.image = image;
attachment.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
NSAttributedString *attributedStr = [NSAttributedString attributedStringWithAttachment:attachment];
NSMutableAttributedString *mattributedStr = [[NSMutableAttributedString alloc] initWithString:@"我是帶附件的文字" attributes:nil];
[mattributedStr insertAttributedString:attributedStr atIndex:mattributedStr.length];
self.attachmentTextView.attributedText = mattributedStr;
attributedStr = [[NSAttributedString alloc] initWithString:@"我是帶鏈接的文字" attributes:@{NSLinkAttributeName:@"https://www.baidu.com"}];
self.linkTextView.attributedText = mattributedStr;
然后在UITextViewDelegate的如下兩個(gè)代理方法中可以對(duì)附件和鏈接進(jìn)行交互
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
NSRange range = NSMakeRange(0, 0);
NSDictionary *attributes;
while (range.location + range.length < self.model.attributedModel.effectString.length) {
attributes = [self.model.attributedModel.effectString attributesAtIndex:range.location + range.length effectiveRange:&range];
if ([attributes valueForKey:NSLinkAttributeName]) {
return YES;
}
}
return NO;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
{
NSRange range = NSMakeRange(0, 0);
NSDictionary *attributes;
while (range.location + range.length < self.model.attributedModel.effectString.length) {
attributes = [self.model.attributedModel.effectString attributesAtIndex:range.location + range.length effectiveRange:&range];
if ([attributes valueForKey:NSAttachmentAttributeName]) {
return YES;
}
}
return NO;
}