【问题标题】:Text size is not same when draw text on UIimage在 UIimage 上绘制文本时文本大小不同
【发布时间】:2016-02-15 10:07:21
【问题描述】:

我正在做一个项目,我需要将文本添加到来自文本字段的图像中。

但是当我看到图像上的文字时,它显示的字体大小小于文本字段的字体大小。

我正在使用以下方法在图像上绘制文本

func drawText(text: String, inImage image: UIImage, atPoint point: CGPoint, fontName:String, fontSize: String,textColor: UIColor) -> UIImage? {

        UIGraphicsBeginImageContextWithOptions(image.size, true, UIScreen.mainScreen().scale)
        UIGraphicsBeginImageContext(image.size)

        image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
        let rect: CGRect = CGRectMake(point.x, point.y, image.size.width, image.size.height)
        // UIColor.whiteColor().set()

        // set the font to Helvetica Neue 18
        if let sizeFont = NSNumberFormatter().numberFromString(fontSize) {

            // TODO: - Need to resolve issue
            let originalSize = sizeFont.integerValue

            let finalFontSize = CGFloat(originalSize)

            let fieldFont = UIFont(name: fontName, size:finalFontSize*1.5)

            // set the line spacing to 6
            let paraStyle = NSMutableParagraphStyle()
            // paraStyle.lineSpacing = 6.0

            // set the Obliqueness to 0.1
            // let skew = 0.1

            let attributes: NSDictionary = [
                NSForegroundColorAttributeName: textColor,
               // NSParagraphStyleAttributeName: paraStyle,
                // NSObliquenessAttributeName: skew,
                NSFontAttributeName: fieldFont!
            ]

            NSString(string: text).drawInRect(rect, withAttributes: attributes as? [String : AnyObject])

            let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage
        }

        return nil
    }

字体大小可以是16-60px。

请告诉我可能的解决方案。

谢谢

【问题讨论】:

  • 请贴一些代码,还有你想要的字体大小,
  • 我遇到了同样的问题。你找到解决办法了吗?

标签: ios swift uiimage drawrect cgcontext


【解决方案1】:

您的代码似乎一切正常。
一个可能的问题是您在 ImageView 中看不到全尺寸的图像,因为它会缩放它,所以您看到的字体比您想要的要小。
您可以在在其上绘制文本之前调整图像大小以适合将显示的容器。 或者你可以计算出fontSize 乘以1/scale,得到
scale = the scale at will the image will be shown
例如,如果图像大于大于并且容器(imageView)小于图像比例将为image.size.height/imageView.frame.size.height
我认为这可以解决您的问题。

【讨论】:

    【解决方案2】:

    正如 LorenzOliveto 所建议的,这可能是由于缩放问题。一种简单的解决方法是将文本添加为​​ imageView 的子视图,然后使用此扩展将 imageView 转换为图像。

    extension UIView {
        /**
         Convert UIView to UIImage
         */
        func toImage() -> UIImage {
            UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0.0)
            self.drawHierarchy(in: self.bounds, afterScreenUpdates: false)
            let snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return snapshotImageFromMyView!
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      相关资源
      最近更新 更多