iOS开发-UITextView文字排版
UITextView文本排版
1.配置NSMutableParagraphStyle
NSMutableParagraphStyle *MParaStyle = [[NSMutableParagraphStyle alloc] init];
MParaStyle.alignment = NSTextAlignmentNatural; // 文字站位
MParaStyle.maximumLineHeight = 20; // 最大高度
MParaStyle.lineHeightMultiple = 10 ; // 平均高度
MParaStyle.minimumLineHeight = 0; // 最小高度
MParaStyle.firstLineHeadIndent = 20; // 首行缩进
MParaStyle.lineSpacing = 0; // 行间距
MParaStyle.headIndent = 20; // 左侧整体缩进
MParaStyle.tailIndent = SCREEN_WIDTH - 20; // 右侧整体缩进
MParaStyle.lineBreakMode = NSLineBreakByCharWrapping; // 内容省略方式
MParaStyle.baseWritingDirection = NSWritingDirectionLeftToRight; // 书写方式
MParaStyle.paragraphSpacingBefore = 0; // 段落之间间距
MParaStyle.paragraphSpacing = 0; // 段落间距离
// MParaStyle.hyphenationFactor = 1; // 连字属性
2.配置attibutes
NSMutableDictionary *attributes = [NSMutableDictionary new];
// 添加paragraphStyle
[attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
// 添加font
[attributes setObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
3.关联text
NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"test" attributes:attributes];
UITextView *textView = [[UITextView alloc] init];
[textView setAttributedText:text];
最近项目中对图文混排有一定的需求,例如价格,文字链接,文字颜色变化等要求,翻
了很多资料,咱们对这些属性做了如下的总结,希望能在方便自己查阅!
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
NSFontAttributeName 设置字体大小和字体的类型 默认12 Helvetica(Neue)NSForegroundColorAttributeName 设置字体颜色,默认黑色 UIColor对象NSBackgroundColorAttributeName 设置字体所在区域的背景颜色,默认为nil,透明色NSLigatureAttributeName 设置连体属性,NSNumber对象 默认0 没有连体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对象 |
我去,大兄弟,有点多啊,各位大爷不要慌,听小弟给你们一一
道来,每个属性一行,分分钟教大家简单学习富文本


OK,咱们现在开始一个一个絮叨
1.NSFontAttributeName
说明:该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)

2.NSForegroundColorAttributeName
说明:该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色
3.NSBackgroundColorAttributeName
说明:设置文字背景颜色
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
NSString *string = @"落霞与孤鹜齐飞,秋水共长天一色"; NSMutableAttributedString *mutableAttriteStr = [[NSMutableAttributedString alloc] init]; NSAttributedString *attributeStr1 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(0, 3)] attributes:@{NSFontAttributeName :[UIFont fontWithName:@"futura" size:12],NSForegroundColorAttributeName : [UIColor redColor],NSBackgroundColorAttributeName : [UIColor yellowColor]}]; NSAttributedString *attributeStr4 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(3, 4)] attributes:@{NSFontAttributeName :[UIFont fontWithName:@"futura" size:22],NSForegroundColorAttributeName : [UIColor redColor],NSBackgroundColorAttributeName : [UIColor yellowColor]}]; NSAttributedString *attributeStr2 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(8, 4)] attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:22],NSForegroundColorAttributeName : [UIColor blackColor],NSBackgroundColorAttributeName : [UIColor lightGrayColor]}]; NSAttributedString *attributeStr5 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(12, 3)] attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName : [UIColor blackColor],NSBackgroundColorAttributeName : [UIColor lightGrayColor]}]; NSAttributedString *attriteStr3 = [[NSAttributedString alloc] initWithString:[string substringWithRange:NSMakeRange(7, 1)] attributes:@{NSBackgroundColorAttributeName : [UIColor greenColor]}]; [mutableAttriteStr appendAttributedString:attributeStr1]; [mutableAttriteStr appendAttributedString:attributeStr4]; [mutableAttriteStr appendAttributedString:attriteStr3]; [mutableAttriteStr appendAttributedString:attributeStr2]; [mutableAttriteStr appendAttributedString:attributeStr5]; self.label1.attributedText = mutableAttriteStr; |
4.NSLigatureAttributeName
连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1
表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值
为 2)

