NSMutableAttributedString使用

来源:互联网 发布:vmware 共享 linux 编辑:程序博客网 时间:2024/04/25 18:14
 
//    // 1
//    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:str];
//    [attributed addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Zapfino" size:20] range:NSMakeRange(0, str.length)];
//    [attributed addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, 2)];
//    label.attributedText = attributed;
//   
//    // 2
//    NSDictionary *attrDict = @{NSFontAttributeName:[UIFont fontWithName: @"Zapfino" size: 15],
//                                NSForegroundColorAttributeName:[UIColor blueColor]};
//   
//    NSMutableAttributedString *attributed2 = [[NSMutableAttributedString alloc] initWithString:str attributes:attrDict];
//    label.attributedText = attributed2;
//   
//    //打印系统中所有字体的类型名字
//    NSArray *familyNames = [UIFont familyNames];
//    for(NSString *familyName in familyNames ){
//        printf("Family: %s \n", [familyName UTF8String]);
//        NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
//        for(NSString *fontName in fontNames) {
//            printf("\tFont: %s \n", [fontName UTF8String]);
//  
//        }
//    }
   
   
   
//方式二的分段处理
   
//第一段
   
//    NSString *originStr = @"家里人今天的上课了国家的路口时根据法律的空格键";
//   
//    NSDictionary *attrDict1 = @{NSFontAttributeName:[UIFont fontWithName: @"Zapfino" size: 15],
//                                 NSForegroundColorAttributeName:[UIColor blueColor] };
//    NSAttributedString *attrStr1 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange: NSMakeRange(0, 4)] attributes: attrDict1];
//   
//    //第二段
//    NSDictionary *attrDict2 = @{NSFontAttributeName:[UIFont fontWithName: @"Zapfino" size: 15],
//                                 NSForegroundColorAttributeName:[UIColor redColor] };
//    NSAttributedString *attrStr2 = [[NSAttributedString alloc] initWithString: [originStr substringWithRange: NSMakeRange(4, 3)] attributes: attrDict2];
//   
//    //第三段
//    NSDictionary *attrDict3 = @{NSFontAttributeName:[UIFont fontWithName: @"Zapfino" size: 15],
//                                 NSForegroundColorAttributeName:[UIColor blackColor] };
//    NSAttributedString *attrStr3 = [[NSAttributedString alloc] initWithString:[originStr substringWithRange:NSMakeRange(7, originStr.length - 4 - 3)] attributes: attrDict3];
//    //合并
//    NSMutableAttributedString *attributedStr03 = [[NSMutableAttributedString alloc] initWithAttributedString: attrStr1];
//    [attributedStr03 appendAttributedString: attrStr2];
//    [attributedStr03 appendAttributedString: attrStr3];
//   
//    label.attributedText = attributedStr03;
   
   
  
// 举例说明:
   
//    1. NSFontAttributeName
   
   
   
