第9月第9天 CTFramesetterCreateWithAttributedString

时间:2023-03-09 15:51:53
第9月第9天 CTFramesetterCreateWithAttributedString

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 = ;
CGFloat minLineHeight = ;
CGFloat paragraphSpacing = 0.0;
CGFloat paragraphSpacingBefore = 0.0;
CTTextAlignment textAlignment = kCTTextAlignmentLeft;//(CTTextAlignment)_textAlignment;
CTLineBreakMode lineBreakMode = kCTLineBreakByWordWrapping;//(CTLineBreakMode)_lineBreakMode;
CGFloat lineSpacing = ;//_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), &paragraphSpacing },
{ kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), &paragraphSpacingBefore }
}; CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(theSettings, sizeof(theSettings) / sizeof(CTParagraphStyleSetting)); CFRange textRange = CFRangeMake(, 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(, ), (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);

https://github.com/honcheng/RTLabel

https://*.com/questions/3374591/ctframesettersuggestframesizewithconstraints-sometimes-returns-incorrect-size