2016-11-20 8 views
0

SO POSTの回答を使用して、画像にテキストを描画しています。私が抱えている問題は、CGPointがイメージの右端付近に設定され、テキストがイメージの外に広がっている場合です。私は、CGPointからの代わりに左を描画する方法を見つけようとしています。または、何らかの形で描画されるテキストポイントのサイズを計算し、このサイズに合わせて描画ポイントを移動します。画像の上にテキストが伸びる

ここに私が使用しているコードがあります。

func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage { 

    let color = UserDefaults.standard.colorForKey(key: "color")! 
    let font = UserDefaults.standard.value(forKey: "font") as! String 
    let size = UserDefaults.standard.value(forKey: "size") as! Int 
    let fontAndSize = UIFont(name: font, size: CGFloat(size))! 

    let scale = UIScreen.main.scale 
    UIGraphicsBeginImageContextWithOptions(image.size, false, scale) 

    let textFontAttributes = [ 
     NSFontAttributeName: fontAndSize, 
     NSForegroundColorAttributeName: color, 
     ] as [String : Any] 
    image.draw(in: CGRect(origin: CGPoint.zero, size: image.size)) 

    let rect = CGRect(origin: point, size: image.size) 
    text.draw(in: rect, withAttributes: textFontAttributes) 

    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage! 
    } 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let mediaType = info[UIImagePickerControllerMediaType] as? String { 
     if mediaType.isEqual((kUTTypeImage as String)) { 
     if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { 
      let width = pickedImage.size.width 
      let height = pickedImage.size.height 
      // this puts the point in the bottom right of the image which then causes the text drawn to extend off of the image 
      let point = CGPoint(x: width - 20, y: height - 50) 
      let timestampText = getTimestampText() as NSString 
      let timestampImage = textToImage(drawText: timestampText, inImage: pickedImage, atPoint: point) 
      if newMedia { 
      UIImageWriteToSavedPhotosAlbum(timestampImage, nil, nil, nil) 
      } 
     } 

答えて

2

を使用して、右にテキストを揃える試すことができます描画してから、このサイズに対応するように描画ポイントを移動させてください」。boundingRectWithSize:options:attributes:context:を使用します。例:

func textToImage(drawText text: NSString, inImage image: UIImage, atPoint point: CGPoint) -> UIImage { 

    let color = UserDefaults.standard.colorForKey(key: "color")! 
    let font = UserDefaults.standard.value(forKey: "font") as! String 
    let size = UserDefaults.standard.value(forKey: "size") as! Int 
    let fontAndSize = UIFont(name: font, size: CGFloat(size))! 

    let scale = UIScreen.main.scale 
    UIGraphicsBeginImageContextWithOptions(image.size, false, scale) 

    let textFontAttributes = [ 
     NSFontAttributeName: fontAndSize, 
     NSForegroundColorAttributeName: color, 
     ] as [String : Any] 

    // Calculate text size 
    let rectSize = text.boundingRect(with: CGSize(width:CGFloat(MAXFLOAT), height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: textFontAttributes, context: nil).size 

    // Subtract the text width from the x origin 
    let rect = CGRect(x:point.x-rectSize.width, y:point.y, width:rectSize.width, height: rectSize.height) 
    text.draw(in: rect, withAttributes: textFontAttributes) 
} 
+0

ありがとうございました!これはうまくいった。私はまた、どのように中心を描くのか把握しようとしています。そのためrectプロパティを次のように変更しました。rect = CGRect(x:point.x-(rectSize.width/2)、y:point.y、width:rectSize.width、height:rectSize.height)これは私に正しいx位置を与えていますが、y位置は正確に中央に位置していません。代わりに、画像上の中心より少し下です。それが正確に中心に置かれていない理由に関する提案はありますか? – chickenparm

+1

心配しないで! rect = CGRect(x:point.x-(rectSize.width/2)、y:point.y-(rectSize.height/2)、width:rectSize.width、height:rectSize.height)に変更しました。それは働いている。 – chickenparm

+0

@chickenparm聞くとうれしい:) –

0

あなたが左に描画する場合は、あなたが実際には「その意志のテキストのポイントでサイズを計算することができますNSParagraphStyleAttributeName

let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle 
textStyle.alignment = NSTextAlignment.rightTextAlignment 

let textFontAttributes = [ 
    NSFontAttributeName: fontAndSize, 
    NSForegroundColorAttributeName: color, 
    NSParagraphAttributeStyleName: textStyle 
    ] as [String : Any] 
+0

あなたの回答よりも。私はあなたの返答で欲しかったのとまったく同じように働くことができませんでしたが、私はそれをLyndseyの反応と一緒に働かせることができました。 – chickenparm

関連する問題