//NSForegroundColorAttributeName 设置字体颜色,取值为 UIColor,默认为黑色
//   
//    NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
//    NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
//    NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
//   
//    label.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//   
//   
//   
////   注意:
////   
////    NSForegroundColorAttributeName设置的颜色与UILabeltextColor属性设置的颜色在地位上是相等的,谁最后赋值,最终显示的就是谁的颜色。
////   
//   
//   
////    2. NSBackgroundColorAttributeName
//   
//   
//    //NSForegroundColorAttributeName设置字体颜色,取值为 UIColor,默认为黑色
//   
//    NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
//    NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
//    NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
//   
//    label.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//   
//   
//    //NSBackgroundColorAttributeName设置字体所在区域背景的颜色,取值为UIColor,默认值为nil
//   
//    NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
//    NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
//    NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
//   
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
//   
//   
//   
////   
////   仔细观察会发现个问题,我并没有关闭 NSForegroundColorAttributeName 属性,但是在运行结果中,所有字体的颜色都变成了默认色——黑色,这说明NSForegroundColorAttributeName NSBackgroundColorAttributeName 的低位是相等的,跟前面介绍的 textColor 一样,哪个属性最后一次赋值,就会冲掉前面的效果,若是我们把属性代码顺序交换一下
//   
//   
//    //NSBackgroundColorAttributeName设置字体所在区域背景的颜色,取值为UIColor,默认值为nil
//   
//    NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
//    NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
//    NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
//   
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
//   
//    //NSForegroundColorAttributeName设置字体颜色,取值为 UIColor,默认为黑色
//   
//    NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
//    NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
//    NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
//   
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//   
//   
//   
//   
////   但是textColor属性可以与NSBackgroundColorAttributeName属性叠加
//   
//   
//    label1.textColor = [UIColor greenColor];
//    label2.textColor = [UIColor yellowColor];
//    label3.textColor = [UIColor blueColor];
//   
//    //NSForegroundColorAttributeName设置字体颜色,取值为 UIColor,默认为黑色
//   
//    NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };
//    NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };
//    NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };
//   
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//   
//   
//    //NSBackgroundColorAttributeName设置字体所在区域背景的颜色,取值为UIColor,默认值为nil
//   
//    NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };
//    NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };
//    NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };
//   
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];
//   
//   
////   
////   虽然 textColor NSFontAttributeName 之前赋值,但是由于 NSFontAttributeName 的属性效果被NSBackgroundColorAttributeName属性冲掉了,所以最终显示了 textColor 的颜色。
//   
//   
//   
////    3. NSLigatureAttributeName
//   
//    //NSLigatureAttributeName设置连体属性,取值为NSNumber对象(整数)0表示没有连体字符,1表示使用默认的连体字符,
//    //                        2表示使用所有连体符号,默认值为 1iOS不支持 2
//   
//    NSString *ligatureStr = @"flush";
//   
//    NSDictionary *attrDict1 = @{ NSLigatureAttributeName: [NSNumber numberWithInt: 0],
//                                 NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30] };
//    label1.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict1];
//   
//    NSDictionary *attrDict2 = @{ NSLigatureAttributeName: @(1),
//                                 NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30]
//                                 };
//    label2.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict2];
////   由于要展示连体字符,所以将前面使用的带有中文的字符串换成 flush
////   
////    NSLigatureAttributeName的取值为NSNumber对象,所以不能直接将一个整数值赋给它,创建NSNumber对象的方法有很多,或者可以简写成 @(int)
//   
//   
//   
//   
//   
////   注意观察字母fl之间的变化。
////   
////   感觉连写就是一个艺术字功能,当字符fl组合使用组合符号(所谓的字形(glyph))绘制时,看起来确实更加美观。但是并非所有的字符之间都有组合符号,事实上,只有某些字体中得某些字符的组合(如字符fl,字符fi等)才具有美观的组合符号。
//   
//   
//   
////    4. NSKernAttributeName
//   
//   
//    //NSKernAttributeName设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
//   
//   
//    NSDictionary *attrDict1 = @{ NSKernAttributeName: @(-3),
//                                 NSFontAttributeName: [UIFont systemFontOfSize: 20]
//                                 };
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//   
//   
//    NSDictionary *attrDict2 = @{ NSKernAttributeName: @(0),
//                                 NSFontAttributeName: [UIFont systemFontOfSize: 20]
//                                 };
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//   
//   
//    NSDictionary *attrDict3 = @{ NSKernAttributeName: @(10),
//                                 NSFontAttributeName: [UIFont systemFontOfSize: 20]
//                                 };
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//   
//   
//   
//   
//   
//   
////    5. NSStrikethroughStyleAttributeName
//   
//   
//    //NSStrikethroughStyleAttributeName设置删除线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值
//    // NSUnderlineStyleNone  不设置删除线
//    // NSUnderlineStyleSingle设置删除线为细单实线
//    // NSUnderlineStyleThick 设置删除线为粗单实线
//    // NSUnderlineStyleDouble设置删除线为细双实线
//   
//   
//    NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//   
//   
//    NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//   
//   
//    NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
////   注意:
////   
////   虽然使用了枚举常量,但是枚举常量的本质仍为整数,所以同样必须先转化为 NSNumber 才能使用
////   
////   删除线和下划线使用相同的枚举常量作为其属性值
////   
////   目前iOS中只有上面列出的4中效果,虽然我们能够在头文件中发现其他更多的取值,但是使用后没有任何效果
//   
//   
//   
//   
//   
//   
////   可以看出,中文和英文删除线的位置有所不同
////   
////   另外,删除线属性取值除了上面的4种外,其实还可以取其他整数值,有兴趣的可以自行试验,取值为0 - 7时,效果为单实线,随着值得增加,单实线逐渐变粗,取值为9 - 15时,效果为双实线,取值越大,双实线越粗。
//   
//    NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(1),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//   
//   
//    NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(3),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//   
//   
//    NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(7),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    _label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//   
//   
//   
//   
//   
////    6. NSStrikethroughColorAttributeName
//   
//   
//    //NSStrikethroughColorAttributeName设置删除线颜色,取值为 UIColor 对象,默认值为黑色
//   
//    NSDictionary *attrDict1 = @{ NSStrikethroughColorAttributeName: [UIColor blueColor],
//                                 NSStrikethroughStyleAttributeName: @(1),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//   
//   
//    NSDictionary *attrDict2 = @{ NSStrikethroughColorAttributeName: [UIColor orangeColor],
//                                 NSStrikethroughStyleAttributeName: @(3),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//   
//   
//    NSDictionary *attrDict3 = @{ NSStrikethroughColorAttributeName: [UIColor greenColor],
//                                 NSStrikethroughStyleAttributeName: @(7),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//   
//   
//   
//   
//   
//   
////    7. NSUnderlineStyleAttributeName
//   
////   下划线除了线条位置和删除线不同外,其他的都可以完全参照删除线设置。
//   
//   
//    //NSUnderlineStyleAttributeName设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
//   
//    NSDictionary *attrDict1 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//   
//   
//    NSDictionary *attrDict2 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//   
//   
//    NSDictionary *attrDict3 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//   
//   
//   
//   
//   
//    8. NSUnderlineColorAttributeName
//   
////   可以完全参照下划线颜色设置
//   
//   
//    //NSUnderlineColorAttributeName设置下划线颜色,取值为 UIColor 对象,默认值为黑色
//   
//    NSDictionary *attrDict1 = @{ NSUnderlineColorAttributeName: [UIColor blueColor],
//                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//   
//   
//    NSDictionary *attrDict2 = @{ NSUnderlineColorAttributeName: [UIColor orangeColor],
//                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//   
//   
//    NSDictionary *attrDict3 = @{ NSUnderlineColorAttributeName: [UIColor greenColor],
//                                 NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:20] };
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//   
//   
//   
//   
//   
//   
////    9. NSStrokeWidthAttributeName
//   
//   
//    //NSStrokeWidthAttributeName设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
//   
//    NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
//    label1.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//   
//   
//    NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
//    label2.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//   
//   
//    NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),
//                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
//    label3.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
//   
//   
//   
//   
//   
//   
////    10. NSStrokeColorAttributeName
//   
//   
//    //NSStrokeColorAttributeName填充部分颜色,不是字体颜色,取值为 UIColor 对象
//   
//    NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),
//                                 NSStrokeColorAttributeName: [UIColor orangeColor],
//                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
//    _label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];
//   
//   
//    NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),
//                                 NSStrokeColorAttributeName: [UIColor blueColor],
//                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
//    _label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];
//   
//   
//    NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),
//                                 NSStrokeColorAttributeName: [UIColor greenColor],
//                                 NSFontAttributeName: [UIFont systemFontOfSize:30] };
//    _label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];
   
   
   
   
}


// 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 对象

0 0
原创粉丝点击