NSMutableAttributeString

 [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];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值