【问题标题】:drawInRect: withAttributes text center vertically or put some paddingdrawInRect: withAttributes 文本垂直居中或放置一些填充
【发布时间】:2014-02-22 06:16:08
【问题描述】:

我在 iOS 7 中使用 drawInRect : withAttributes 向 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];

【问题讨论】:

    标签: ios objective-c ios6 ios7 uikit


    【解决方案1】:

    根据@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];
    

    【讨论】:

      【解决方案2】:

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

      我将分享一段我用来垂直居中的代码。在我的例子中,我使用不同的 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 生成的矩形将仅用于绘制文本,而不是边界矩形,否则您会得到您描述的效果。
      • 谢谢。得到它的工作。我无法使用您的代码,因为这些方法在 iOS 7 中已弃用。但我实现了您的概念 :-) 感谢您的帮助。
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        Swift 版本,以 extension 的形式,基于 @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)
        

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            猜你喜欢
            • 2011-09-06
            • 2013-10-15
            • 2015-09-16
            • 2015-08-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多