NSMutableAttributedString

来源:互联网 发布:剑三捏脸数据萝莉万花 编辑:程序博客网 时间:2024/03/29 20:15

NSMutableAttributedString *attrString = [[NSMutableAttributedString allocinitWithString:title];

    NSRange range = NSMakeRange(0, [attrString length]);

    

    [attrString beginEditing];

    [attrString addAttribute:NSLinkAttributeName value:aURL range:range];

    

    // make the text appear in blue

    [attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColorrange:range];

    

    // next make the text appear with an underline

    [attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumbernumberWithInteger:NSSingleUnderlineStylerange:range];

    

    // allow truncation

    NSMutableParagraphStyle *ps = [[NSParagraphStyle defaultParagraphStylemutableCopy];

    [ps setLineBreakMode:NSLineBreakByTruncatingTail];

    [attrString addAttribute:NSParagraphStyleAttributeName value:ps range:range];

    [ps release];

    

    [attrString endEditing];

    

    return [attrString autorelease];    



NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
3[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
4[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
5[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
6[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
7[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
8[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
9attrLabel.attributedText = str;

原创粉丝点击