|
1
2
3
4
5
6
7
8
9
10
11
|
// 连体艺术字,不是每个都能连起的,f和l f和i就可以,其他各位可以自己去试试 self.label2.attributedText = [[NSAttributedString alloc] initWithString:@"flush and fily" attributes:@{NSLigatureAttributeName : [NSNumber numberWithInt:1],NSFontAttributeName : [UIFont fontWithName:@"futura" size:30]}]; NSMutableAttributedString *mutableAttributeStr2 = [[NSMutableAttributedString alloc] init]; NSAttributedString *string6 = [[NSAttributedString alloc] initWithString:@"ABCDE " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:-3],NSForegroundColorAttributeName : [UIColor redColor]}]; NSAttributedString *string7 = [[NSAttributedString alloc] initWithString:@"FGHIJ " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:0],NSForegroundColorAttributeName : [UIColor yellowColor]}]; NSAttributedString *string8 = [[NSAttributedString alloc] initWithString:@"KLMNO " attributes:@{NSKernAttributeName : @(15),NSForegroundColorAttributeName : [UIColor blueColor]}]; [mutableAttributeStr2 appendAttributedString:string6]; [mutableAttributeStr2 appendAttributedString:string7]; [mutableAttributeStr2 appendAttributedString:string8]; self.label3.attributedText = mutableAttributeStr2; |
5.NSKernAttributeName
字符间距正值间距加宽,负值间距变窄

|
1
2
3
4
5
6
7
8
|
NSMutableAttributedString *mutableAttributeStr2 = [[NSMutableAttributedString alloc] init]; NSAttributedString *string6 = [[NSAttributedString alloc] initWithString:@"ABCDE " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:-3],NSForegroundColorAttributeName : [UIColor redColor]}]; NSAttributedString *string7 = [[NSAttributedString alloc] initWithString:@"FGHIJ " attributes:@{NSKernAttributeName : [NSNumber numberWithInt:0],NSForegroundColorAttributeName : [UIColor yellowColor]}]; NSAttributedString *string8 = [[NSAttributedString alloc] initWithString:@"KLMNO " attributes:@{NSKernAttributeName : @(15),NSForegroundColorAttributeName : [UIColor blueColor]}]; [mutableAttributeStr2 appendAttributedString:string6]; [mutableAttributeStr2 appendAttributedString:string7]; [mutableAttributeStr2 appendAttributedString:string8]; self.label3.attributedText = mutableAttributeStr2; |
6.NSStrikethroughStyleAttributeName 和 NSStrikethroughColorAttributeName
NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数),
NSStrikethroughColorAttributeName 设置删除线颜色
枚举常量 NSUnderlineStyle中的值:
NSUnderlineStyleNone =0x00, 不设置
NSUnderlineStyleSingle =0x01, 设置单细删除线
NSUnderlineStyleThickNS_ENUM_AVAILABLE(10_0,7_0) = 0x02, 设置粗单删除线
NSUnderlineStyleDoubleNS_ENUM_AVAILABLE(10_0,7_0) = 0x09,双细删除线

|
1
2
3
4
5
6
7
8
9
|
NSMutableAttributedString *mutableAttributeStr3 = [[NSMutableAttributedString alloc] init]; NSAttributedString *string9 = [[NSAttributedString alloc] initWithString:@"只要998 " attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle),NSStrikethroughColorAttributeName : [UIColor redColor]}]; NSAttributedString *string10 = [[NSAttributedString alloc] initWithString:@"真的998 " attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleThick),NSStrikethroughColorAttributeName : [UIColor blueColor]}]; NSAttributedString *string11 = [[NSAttributedString alloc] initWithString:@"好吧9块拿走" attributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleDouble),NSStrikethroughColorAttributeName : [UIColor yellowColor]}]; [mutableAttributeStr3 appendAttributedString:string9]; [mutableAttributeStr3 appendAttributedString:string10]; [mutableAttributeStr3 appendAttributedString:string11]; self.label4.attributedText = mutableAttributeStr3; |
7.NSUnderlineStyleAttributeName 和
NSUnderlineColorAttributeName
给文字加下划线和更换下划线颜色,属性和上面的删除线都是一样用的

