zoukankan      html  css  js  c++  java
  • 第9月第9天 CTFramesetterCreateWithAttributedString

    1.

            NSString  *text = @"This
    is
    some
    multi-line
    sample
    text.";
            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), &paragraphSpacing },
                { kCTParagraphStyleSpecifierParagraphSpacingBefore, sizeof(CGFloat), &paragraphSpacingBefore }
            };
            
            
            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);

    https://github.com/honcheng/RTLabel

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

  • 相关阅读:
    问题:为命名空间,在此被用作类型和此项目作为引用添加将导致循环依赖项
    关于ObjectDataProvider绑定方法使用案例
    关于C#的数据绑定,存取数据库实例详解 (三)
    关于C#的数据绑定,存取数据库实例详解 (二)
    关于C#的数据绑定,存取数据库实例详解 (一)
    UOS安装到虚拟机上
    连接第三方接口
    工作流
    关于国产项目开发(数据库+服务器+架构)
    字段显示替换
  • 原文地址:https://www.cnblogs.com/javastart/p/6971907.html
Copyright © 2011-2022 走看看