码迷,mamicode.com
首页 > 移动开发 > 详细

iOS6之后 NSAttributedString 的福利

时间:2014-08-17 22:49:13      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:富文本   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];
}

bubuko.com,布布扣

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

iOS6之后 NSAttributedString 的福利

标签:富文本   nsattributedstring   

原文地址:http://blog.csdn.net/hmt20130412/article/details/38645141

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!