NSMutableAttributeString

来源:互联网 发布:oracle sql nvl函数 编辑:程序博客网 时间:2024/06/08 00:07
 [mutableAttributed addAttribute:(NSString *)(kCTForegroundColorAttributeName)
                       value:(id)[UIColor blueColor].CGColor
                       range:NSMakeRange(0, len)];


  CTFontRef ctFont = CTFontCreateWithName((CFStringRef)AtFont.fontName, 
                                            AtFont.pointSize, 
                                            NULL);
  [mutableAttributed addAttribute:(NSString *)(kCTFontAttributeName) 
                       value:(id)ctFont 
                       range:NSMakeRange(0, 1)];
   [attrString addAttribute:(__bridge NSString *)(kCTForegroundColorAttributeName)
                       value:(id)[UIColor blueColor].CGColor
                       range:NSMakeRange(0, len)];


 [mutableAttributed addAttribute:(__bridge NSString*)kCTUnderlineStyleAttributeName value:[NSNumbernumberWithInteger:NSSingleUnderlineStyle] range:range];



////////////////////////////////////////////////////////////////////////////////////////////


 @property (nonatomic, assign) CGFloat lineSpacing;
/* Distance between the bottom of this paragraph and top of next (or the beginning of its paragraphSpacingBefore, if any) */
@property (nonatomic, assign) CGFloat paragraphSpacing;


@property (nonatomic, assign) CTTextAlignment textAlignment;
@property (nonatomic, assign) CTLineBreakMode lineBreakMode;


@property (nonatomic, assign) CGFloat firstLineHeadIndent; // Distance from margin to edge appropriate for text direction
@property (nonatomic, assign) CGFloat headIndent; // Distance from margin to front edge of paragraph
@property (nonatomic, assign) CGFloat tailIndent; // Distance from margin to back edge of paragraph. Use negative values for distance to other edge.

@property (nonatomic, assign) CTWritingDirection baseWritingDirection;


/* Line height is the distance from bottom of descenders to top of ascenders; basically the line fragment height. Does not include lineSpacing (which is added after this computation). */
@property (nonatomic, assign) CGFloat minimumLineHeight;
@property (nonatomic, assign) CGFloat maximumLineHeight; // 0 implies no maximum.
/* Natural line height is multiplied by this factor (if positive) before being constrained by minimum and maximum line height. */
@property (nonatomic, assign) CGFloat lineHeightMultiple;
/* Distance between the bottom of the previous paragraph (or the end of its paragraphSpacing, if any) and the top of this paragraph. */
@property (nonatomic, assign) CGFloat paragraphSpacingBefore;
 
 +(CTParagraphStyleRef)createCTParagraphStyle (添加类别)
{
    const int kSettingsCount = 12;
    CTParagraphStyleSetting settings[kSettingsCount] =
    {
        { kCTParagraphStyleSpecifierLineSpacing, sizeof(_lineSpacing), &_lineSpacing },
        { kCTParagraphStyleSpecifierParagraphSpacing, sizeof(_paragraphSpacing), &_paragraphSpacing },
        { kCTParagraphStyleSpecifierAlignment, sizeof(_textAlignment), &_textAlignment },
        { kCTParagraphStyleSpecifierLineBreakMode, sizeof(_lineBreakMode), &_lineBreakMode },


        { kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(_firstLineHeadIndent), &_firstLineHeadIndent},
        { kCTParagraphStyleSpecifierHeadIndent, sizeof(_headIndent), &_headIndent},
        { kCTParagraphStyleSpecifierTailIndent, sizeof(_tailIndent), &_tailIndent},
        { kCTParagraphStyleSpecifierBaseWritingDirection, sizeof(_baseWritingDirection), &_baseWritingDirection },
        
        { kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(_minimumLineHeight), &_minimumLineHeight },
        { kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(_maximumLineHeight), &_maximumLineHeight },
        { kCTParagraphStyleSpecifierLineHeightMultiple, sizeof(_lineHeightMultiple), &_lineHeightMultiple },
        { kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(_paragraphSpacingBefore), &_paragraphSpacingBefore }
    };
    return CTParagraphStyleCreate(settings, kSettingsCount);
}


 CTParagraphStyleRef newParaStyle = [CTParagraphStyleRef createCTParagraphStyle];
[self removeAttribute:(__bridge NSString*)kCTParagraphStyleAttributeName range:range]; // Work around for Apple leak
[self addAttribute:(__bridge NSString*)kCTParagraphStyleAttributeName value:(__bridge id)newParaStyle range:range];
 CFRelease(newParaStyle);
 
iOS6 以上(包括IOS6)
   NSMutableParagraphStyle *paragraphStyle;
   
   paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
   [paragraphStyle setAlignment:NSCenterTextAlignment];
   
   NSFont *displayFont = [NSFont fontWithName:@"Helvetica-BoldOblique" size:18];
   NSDictionary *attribDict = [NSDictionary dictionaryWithObjectsAndKeys:displayFont, NSFontAttributeName, [NSColor whiteColor], NSForegroundColorAttributeName, paragraphStyle, NSParagraphStyleAttributeName, nil];
   NSAttributedString *attrib = [[NSAttributedString alloc] initWithString:@"hello, world" attributes:attribDict];
   [displayField setAttributedTitle:attrib];
   [attrib release];
   [paragraphStyle release];
0 0
原创粉丝点击