1.
NSString *text = @"This\nis\nsome\nmulti-line\nsample\ntext.";
UIFont *uiFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CTFontRef ctFont = CTFontCreateWithName((CFStringRef) uiFont.fontName, uiFont.pointSize, NULL);
//
// // When you create an attributed string the default paragraph style has a leading
// // of 0.0. Create a paragraph style that will set the line adjustment equal to
// // the leading value of the font.
// CGFloat leading = uiFont.lineHeight - uiFont.ascender + uiFont.descender;
// CTParagraphStyleSetting paragraphSettings[1] = { kCTParagraphStyleSpecifierLineSpacingAdjustment, sizeof (CGFloat), &leading };
//
// CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(paragraphSettings, 1);
// direction
CTWritingDirection direction = kCTWritingDirectionLeftToRight;
// leading
CGFloat firstLineIndent = 0.0;
CGFloat headIndent = 0.0;
CGFloat tailIndent = 0.0;
CGFloat lineHeightMultiple = 1.0;
CGFloat maxLineHeight = 0;
CGFloat minLineHeight = 0;
CGFloat paragraphSpacing = 0.0;
CGFloat paragraphSpacingBefore = 0.0;
CTTextAlignment textAlignment = kCTTextAlignmentLeft;//(CTTextAlignment)_textAlignment;
CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;//(CTLineBreakMode)_lineBreakMode;
CGFloat lineSpacing = 3;//_lineSpacing;
CTParagraphStyleSetting theSettings[] =
{
{ kCTParagraphStyleSpecifierAlignment, sizeof(CTTextAlignment), &textAlignment },
{ kCTParagraphStyleSpecifierLineBreakMode, sizeof(CTLineBreakMode), &lineBreakMode },
{ kCTParagraphStyleSpecifierBaseWritingDirection, sizeof(CTWritingDirection), &direction },
{ kCTParagraphStyleSpecifierMinimumLineSpacing, sizeof(CGFloat), &lineSpacing }, // leading
{ kCTParagraphStyleSpecifierMaximumLineSpacing, sizeof(CGFloat), &lineSpacing }, // leading
{ kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &firstLineIndent },
{ kCTParagraphStyleSpecifierHeadIndent, sizeof(CGFloat), &headIndent },
{ kCTParagraphStyleSpecifierTailIndent, sizeof(CGFloat), &tailIndent },
{ kCTParagraphStyleSpecifierLineHeightMultiple, sizeof(CGFloat), &lineHeightMultiple },
{ kCTParagraphStyleSpecifierMaximumLineHeight, sizeof(CGFloat), &maxLineHeight },
{ kCTParagraphStyleSpecifierMinimumLineHeight, sizeof(CGFloat), &minLineHeight },
{ kCTParagraphStyleSpecifierParagraphSpacing, sizeof(CGFloat), ¶graphSpacing },
{ kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), ¶graphSpacingBefore }
};
CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, sizeof(theSettings) / sizeof(CTParagraphStyleSetting));
CFRange textRange = CFRangeMake(0, text.length);
// Create an empty mutable string big enough to hold our test
CFMutableAttributedStringRef string = CFAttributedStringCreateMutable(kCFAllocatorDefault, text.length);
// Inject our text into it
CFAttributedStringReplaceString(string, CFRangeMake(0, 0), (CFStringRef) text);
// Apply our font and line spacing attributes over the span
CFAttributedStringSetAttribute(string, textRange, kCTFontAttributeName, ctFont);
CFAttributedStringSetAttribute(string, textRange, kCTParagraphStyleAttributeName, paragraphStyle);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(string);
CFRange fitRange;
CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, textRange, NULL, self.view.bounds.size, &fitRange);
CFRelease(paragraphStyle);
CFRelease(ctFont);
CFRelease(framesetter);
CFRelease(string);