ios开发文字排版,段落排版,富文本

https://blog.csdn.net/weidfyr/article/details/48316775

1,使用NSMutableAttributedString设置文字的各种属性

1)第一种方式

建立一个属性字典,,用字典和字符串初始化NSMutableAttributedString对象,这个对象就有了属性字典中的属性

2)第二种方式

先用字符串初始化一个NSMutableAttributedString类型对象,然后单独给指定范围的文字指定属性。

3)使用步骤,Demo:

//1,建立一个字符串
NSString *str = @"在北京时间3 月 10 日凌晨的苹果发布活动上,HBO 宣布推出与苹果独家合作的 HBO Now 服务,并播出了万众期待的第五季《权力的游戏》预告片。 2015年3月11日,HBO 正式宣布了《权力的游戏》第五季将在全球同步播出的决定,通过 HBO 旗下的全球各个电视网播出,该剧首播将在美国东部时间 4 月 12 日晚 9 点(北京时间 4 月13 日早 9 点)进行,包括 HBO 亚洲,HBO 加拿大,HBO 欧洲,HBO 拉美,HBO 荷兰,HBO 北欧各个电视网将于同一时间播出。";
//2,定义字符串的属性字典
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineSpacing = 10; //设置行间距
NSDictionary *attributeDict = @{
NSFontAttributeName: [UIFont systemFontOfSize:12],
NSForegroundColorAttributeName: [UIColor blackColor],
NSKernAttributeName: @2, NSParagraphStyleAttributeName: paragraph};
//3,根据属性字典和字符串,计算字符串使用指定的属性设置时候,占用空间的大小,返回CGRect类型
CGSize contentSize = [str boundingRectWithSize:CGSizeMake(self.view.frame.size.width-40, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributeDict
context:nil].size;//计算文字大小

//4,构建带格式的文本字符串
NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str attributes:attributeDict];
//5,构建label
_textLabel = [[UILabel alloc] init];
_textLabel.frame = CGRectMake(20, 20, contentSize.width, contentSize.height);
_textLabel.numberOfLines = 0; //设置分行显示,必须必须设置这个属性
//6,将格式化的文本添加到label上。
_textLabel.attributedText = attributeStr;
4)NSMutableAttributedString 可以设置的属性

// NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
// NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色
// NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
// NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
// NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
// NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)
// NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
// NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
// NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
// NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
// NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象
// NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象
// NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
// NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
// NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
// NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
// NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
// NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
// NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址
// NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
// NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象
5)NSMutableAttributedString设置文字属性,Demo

NSMutableAttributedString *testStr = [[NSMutableAttributedString alloc] initWithString:str];
//1,设置指定范围内的 字体
[testStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(5, 30)];
//2,设置指定范围内 字体颜色
[testStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 10)];
//3,设置 字体所在区域的背景颜色
[testStr addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(40, 30)];
//4,设置连体属性,取值为NSNumber 对象(整数)
[testStr addAttribute:NSLigatureAttributeName value:@1 range:NSMakeRange(40, 30)];
//5,设置指定范围内 字符间距
[testStr addAttribute:NSKernAttributeName value:@10 range:NSMakeRange(70, 30)];
//6,设置删除线
[testStr addAttribute:NSStrikethroughStyleAttributeName value:@1 range:NSMakeRange(120, 20)];
//7,设置删除线的颜色
[testStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(120, 10)];
//8,设置下划线
[testStr addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSMakeRange(140, 20)];
//9,设置下划线颜色
[testStr addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor] range:NSMakeRange(140, 10)];
//10,设置字体倾斜度,负值向左,正值向右
[testStr addAttribute:NSObliquenessAttributeName value:@-0.5 range:NSMakeRange(160, 30)];
//11,设置字体拉伸压缩,正值拉伸,负值压缩
[testStr addAttribute:NSExpansionAttributeName value:@-0.5 range:NSMakeRange(190, 20)];
// 设置文字书写方向,从左向右书写或者从右向左书写
// [testStr addAttribute:NSWritingDirectionAttributeName value:@1 range:NSMakeRange(220, 20)];
//12,设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
[testStr addAttribute:NSVerticalGlyphFormAttributeName value:@1 range:NSMakeRange(0, testStr.length)];
//13,设置链接属性,点击后调用浏览器打开指定URL地址
[testStr addAttribute:NSLinkAttributeName value:@"http://what-forever.com" range:NSMakeRange(220, 20)];
//14,设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
[testStr addAttribute:NSStrokeWidthAttributeName value:@0.5 range:NSMakeRange(240, 30)];
//15,填充部分颜色,不是字体颜色,取值为 UIColor 对象
[testStr addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:NSMakeRange(240, 30)];
//16,设置阴影属性,取值为 NSShadow 对象
[testStr addAttribute:NSShadowAttributeName value:[[NSShadow alloc] init] range:NSMakeRange(270, 30)];
//17,设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
[testStr addAttribute:NSBaselineOffsetAttributeName value:@10 range:NSMakeRange(300, 30)];
//18,设置文本段落排版格式,取值为 NSParagraphStyle 对象
[testStr addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(70, 30)];
效果:

2,NSMutableParagraphStyle,继承自NSParagraphStyle

1)可以设置的属性:

1 @property(readwrite) CGFloat lineSpacing;              //行间距
2 @property(readwrite) CGFloat paragraphSpacing;           //段间距
3 @property(readwrite) NSTextAlignment alignment;           //对齐方式
4 @property(readwrite) CGFloat firstLineHeadIndent;          //首行缩紧
5 @property(readwrite) CGFloat headIndent;               //除首行之外其他行缩进
6 @property(readwrite) CGFloat tailIndent;               //每行容纳字符的宽度
7 @property(readwrite) NSLineBreakMode lineBreakMode;        //换行方式
8 @property(readwrite) CGFloat minimumLineHeight;           //最小行高
9 @property(readwrite) CGFloat maximumLineHeight;           //最大行高
10 @property(readwrite) NSWritingDirection baseWritingDirection;  //书写方式(NSWritingDirectionNatural,NSWritingDirectionLeftToRight,NSWritingDirectionRightToLeft)
11 @property(readwrite) CGFloat lineHeightMultiple;
12 @property(readwrite) CGFloat paragraphSpacingBefore;
13 @property(readwrite) float hyphenationFactor;
14 @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
15 @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);
2)Demo

NSMutableParagraphStyle *paragra = [[NSMutableParagraphStyle alloc] init];
paragra.lineSpacing = 5;//1,设置行间距
paragra.paragraphSpacing = 10; //2,设置段间距
paragra.alignment = UITextAlignmentLeft;//3,设置对齐方式
paragra.firstLineHeadIndent = 50;//4,首行缩进距离
paragra.headIndent = 10;//5,除首行之外其他行缩进
paragra.tailIndent = 300;//6,每行容纳字符的宽度
paragra.minimumLineHeight = 2;//7,每行最小高度
paragra.maximumLineHeight = 10;//8,每行最大高度
paragra.lineBreakMode = NSLineBreakByCharWrapping;//9,换行方式
//lineBreakMode 属性的可选项
{
// NSLineBreakByWordWrapping = 0, // Wrap at word boundaries, default */
// NSLineBreakByCharWrapping, /* Wrap at character boundaries */
// NSLineBreakByClipping, /* Simply clip */
// NSLineBreakByTruncatingHead, /* Truncate at head of line: "...wxyz" */
// NSLineBreakByTruncatingTail, /* Truncate at tail of line: "abcd..." */
// NSLineBreakByTruncatingMiddle /* Truncate middle of line: "ab...yz" */
}

[testStr addAttribute:NSParagraphStyleAttributeName value:paragra range:NSMakeRange(0, testStr.length)];

3,NSShadow用法    
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor redColor]; //设置阴影的颜色
shadow.shadowOffset = CGSizeMake(2, 1);//设置阴影的偏移方向,x:左右,负左正右;y:上下,负上正下
shadow.shadowBlurRadius = 5;

————————————————
版权声明:本文为CSDN博主「hell03W」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weidfyr/article/details/48316775

原文地址:https://www.cnblogs.com/itlover2013/p/14837011.html