iOS6之后 NSAttributedString 的福利

         @在iOS6之前需要使用NSMutableAttributedString时都需要导入:CoreText.framework框架的,但在iOS6 之后就不在需要了.

- (void)testOfNSMutableAttributedStringAndNSAttributedString
{
    /**
     *  - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
     *  主要方法
     *  name   属性名
     *  value  属性对应效果的值
     *  range  效果所映射的范围
     */
    
    #pragma mark  测试数据0
    NSString *testString = @"NSMutableAttributed---0";
    UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 40)];
    NSMutableAttributedString * testAttriString = [[NSMutableAttributedString alloc] initWithString:testString];
    // 添加删除线
    [testAttriString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, testAttriString.length)];
    // 添加下划线
    [testAttriString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, testAttriString.length)];
    // 设置文本的字体以及大小
    [testAttriString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-Bold" size:15] range:NSMakeRange(0, testAttriString.length)];
    // 设置笔画的粗细
    [testAttriString addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleDouble] range:NSMakeRange(0, testAttriString.length)];
    // label的背景颜色
    [testAttriString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(0, testAttriString.length)];
    // 目前没测出什么效果.....
    [testAttriString addAttribute:NSVerticalGlyphFormAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(0, testAttriString.length)];
    // label上文本颜色(也会影响删除线和下划线的颜色)
    [testAttriString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, testAttriString.length)];
    testLabel.attributedText = testAttriString;
    
    #pragma mark 测试数据1
    NSString *testString1 = @"NSMutableAttributed---1";
    UILabel *testLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 200, 200, 40)];
    NSMutableAttributedString * testAttriString1 = [[NSMutableAttributedString alloc] initWithString:testString1];
    // 实现文本内容颜色和下划线,删除线的颜色不一样
    // NSStrokeColorAttributeName 单独设置没有效果
    // 必须与NSStrokeWidthAttributeName一起设置
    [testAttriString1 addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(0, testAttriString1.length)];
    [testAttriString1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, testAttriString1.length)];
    [testAttriString1 addAttribute:NSStrokeColorAttributeName value:[UIColor cyanColor] range:NSMakeRange(0, testAttriString1.length)];
    [testAttriString1 addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleDouble] range:NSMakeRange(0, testAttriString1.length)];
    testLabel1.attributedText = testAttriString1;
    
    
    #pragma mark 测试数据2
    NSString *testString2 = @"NSMutableAttributed---2";
    UILabel *testLabel2= [[UILabel alloc] initWithFrame:CGRectMake(60, 300, 200, 40)];
    NSMutableAttributedString * testAttriString2 = [[NSMutableAttributedString alloc] initWithString:testString2];
    // 笔画的阴影效果
    NSShadow *shadow = [[NSShadow alloc] init];
    [shadow setShadowColor:[UIColor colorWithRed:0.053 green:0.088 blue:0.205 alpha:1.000]];
    [shadow setShadowBlurRadius:4.0];
    [shadow setShadowOffset:CGSizeMake(2, 2)];
    [testAttriString2 addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, [testAttriString2 length])];
    testLabel2.backgroundColor = [UIColor clearColor];
    testLabel2.attributedText = testAttriString2;
    
    [self.view addSubview:testLabel];
    [self.view addSubview:testLabel1];
    [self.view addSubview:testLabel2];
}
iOS6之后 NSAttributedString 的福利

iOS6之后 NSAttributedString 的福利,布布扣,bubuko.com

iOS6之后 NSAttributedString 的福利

上一篇:iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Type


下一篇:android开发利器--站在巨人肩膀上前行