以Core Text为中心绘制NSAttributedString

时间:2012-11-11 00:35:40

标签: ios cocoa-touch text core-text nsattributedstring

我有一个在rect中绘制NSAttributedString的方法:

-(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);
}

几个月前,我在教程的帮助下把它放在一起。事实证明,默认情况下,这会将字符串左对齐。我对Core Text不太狡猾,有没有人知道我怎么能用文本对齐中心绘制它?

(请不要推荐外部标签绘图类,我需要使用Core Text执行此操作)。

2 个答案:

答案 0 :(得分:3)

CTTextAlignment的{​​{1}}设置为CTParagraphStyle,以获取您想要居中的属性字符串的范围。

然后创建kCTCenterTextAlignment,然后绘制。


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

答案 1 :(得分:0)

for 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: "Stop\nall Dance",
                                       attributes: attributes)


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