在图像上绘制的文字太小

时间:2017-08-06 17:29:03

标签: ios swift text draw

我正在尝试将一个可调整大小的textView绘制到类似于Snapchat的图像上,但是当我将标签绘制到图像上时,文本显着更小并且更靠近左上角。

func drawTextToImage(){

    let rect = textView.frame
    let showText = textView.text
    let font = UIFont.boldSystemFont(ofSize: 26)
    let attr = [NSFontAttributeName: font, NSForegroundColorAttributeName:UIColor.white]
    let image = UIImage(named: "3FactorPostGreen")

    //First create the rotated and transparent image with text
    UIGraphicsBeginImageContextWithOptions(CGSize(width: rect.size.width, height: rect.size.height), false, 0)
    if let context = UIGraphicsGetCurrentContext() {
        context.rotate (by: 45.0 * CGFloat.pi/180.0) //45˚
    }
    showText?.draw(in: CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width, height: rect.size.height), withAttributes: attr)
    let rotatedImageWithText = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext()


    //Then, draw rotated image(with text) onto the background image
    UIGraphicsBeginImageContextWithOptions(CGSize(width: rect.size.width, height: rect.size.height), true, 0)

    image?.draw(in: rect)
    rotatedImageWithText?.draw(in: rect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext()

    self.imageView.image = newImage
}

然后这是照片之前和之后

更大的文字: enter image description here enter image description here

较小的文字: enter image description here enter image description here

我不知道当这种情况发生时会发生什么,但如果有人能够解释和帮助那将是非常棒的。

0 个答案:

没有答案