|
1
2
3
4
5
6
7
8
9
|
NSMutableAttributedString *mutableAttributeStr4 = [[NSMutableAttributedString alloc] init]; NSAttributedString *string12 = [[NSAttributedString alloc] initWithString:@"只要888 " attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle),NSUnderlineColorAttributeName : [UIColor redColor]}]; NSAttributedString *string13 = [[NSAttributedString alloc] initWithString:@"真的88 " attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleThick),NSUnderlineColorAttributeName : [UIColor purpleColor]}]; NSAttributedString *string14 = [[NSAttributedString alloc] initWithString:@"好吧8块拿走" attributes:@{NSUnderlineStyleAttributeName : @(NSUnderlineStyleDouble),NSUnderlineColorAttributeName : [UIColor yellowColor]}]; [mutableAttributeStr4 appendAttributedString:string12]; [mutableAttributeStr4 appendAttributedString:string13]; [mutableAttributeStr4 appendAttributedString:string14]; self.label5.attributedText = mutableAttributeStr4; |
8.NSStrokeWidthAttributeName 和NSStrokeColorAttributeName
设置文字描边颜色,需要和NSStrokeWidthAttributeName设置描边宽度,这样就能使文字空心.
NSStrokeWidthAttributeName 这个属性所对应的值是一个 NSNumber 对象(小数)。该值改变笔画宽度(相对于字体 size 的百分比),负值填充效果,正值中空效果,默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为 3.0。
同时设置了空心的两个属性,并且 NSStrokeWidthAttributeName 属性设置为整数,文字前景色就无效果了

|
1
2
3
4
5
6
7
8
9
10
|
NSMutableAttributedString *mutableAttributeStr5 = [[NSMutableAttributedString alloc] init]; // 如果这个stroke的width正数的时候,字体就中空了,那么前景色就设置无效了 NSAttributedString *string15 = [[NSAttributedString alloc] initWithString:@"HELLO " attributes:@{NSStrokeWidthAttributeName : @(5),NSStrokeColorAttributeName : [UIColor yellowColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor]}]; NSAttributedString *string16 = [[NSAttributedString alloc] initWithString:@"YUKD " attributes:@{NSStrokeWidthAttributeName : @(0),NSStrokeColorAttributeName : [UIColor redColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:30]}]; NSAttributedString *string17 = [[NSAttributedString alloc] initWithString:@"GOOGLE" attributes:@{NSStrokeWidthAttributeName : @(-5),NSStrokeColorAttributeName : [UIColor greenColor],NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor lightGrayColor]}]; [mutableAttributeStr5 appendAttributedString:string15]; [mutableAttributeStr5 appendAttributedString:string16]; [mutableAttributeStr5 appendAttributedString:string17]; self.label6.attributedText = mutableAttributeStr5; |
9.NSShadowAttributeName
设置文字阴影,取值为NSShadow对象

|
1
2
3
4
5
6
7
8
|
// 设置阴影 NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowBlurRadius = 5.0f; // 模糊度 shadow.shadowColor = [UIColor blueColor]; shadow.shadowOffset = CGSizeMake(1, 5); NSAttributedString *string20 = [[NSAttributedString alloc] initWithString:@"HELLO_HALY_璟" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow}]; self.label7.attributedText = string20; |
10.NSTextEffectAttributeNam
NSTextEffectAttributeName //设置文本特殊效果,取值为NSString类型,目前只有一个可用效果 NSTextEffectLetterpressStyle(凸版印刷效果)

|
1
2
3
4
5
|
// 文本印刷,我也不知道是什么鬼 // 设置阴影 NSAttributedString *string21 = [[NSAttributedString alloc] initWithString:@"HELLO_HALY_璟" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow,NSTextEffectAttributeName : NSTextEffectLetterpressStyle}]; self.label8.attributedText = string21; |
11.NSBaselineOffsetAttributeName
文字基线偏移,想要达到以下图片的效果,就要设置需要的文字偏移,正数上移,负数下
移

