文本常用Tips

很久没有写了,可能接下来会整理一下开发常用小Tips,看看苹果文档(没错我就是懒!),
今天就先说说文本开发中常用的一些知识点。

常用编辑文本属性的类

NSAttributedString

  • 设置字体大小
    NSFontAttributeName : [UIFont systemFontOfSize:16]
  • 设置字间距
    NSKernAttributeName : @2.0f
  • 设置字体颜色
    NSForegroundColorAttributeName : [UIColor yellowColor]
  • 设置段落样式(下文详解
    NSParagraphStyleAttributeName
  • 设置背景颜色
    NSBackgroundColorAttributeName : [UIColor yellowColor]
  • 设置空心(两个属性同时设置才有效果)
    NSStrokeColorAttributeName : [UIColor yellowColor] //描边颜色
    NSStrokeWidthAttributeName : @3.0f //描边宽度(默认为 0,即不改变,大于0改变描边宽度,小于0同时改变文字的描边和填充宽度)
  • 设置删除线和下划线
    NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle)
    NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)
  • 设置阴影
    NSShadowAttributeName : [[NSShadow alloc]init] (此处是强调传入阴影对象,需要设置阴影样式,且阴影需要配合以下三个属性其中之一才能正常使用)
  • 设置竖排文本
    NSVerticalGlyphFormAttributeName : @(0)
  • 设置字体倾斜
    NSObliquenessAttributeName : @1
  • 设置文本扁平化
    NSExpansionAttributeName : @1

使用示例:

///设置tabBar字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];`

NSMutableParagraphStyle

Important

A paragraph style object should not be mutated after adding it to an attributed string; doing so can cause your program to crash.
苹果文档里有一个重要提示:ParagraphStyle赋值到AttributedString之后就不能变动了,不然会导致崩溃!这是使用过程中需要注意的!

使用示例:

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.alignment = NSTextAlignmentLeft;              //传说中的对齐方式 -> 左
paraStyle.firstLineHeadIndent = 0.0;                    //传说中的首行头部缩进
paraStyle.headIndent = 0;                               //传说中的除了首行的头部缩进
paraStyle.tailIndent = 0;                               //传说中的尾部缩进
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;    //传说中的断句模式
paraStyle.maximumLineHeight                             //传说中的最大行高,同理minimumLineHeight就是最小行高
paraStyle.lineSpacing = 8;                              //传说中的行间距
paraStyle.paragraphSpacing = 0.0;                       //传说中的段落间距
paraStyle.paragraphSpacingBefore = 0.0;                 //传说中的段落顶部和文本内容的间距
paraStyle.hyphenationFactor = 1.0;                      //传说中的断词功能,取值0~1
paraStyle.baseWritingDirection                          //传说中的书写方向,左到右、右到左、双向
paraStyle.lineHeightMultiple                            //传说中的行高乘数,改变行高不改变字体大小

//设置字间距 NSKernAttributeName:@1.5f
NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:16],NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                      };

NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:yourOwnString];
self.topicContentLabel.attributedText = attributeStr;

如果还满足不了需求?

毕竟需求是无止境的,比如分段属性,就添加一个范围

//创建可变的文本属性
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: yourOwnString];
//给所有字符设置字体为Zapfino,字体高度为15像素
[attributedString addAttribute: NSFontAttributeName value: [UIFont fontWithName: @"Zapfino" size: 16] range: NSMakeRange(0, yourOwnString.length)];
//分段属性:前1个字符颜色设置成蓝色
[attributedString addAttribute: NSForegroundColorAttributeName value: [UIColor blueColor] range: NSMakeRange(0, 1)];
//分段属性:第2个字符开始的3个字符设置为红色
[attributedString addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(1, 3)];

最后还没办法的话,就去进修两个框架TextKitCoreText吧,听说很厉(难)害(学)。

你可能感兴趣的:(文本常用Tips)