以核心文本为中心绘制nsattribatedstring



我有一种方法,可以在rect中绘制nsattribedstring:

-(void)drawInRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge  CFAttributedStringRef)self);
    // left column form
    CGMutablePathRef leftColumnPath = CGPathCreateMutable();
    CGPathAddRect(leftColumnPath, NULL, CGRectMake(rect.origin.x, -rect.origin.y, rect.size.width, rect.size.height));
    // left column frame
    CGFloat translateAmount = rect.size.height;
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, translateAmount);
    CGContextScaleCTM(context, 1.0, -1.0);
    CTFrameRef leftFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), leftColumnPath, NULL);
    CTFrameDraw(leftFrame, context);
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, translateAmount);
    CGContextScaleCTM(context, 1.0, -1.0);
    CFRelease(leftFrame);
    CFRelease(framesetter);
    CGPathRelease(leftColumnPath);
}

几个月前,我在教程的一些帮助下将其放在一起。事实证明,默认情况下,绘制左对齐的字符串。我对核心文字不太狡猾,有人知道我如何能够用文本对齐中心绘制它吗?

(请不要推荐外部标签绘图课,我需要使用核心文本进行此操作)。

CTParagraphStyleCTTextAlignment设置为您想要的属性字符串范围的kCTCenterTextAlignment

然后创建CTFramesetter,然后绘制。


示例:http://foobarpig.com/iphone/coretext-set-text-text-font-and-alignment-to-cfattribedstring.html

swift 3.0:

final private func drawIn(rect: CGRect, attribString: NSAttributedString ){
        guard let context = UIGraphicsGetCurrentContext() else{
            return
        }
        let framesetter = CTFramesetterCreateWithAttributedString(attribString)
        // left column form
        let leftColumnPath = CGMutablePath()
        leftColumnPath.addRect(CGRect(x:rect.origin.x,
                                      y: -rect.origin.y,
                                      width: rect.size.width,
                                      height: rect.size.height)
        )
        // left column frame
        let translateAmount = rect.size.height
        context.saveGState()
        context.textMatrix = CGAffineTransform.identity
        context.translateBy(x: 0, y: translateAmount)
        context.scaleBy(x: 1.0, y: -1.0)
        let leftFrame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), leftColumnPath, nil)
        CTFrameDraw(leftFrame, context)
        context.textMatrix = CGAffineTransform.identity
        context.translateBy(x: 0, y: translateAmount)
        context.scaleBy(x: 1.0, y: -1.0)
    }

    final private func drawStopMessage(){
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center

        let attributes = [NSParagraphStyleAttributeName :  paragraphStyle,
                          NSFontAttributeName :   UIFont.systemFont(ofSize: 24.0),
                          NSForegroundColorAttributeName : UIColor.blue,
                          ]
        let attrString = NSAttributedString(string: "Stopnall Dance",
                                       attributes: attributes)

        let rect = CGRect(x: 20, y: 100, width: 300, height: 300)
        drawIn(rect: rect, attribString: attrString)
    }

最新更新