|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
// 设置文本的基线 负数向下 正数向上 0不变 UIFont *bigFont = [UIFont boldSystemFontOfSize:36]; UIFont *smallFont = [UIFont boldSystemFontOfSize:bigFont.pointSize / 2]; CGFloat capHeight = bigFont.pointSize - smallFont.pointSize; NSMutableAttributedString *attributeString6 = [[NSMutableAttributedString alloc] init]; NSAttributedString *string22 = [[NSAttributedString alloc] initWithString:@"¥" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}]; NSAttributedString *string23 = [[NSAttributedString alloc] initWithString:@"9" attributes:@{NSFontAttributeName : bigFont,NSForegroundColorAttributeName : [UIColor blueColor],NSBaselineOffsetAttributeName : @(0)}]; NSAttributedString *string24 = [[NSAttributedString alloc] initWithString:@".99" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}]; NSAttributedString *string28 = [[NSAttributedString alloc] initWithString:@" 七夕大促销 " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:24],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0),NSShadowAttributeName : shadow}]; NSAttributedString *string25 = [[NSAttributedString alloc] initWithString:@"¥" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(capHeight - 5)}]; NSAttributedString *string26 = [[NSAttributedString alloc] initWithString:@"0" attributes:@{NSFontAttributeName : bigFont,NSForegroundColorAttributeName : [UIColor redColor],NSBaselineOffsetAttributeName : @(0)}]; NSAttributedString *string27 = [[NSAttributedString alloc] initWithString:@".09" attributes:@{NSFontAttributeName : smallFont,NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(capHeight - 5)}]; [attributeString6 appendAttributedString:string22]; [attributeString6 appendAttributedString:string23]; [attributeString6 appendAttributedString:string24]; [attributeString6 appendAttributedString:string28]; [attributeString6 appendAttributedString:string25]; [attributeString6 appendAttributedString:string26]; [attributeString6 appendAttributedString:string27]; self.label9.attributedText = attributeString6; NSMutableAttributedString *attributeString7 = [[NSMutableAttributedString alloc] init]; NSAttributedString *string29 = [[NSAttributedString alloc] initWithString:@"Hello " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:40],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}]; NSAttributedString *string30 = [[NSAttributedString alloc] initWithString:@" World " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}]; NSAttributedString *string31 = [[NSAttributedString alloc] initWithString:@"Hello " attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:40],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(0)}]; NSAttributedString *string32 = [[NSAttributedString alloc] initWithString:@" World" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName : [UIColor blackColor],NSBaselineOffsetAttributeName : @(6)}]; [attributeString7 appendAttributedString:string29]; [attributeString7 appendAttributedString:string30]; [attributeString7 appendAttributedString:string31]; [attributeString7 appendAttributedString:string32]; self.label10.attributedText = attributeString7; |
12.NSObliquenessAttributeName
NSObliquenessAttributeName 设置字体倾斜度,取值为 NSNumber(float),正值右倾,负值左倾
13.NSExpansionAttributeName
NSExpansionAttributeName 设置字体的横向拉伸,取值为NSNumber (float),正值拉伸 ,负值压缩
14.NSVerticalGlyphFormAttributeName
NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为NSNumber对象(整数),0表示横排文本,1表示竖排文本 在iOS中只支持0

|
1
2
3
4
5
6
7
8
|
// 说明:NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为NSNumber对象(整数),0表示横排文本,1表示竖排文本 在iOS中只支持0//说明:NSObliquenessAttributeName 设置字体倾斜度,取值为 NSNumber(float),正值右倾,负值左倾// NSExpansionAttributeName// 说明:NSExpansionAttributeName 设置字体的横向拉伸,取值为NSNumber (float),正值拉伸 ,负值压缩 NSMutableAttributedString *mutableAttriteStr = [[NSMutableAttributedString alloc] init]; NSAttributedString *attributeStr1 = [[NSAttributedString alloc] initWithString:@"HeHe_XiXi_mm" attributes:@{NSFontAttributeName :[UIFont boldSystemFontOfSize:30],NSForegroundColorAttributeName : [UIColor redColor],NSShadowAttributeName : shadow,NSObliquenessAttributeName : @(1),NSExpansionAttributeName : @(1),NSVerticalGlyphFormAttributeName : @(1)}]; [mutableAttriteStr appendAttributedString:attributeStr1]; self.label1.attributedText = mutableAttriteStr; |
15.NSWritingDirectionAttributeName
文字书写方向取值为以下组合为例 他是一个数组包含NSNumber 一般书写就是L ---R
那么我们多个需求也就是从R --- L
The values of theNSNumberobjects should be0,1,2, or3, forLRE,RLE,LRO, orRLOrespectively, and combinations ofleftToRightandrightToLeftwithNSTextWritingDirectionEmbeddingorNSTextWritingDirectionOverride

