你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS6之後 NSAttributedString 的福利

iOS6之後 NSAttributedString 的福利

編輯:IOS開發綜合

@在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];
}


  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved