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

本文详细介绍如何在iOS应用中使用NSMutableAttributedString实现富文本编辑,包括设置字体、颜色、背景等属性,以及如何利用NSMutableParagraphStyle调整段落样式。通过实例演示了不同属性的设置方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

2015年09月09日 15:32:18 hell03W 阅读数:4728 标签: ios开发富文本 更多

个人分类: IOS UI控件IOS 开发

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.youkuaiyun.com/walden00/article/details/48316775

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

1)第一种方式

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

2)第二种方式

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

3)使用步骤,Demo:

 

 
  1. //1,建立一个字符串

  2. NSString *str = @"在北京时间3 月 10 日凌晨的苹果发布活动上,HBO 宣布推出与苹果独家合作的 HBO Now 服务,并播出了万众期待的第五季《权力的游戏》预告片。\n2015年3月11日,HBO 正式宣布了《权力的游戏》第五季将在全球同步播出的决定,通过 HBO 旗下的全球各个电视网播出,该剧首播将在美国东部时间 4 月 12 日晚 9 点(北京时间 4 月13 日早 9 点)进行,包括 HBO 亚洲,HBO 加拿大,HBO 欧洲,HBO 拉美,HBO 荷兰,HBO 北欧各个电视网将于同一时间播出。";

  3. //2,定义字符串的属性字典

  4. NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];

  5. paragraph.lineSpacing = 10; //设置行间距

  6. NSDictionary *attributeDict = @{

  7. NSFontAttributeName: [UIFont systemFontOfSize:12],

  8. NSForegroundColorAttributeName: [UIColor blackColor],

  9. NSKernAttributeName: @2, NSParagraphStyleAttributeName: paragraph};

  10. //3,根据属性字典和字符串,计算字符串使用指定的属性设置时候,占用空间的大小,返回CGRect类型

  11. CGSize contentSize = [str boundingRectWithSize:CGSizeMake(self.view.frame.size.width-40, MAXFLOAT)

  12. options:NSStringDrawingUsesLineFragmentOrigin

  13. attributes:attributeDict

  14. context:nil].size;//计算文字大小

  15.  
  16. //4,构建带格式的文本字符串

  17. NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:str attributes:attributeDict];

  18. //5,构建label

  19. _textLabel = [[UILabel alloc] init];

  20. _textLabel.frame = CGRectMake(20, 20, contentSize.width, contentSize.height);

  21. _textLabel.numberOfLines = 0; //设置分行显示,必须必须设置这个属性

  22. //6,将格式化的文本添加到label上。

  23. _textLabel.attributedText = attributeStr;

 

 

4)NSMutableAttributedString 可以设置的属性

 

 

 
  1. // NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12

  2. // NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色

  3. // NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色

  4. // NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符

  5. // NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄

  6. // NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)

  7. // NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色

  8. // NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似

  9. // NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色

  10. // NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果

  11. // NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象

  12. // NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象

  13. // NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:

  14. // NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏

  15. // NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾

  16. // NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本

  17. // NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写

  18. // NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本

  19. // NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址

  20. // NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排

  21. // NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象

 

5)NSMutableAttributedString设置文字属性,Demo

 

 

 
  1. NSMutableAttributedString *testStr = [[NSMutableAttributedString alloc] initWithString:str];

  2. //1,设置指定范围内的 字体

  3. [testStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(5, 30)];

  4. //2,设置指定范围内 字体颜色

  5. [testStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 10)];

  6. //3,设置 字体所在区域的背景颜色

  7. [testStr addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(40, 30)];

  8. //4,设置连体属性,取值为NSNumber 对象(整数)

  9. [testStr addAttribute:NSLigatureAttributeName value:@1 range:NSMakeRange(40, 30)];

  10. //5,设置指定范围内 字符间距

  11. [testStr addAttribute:NSKernAttributeName value:@10 range:NSMakeRange(70, 30)];

  12. //6,设置删除线

  13. [testStr addAttribute:NSStrikethroughStyleAttributeName value:@1 range:NSMakeRange(120, 20)];

  14. //7,设置删除线的颜色

  15. [testStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(120, 10)];

  16. //8,设置下划线

  17. [testStr addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSMakeRange(140, 20)];

  18. //9,设置下划线颜色

  19. [testStr addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor] range:NSMakeRange(140, 10)];

  20. //10,设置字体倾斜度,负值向左,正值向右

  21. [testStr addAttribute:NSObliquenessAttributeName value:@-0.5 range:NSMakeRange(160, 30)];

  22. //11,设置字体拉伸压缩,正值拉伸,负值压缩

  23. [testStr addAttribute:NSExpansionAttributeName value:@-0.5 range:NSMakeRange(190, 20)];

  24. // 设置文字书写方向,从左向右书写或者从右向左书写

  25. // [testStr addAttribute:NSWritingDirectionAttributeName value:@1 range:NSMakeRange(220, 20)];

  26. //12,设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本

  27. [testStr addAttribute:NSVerticalGlyphFormAttributeName value:@1 range:NSMakeRange(0, testStr.length)];

  28. //13,设置链接属性,点击后调用浏览器打开指定URL地址

  29. [testStr addAttribute:NSLinkAttributeName value:@"http://what-forever.com" range:NSMakeRange(220, 20)];

  30. //14,设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果

  31. [testStr addAttribute:NSStrokeWidthAttributeName value:@0.5 range:NSMakeRange(240, 30)];

  32. //15,填充部分颜色,不是字体颜色,取值为 UIColor 对象

  33. [testStr addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:NSMakeRange(240, 30)];

  34. //16,设置阴影属性,取值为 NSShadow 对象

  35. [testStr addAttribute:NSShadowAttributeName value:[[NSShadow alloc] init] range:NSMakeRange(270, 30)];

  36. //17,设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏

  37. [testStr addAttribute:NSBaselineOffsetAttributeName value:@10 range:NSMakeRange(300, 30)];

  38. //18,设置文本段落排版格式,取值为 NSParagraphStyle 对象

  39. [testStr addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(70, 30)];

 