|
1
2
3
4
5
6
7
8
9
10
|
<code><code><code> // 文字书写方向// The values of the NSNumber objects should be 0, 1, 2, or 3, for LRE, RLE, LRO, or RLO respectively, and combinations of leftToRight and rightToLeft with NSTextWritingDirectionEmbedding or NSTextWritingDirectionOverride, as shown in Table 1.// NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding// NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding// NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride// NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride NSMutableAttributedString *attributuStr2 = [[NSMutableAttributedString alloc] init]; NSAttributedString *string1 = [[NSAttributedString alloc] initWithString:@"一切皆有可能" attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:24],NSForegroundColorAttributeName : [UIColor redColor],NSWritingDirectionAttributeName : @[@(3)]}]; [attributuStr2 appendAttributedString:string1]; self.label.attributedText = attributuStr2;</code></code></code> |
16.NSLinkAttributeName
这货有点奇葩,所以很多人用第三方例如YYLabel来做,这东西不能在UILabel和UITextField使用,只能用UITextView来进行,实现他的代理,在代理方法里面进行URL跳转
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
该方法返回YES就能打开URL,NO不做任何事情
注:
1.一定要实现UITextView的代理才能进行URL跳转
2.textView的editable属性修改为NO,在编辑时不可点击

|
1
2
3
4
5
6
7
8
9
10
11
|
<code><code><code>// 把 NSLinkAttributeName 属性单独列出来,是因为在 UILabel 和 UITextField 中是无法使用该属性的。更准确点说是在UILabel 和 UITextField 中无法实现点击链接启动浏览器打开一个URL地址,因为在此过程中用到了一个代理函数。只能用在 UITextView 中 self.textView.delegate = self; self.textView.scrollEnabled = NO; self.textView.editable = NO; self.textView.textContainer.lineFragmentPadding = 0; self.textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0); NSString *str = @" 跳转到宓珂璟的博客"; NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str]; [attrStr addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://blog.csdn.net/deft_mkjing"] range:[str rangeOfString:@"宓珂璟的博客"]]; [attrStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:30] range:NSMakeRange(0, str.length)]; self.textView.attributedText = attrStr;</code></code></code> |
17.NSTextAttachment
设置文本附件,取值为NSTextAttachment对象 常用于图文混排
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<code><code><code> NSString *words = @"天才"; NSMutableAttributedString *strAtt = [[NSMutableAttributedString alloc] initWithString:words attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:25]}]; NSTextAttachment *attatch = [[NSTextAttachment alloc] initWithData:nil ofType:nil]; attatch.bounds = CGRectMake(0, 0, 40, 30); attatch.image = [UIImage imageNamed:@"$DCZ2WLN9RWI6JF(Q`P_(NH.jpg"]; NSTextAttachment *attatch1 = [[NSTextAttachment alloc] initWithData:nil ofType:nil]; attatch1.bounds = CGRectMake(0, 0, 50, 30); attatch1.image = [UIImage imageNamed:@"%5T@J(4WKWSOX2~~PY0{4M0.jpg"]; NSAttributedString *string8 = [NSAttributedString attributedStringWithAttachment:attatch]; NSAttributedString *string9 = [NSAttributedString attributedStringWithAttachment:attatch1]; [strAtt insertAttributedString:string8 atIndex:1]; [strAtt insertAttributedString:string9 atIndex:0]; self.label3.attributedText = strAtt;</code></code></code> |
基本的就介绍到这里了,上面那些已经足够用了,而且可以任意组合出想要的图文混排,
如果遇到特殊的需求或者不能满足的,那么就需要各位去试试往上的第三方富文本了
给个好用的大家可以试试点击打开链接
本次Demo地址:Demo传送门
iOS开发-UITextView文字排版的更多相关文章
- iOS开发-UITextView实现PlaceHolder的方式
之前开发遇到过UITextField中加入一个PlaceHolder的问题,直接设置一下即可,不过这次是需要在UITextView中实现一个PlaceHolder,跟之前有点不同.在网上参考了各位前辈 ...
- iOS开发-UITextView根据内容自适应高度
UITextView作为内容文本输入区域,有的时候我们需要根据内容动态改变文本区域的高度,效果如下: 定义UITextView,实现UITextViewDelegate: -(UITextView * ...
- 《iOS开发指南:从零基础到App Store上架(第2版)》
<iOS开发指南:从零基础到App Store上架(第2版)> 基本信息 作者: 关东升 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115348029 上架时间:201 ...
- iOS开发中设置UITextField的占位文字的颜色,和光标的颜色
在iOS开发中,对于很多初学者而言,很有可能碰到需要修改UITextField的占位文字的颜色,以及当UITextField成为第一响应者后光标的颜色,那么下面小编就介绍一下修改占位文字和光标的颜色. ...
- iOS开发——UI篇&文字渐变效果:图层中的mask属性
文字渐变效果:图层中的mask属性 本次文章,主要讲述的是图层中的mask属性,利用它,可以做出文字渐变效果! 一.文字渐变效果: 二.文字渐变实现思路: 1.创建一个颜色渐变层,渐变图层跟文字控件一 ...
- [转] iOS文字排版(CoreText)那些事儿
文章转载自 http://www.cocoachina.com/applenews/devnews/2014/0521/8504.html iOS文字排版(CoreText)那些事儿 转自阿毛的蛋疼地 ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- iOS:iOS开发非常全的三方库、插件等等
iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...
- iOS开发--iOS及Mac开源项目和学习资料
文/零距离仰望星空(简书作者)原文链接:http://www.jianshu.com/p/f6cdbc8192ba著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文出处:codecl ...
随机推荐
- MySQL_ERROR 1231 (42000) at line XX in file 'file_name' Variable 'time_zone' can't be
类似的错误信息如下 ERROR 1231 (42000) at line 10370 in file: '/root/sql/ultrax_170322.dmp': Variable 'time_zo ...
- iOS开发值得学习的Demo
一.HXWeiboPhotoPicker - 仿微博照片选择器 GitHub地址:https://github.com/LoveZYForever/HXWeiboPhotoPicker 二.AFNet ...
- Win7旗舰版中的IIS配置asp.net 完美通过版,附代码 以及出现的 CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\8d57d
先解决问题:“c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\8d57d 图: 其他的解决方案 ...
- Win10m的前景到底在何方?
今天晚上就是build2016的微软开发者大会了,满怀着期待. 本人一直是一名微软的粉丝,我年纪小,刚开始接触电脑的时候是win98,那时候也没怎么玩过电脑,到后来经常接触电脑的时候,所有的电脑都是w ...
- C# 随机数生成避免重复
public string GetMsgID() { Random rand = new Random((int)DateTime.Now.Ticks); string szRand = rand.N ...
- python中的参数传递
一般的参数顺序是先位置,再关键字,然后是包裹位置传递,包裹关键字传递.
- 在.Net中进行SQL Server数据库备份与还原操作实用类
#region 类说明 //----------------------------------------------------------------------------- // // 项目 ...
- 锁(3)-- DB锁
1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据库执行请求,T2代表另一个请求,也可以理解为T1为一 ...
- SPA(单页面web应用程序)
单页web应用(single page web application,SPA),就是只有一张web页面的应用,是加载单个HTML页面并在用户与应用程序交互时动态更新该页面的web应用程序. 浏览器一 ...
- java poi分批次导入Excel
最近换了新工作,公司要求导入Excel要分批次导入,并且是多线程的情况下执行导入,查了很多资料,没看到比较复合的,就打算自己写一个吧,可能有不足,希望指出. 上面说到多线程,这边就不贴出代码了,具体思 ...