drawInRect:withAttributes文本中心垂直或放置一些填充

时间:2014-02-22 06:16:09

标签: ios objective-c ios6 ios7 uikit

我正在使用drawInRect:withAttributes将文本添加到iOS 7中的pdf。我需要将文本垂直居中在CGRect内部,或者至少我需要在CGRect边框和文本之间保留一些间隙/填充。否则文字看起来太靠近盒子了。这样做有什么属性吗?如果不是最好的方法是什么?以下是我的代码 我试过NSBaselineOffsetAttributeName,但它只增加了每一行之间的差距,但没有增加到rect边界的间隙。
感谢

 CGContextRef    currentContext = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(currentContext, bgColor.CGColor);
CGRect renderingRect = CGRectMake(startPoint.x, startPoint.y, width, height);
CGContextFillRect(currentContext, renderingRect);

NSDictionary *attributes = @{ NSFontAttributeName: font,
                              NSForegroundColorAttributeName: fgColor,
                              };

[textToDraw drawInRect:renderingRect  withAttributes:attributes];

5 个答案:

答案 0 :(得分:17)

根据@ Merlevede的回答,使用新API,在iOS 8(或7+)中如何做到这一点:

    NSString *string = ....
    NSDictionary *attributes = ....
    CGSize size = [string sizeWithAttributes:attributes];

    CGRect r = CGRectMake(rect.origin.x,
                          rect.origin.y + (rect.size.height - size.height)/2.0,
                          rect.size.width,
                          size.height);


    [string drawInRect:r withAttributes:attributes];

答案 1 :(得分:9)

首先,您需要计算文本的高度,使用此信息和边界矩形的高度,您可以轻松计算新矩形以使文本居中。

我将分享一段代码,用于垂直居中。在我的情况下,我使用不同的drawInRect函数(drawInRect:withFont ...),我使用sizeWithFont来计算文本的大小。你可以调整这段代码来使用你已经使用过的函数(带属性),或者使用我在这里发布的函数。

UIFont *font = [UIFont systemFontOfSize:14];
CGSize size = [text sizeWithFont:font];
if (size.width < rect.size.width)
{
    CGRect r = CGRectMake(rect.origin.x, 
                          rect.origin.y + (rect.size.height - size.height)/2, 
                          rect.size.width, 
                          (rect.size.height - size.height)/2);
    [text drawInRect:r withFont:font lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentLeft];
}

答案 2 :(得分:5)

Swift 3解决方案:

extension NSString {
    func drawVerticallyCentered(in rect: CGRect, withAttributes attributes: [String : Any]? = nil) {
        let size = self.size(attributes: attributes)
        let centeredRect = CGRect(x: rect.origin.x, y: rect.origin.y + (rect.size.height-size.height)/2.0, width: rect.size.width, height: size.height)
        self.draw(in: centeredRect, withAttributes: attributes)
    }
}

答案 3 :(得分:3)

基于@WillLarche和@Merlevede答案的扩展程序形式的

Swift版本

extension CGRect {
    func verticalCenteredRectForString(string:NSString,withAttributes attributes : [String:AnyObject])->CGRect {
        let size = string.sizeWithAttributes(attributes)
        return CGRectMake(self.origin.x, self.origin.y + (self.size.height-size.height)/2.0 , self.size.width, size.height)
    }
}

用法

let myString = NSString(string:"Some text")
let centeredRect = customRect.verticalCenteredRectForString(myString,attrParams)
myString.drawInRect(centereRect, withAttributes : attrParams)

P.S。 我知道方法的名称可能更好;)

答案 4 :(得分:1)

Swift 4解决方案:

extension NSString {

    func drawVerticallyCentered(in rect: CGRect, withAttributes attributes: [NSAttributedStringKey : Any]? = nil) {
        let size = self.size(withAttributes: attributes)
        let centeredRect = CGRect(x: rect.origin.x, y: rect.origin.y + (rect.size.height-size.height)/2.0, width: rect.size.width, height: size.height)
        self.draw(in: centeredRect, withAttributes: attributes)
    }
}