drawInRect: withAttributes如何使文本居中垂直显示或添加一些填充。

14

我正在使用drawInRect:withAttributes在iOS 7中向PDF添加文本。我需要垂直居中文本在CGRect内部,或者至少要在CGRect边框和文本之间保留一定的间隙/填充。否则,文本看起来太靠近框了。是否有任何属性可以做到这一点?如果没有,最好的方法是什么?下面是我的代码。
我尝试过NSBaselineOffsetAttributeName,但它只增加每行之间的间距,而不是与矩形边框之间的间距。
谢谢

 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个回答

17

以下是如何在iOS 8中(或7+)执行此操作,基于@Merlevede的答案进行了更正并使用了新的API:

    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];

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];
}

谢谢,我会尝试这个方法。但是我之前尝试过类似的方法,通过改变y轴位置,整个矩形都下降了,而不仅仅是文本。可能是我做错了什么。我会再次尝试并查看结果。 - Madhu
@Madhu,生成的矩形仅用于绘制文本,而不是边界矩形,否则会出现您所描述的效果。 - Merlevede
谢谢。搞定了。我不能使用你的代码,因为这些方法在iOS 7中已经过时了。但是我实现了你的概念 :-) 感谢你的帮助。 - Madhu

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

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

3

Swift版本扩展形式存在,基于@WillLarche和@Merlevede的答案。

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)

附言:我知道方法的名称可能会更好;)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接