效果:

2,NSMutableParagraphStyle,继承自NSParagraphStyle

1)可以设置的属性:

 

 
  1. 1 @property(readwrite) CGFloat lineSpacing;              //行间距

  2. 2 @property(readwrite) CGFloat paragraphSpacing;           //段间距

  3. 3 @property(readwrite) NSTextAlignment alignment;           //对齐方式

  4. 4 @property(readwrite) CGFloat firstLineHeadIndent;          //首行缩紧

  5. 5 @property(readwrite) CGFloat headIndent;               //除首行之外其他行缩进

  6. 6 @property(readwrite) CGFloat tailIndent;               //每行容纳字符的宽度

  7. 7 @property(readwrite) NSLineBreakMode lineBreakMode;        //换行方式

  8. 8 @property(readwrite) CGFloat minimumLineHeight;           //最小行高

  9. 9 @property(readwrite) CGFloat maximumLineHeight;           //最大行高

  10. 10 @property(readwrite) NSWritingDirection baseWritingDirection;  //书写方式(NSWritingDirectionNatural,NSWritingDirectionLeftToRight,NSWritingDirectionRightToLeft)

  11. 11 @property(readwrite) CGFloat lineHeightMultiple;

  12. 12 @property(readwrite) CGFloat paragraphSpacingBefore;

  13. 13 @property(readwrite) float hyphenationFactor;

  14. 14 @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);

  15. 15 @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);

 

2)Demo

 
  1. NSMutableParagraphStyle *paragra = [[NSMutableParagraphStyle alloc] init];

  2. paragra.lineSpacing = 5;//1,设置行间距

  3. paragra.paragraphSpacing = 10; //2,设置段间距

  4. paragra.alignment = UITextAlignmentLeft;//3,设置对齐方式

  5. paragra.firstLineHeadIndent = 50;//4,首行缩进距离

  6. paragra.headIndent = 10;//5,除首行之外其他行缩进

  7. paragra.tailIndent = 300;//6,每行容纳字符的宽度

  8. paragra.minimumLineHeight = 2;//7,每行最小高度

  9. paragra.maximumLineHeight = 10;//8,每行最大高度

  10. paragra.lineBreakMode = NSLineBreakByCharWrapping;//9,换行方式

  11. //lineBreakMode 属性的可选项

  12. {

  13. // NSLineBreakByWordWrapping = 0, // Wrap at word boundaries, default */

  14. // NSLineBreakByCharWrapping, /* Wrap at character boundaries */

  15. // NSLineBreakByClipping, /* Simply clip */

  16. // NSLineBreakByTruncatingHead, /* Truncate at head of line: "...wxyz" */

  17. // NSLineBreakByTruncatingTail, /* Truncate at tail of line: "abcd..." */

  18. // NSLineBreakByTruncatingMiddle /* Truncate middle of line: "ab...yz" */

  19. }

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


3,NSShadow用法    

 

 

 
  1. NSShadow *shadow = [[NSShadow alloc] init];

  2. shadow.shadowColor = [UIColor redColor]; //设置阴影的颜色

  3. shadow.shadowOffset = CGSizeMake(2, 1);//设置阴影的偏移方向,x:左右,负左正右;y:上下,负上正下

  4. shadow.shadowBlurRadius = 5;


 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值