iOS6之後 NSAttributedString 的福利

來源:互聯網
上載者:User

標籤:富文本   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];}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.