1

彼ら。Core Text を使用してリッチ テキスト エディタを作成しました。固定の行の高さを維持したいのですが、次のコードで実際に実行できます。

NSMutableAttributedString *_attributedText = [[NSMutableAttributedString alloc]initWithAttributedString:_attributedString] ;

CGFloat lineSpace=20;

CTParagraphStyleSetting lineSpaceStyle;

lineSpaceStyle.spec=kCTParagraphStyleSpecifierMaximumLineHeight;


lineSpaceStyle.valueSize=sizeof(lineSpace);

lineSpaceStyle.value=&lineSpace;

//CTLineBreakMode lineBreakMode = kCTLineBreakByCharWrapping;

CTParagraphStyleSetting settings[]={

    lineSpaceStyle,

   // {.spec = kCTParagraphStyleSpecifierLineBreakMode, .valueSize = sizeof(CTLineBreakMode), .value = (const void*)&lineBreakMode},

};

CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings));

[_attributedText addAttribute:(id)kCTParagraphStyleAttributeName value:(id)paragraphStyle range:NSMakeRange(0, _attributedText.mutableString.length)];
_attributedString = [_attributedText copy];
[_attributedText release];
_attributedText = nil;
CFRelease(paragraphStyle);

次に、画像を挿入したいと思います。次のコードを使用して、画像にテキストを入力します。

CTRunDelegateCallbacks callbacks = {
                                       .version = kCTRunDelegateVersion1,
                                       .dealloc = AttachmentRunDelegateDealloc,
                                       .getAscent = AttachmentRunDelegateGetAscent,
                                       .getDescent = AttachmentRunDelegateGetDescent,
                                       .getWidth = AttachmentRunDelegateGetWidth
                                   };

 CTRunDelegateRef Rundelegate = CTRunDelegateCreate(&callbacks, [image retain]); //3

                                   NSMutableDictionary *attrDictionaryDelegate = [NSMutableDictionary dictionaryWithDictionary:self.defaultAttributes];
                                   [attrDictionaryDelegate setObject:image
                                                              forKey:EGOTextAttachmentAttributeName];
                                   [attrDictionaryDelegate setObject:(id)Rundelegate
                                                              forKey:(NSString*)kCTRunDelegateAttributeName];
                                   [attrDictionaryDelegate setObject:fulltext
                                                              forKey:EGOTextAttachmentOriginStringKey];
                                   NSAttributedString *newString = [[NSAttributedString alloc] initWithString:EGOTextAttachmentPlaceholderString
                                                                                                   attributes:attrDictionaryDelegate];
                                   [attrString replaceCharactersInRange:[result resultByAdjustingRangesWithOffset:attrString.length-astring.length].range
                                                   withAttributedString:newString];

要するにCore Textで画像上にテキストをレイアウトさせる方法はありますか????? 誰????

4

1 に答える 1

1

非常に簡単

  • CoreText で行の高さを固定し、

  • 画像上のテキストレイアウトをimage.draw(in: frame)

で計算されたフレームかもしれませんCoreText

これが例です。背景画像を行の最初の単語の下に置きます

        // here is set up
       guard let ctx = UIGraphicsGetCurrentContext(), let f = frameRef else{
           return
       }
        
        let xHigh = bounds.size.height
        ctx.textMatrix = CGAffineTransform.identity
        ctx.translateBy(x: 0, y: xHigh)
        ctx.scaleBy(x: 1.0, y: -1.0)
        guard let lines = CTFrameGetLines(f) as? [CTLine] else{
           return
        }
        // here is an image
        let bgGrip = UIImage(named: "grip")
        if let pieces = CTLineGetGlyphRuns(line) as? [CTRun]{
                    let pieceCnt = pieces.count
                    for i in 0..<pieceCnt{
                        var p = lineOrigin
                        if i == 0{
                            var frame = imageFrame
                            frame.origin.y = lineOrigin.y + lineAscent - imageHeight + TextContentConst.offsetP.y
                            bgGrip?.draw(in: frame)
                            p.x += offsetX
                        }
                        ctx.textPosition = p
                        CTRunDraw(pieces[i], ctx, CFRange(location: 0, length: 0))
                    }
                }
于 2021-03-18T01:32:23.767 に答える