2017-02-28 4 views
3

背景:Core Graphicsを使用したUIImageViewで描画しています。私は最終的にコアグラフィックス図面上にテキスト文字列を描画したいと思います。Swift 3のCore Graphics上にテキストを描画する。

This (hackingwithswift.com)私は画像にテキストを描画し、その画像にmy imageViewを設定してからコアグラフィックスを描画します。しかし、私はテキストをコアグラフィックスの描画の上に重ねることを望みます。

今私はこのhttps://stackoverflow.com/a/35574171/6337391に移動しましたが、私はまだ私の図面の上に書くことができません。

class MapViewController: UIViewController { 

    @IBOutlet var imageView: UIImageView! 

    func drawScale() { 
     // establish 6 points to draw a little bar with 
     let length = CGFloat(80.0) 
     let bottom = imageView.bounds.size.height - 15.0 
     let left = CGFloat(20.0) 
     let top = bottom - 20.0 
     let middle = top + 10.0 
     let right = left + length 
     let topLeft = CGPoint(x: left, y: top) 
     let topRight = CGPoint(x: right, y: top) 
     let bottomRight = CGPoint(x: right, y: bottom) 
     let bottomLeft = CGPoint(x: left, y: bottom) 
     let middleLeft = CGPoint(x: left, y: middle) 
     let middleRight = CGPoint(x: right, y: middle) 

     // draw the bar 
     drawLineFrom(fromPoint: topLeft, toPoint: bottomLeft, color: UIColor.red.cgColor, width: 1.0, alias: false) 
     drawLineFrom(fromPoint: topRight, toPoint: bottomRight, color: UIColor.red.cgColor, width: 1.0, alias: false) 
     drawLineFrom(fromPoint: middleLeft, toPoint: middleRight, color: UIColor.red.cgColor, width: 1.0, alias: false) 

     // draw a text string 
     UIGraphicsBeginImageContext(self.imageView.frame.size) 

     let paragraphStyle = NSMutableParagraphStyle() 
     paragraphStyle.alignment = .center 

     let attrs = [ 
      NSFontAttributeName: UIFont.systemFont(ofSize: 12), 
      NSParagraphStyleAttributeName: paragraphStyle, 
      NSForegroundColorAttributeName: UIColor.red] 

     let scaleString = "45 feet" 
     scaleString.draw(at: CGPoint(x: 0, y: 50), withAttributes: attrs) 

     let newImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
    } 

    /* Stroke a line between two CGPoints. Color, width, anti-alias options. */ 
    func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint, color: CGColor, width: CGFloat, alias: Bool) { 
     // 1 
     UIGraphicsBeginImageContext(self.imageView.frame.size) 
     let context = UIGraphicsGetCurrentContext()! 
     context.setShouldAntialias(alias) 
     self.imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.imageView.frame.size.width, height: self.imageView.frame.size.height)) 

     // 2 
     context.move(to: fromPoint) 
     context.addLine(to: toPoint) 

     // 3 
     context.setLineWidth(width) 
     context.setStrokeColor(color) 

     // 4 
     context.strokePath() 

     // 5 
     self.imageView.image = UIGraphicsGetImageFromCurrentImageContext() 
     self.imageView.alpha = 1.0 
     UIGraphicsEndImageContext() 
    } 
} 

doesnt work。テキスト文字列を描画すると、バーが完全に消去されます。文字列描画コードの下にバー描画コードを移動すると、両方が表示されますが、もちろんバーは文字列の上にあります。

+0

あなたがCATextLayerにテキストを作成し、画像ビューに追加してもらえますか? – dfd

+0

「コアグラフィックスを使用したUIImageViewを描画しています」とは何を意味し、どのクラスでコードを実行していますか? –

+0

文脈コードを少し追加しました。役立つと思います。答えはCore Textの中にあると思います。私はその道を自分で解決しようとします。あなたはUIGraphicsBeginImageContextを削除した点で私のコードと異なるだけです。(リンク先はhttp://stackoverflow.com/questions/27052665/swift-playground-core-graphics-core-text-custom-view) –

答えて

0

スウィフト3.0:下のテキスト

func drawMyText(myText:String,textColor:UIColor, FontName:String, FontSize:CGFloat, inRect:CGRect){ 

    let textFont = UIFont(name: FontName, size: FontSize)! 
    let textFontAttributes = [ 
     NSFontAttributeName: textFont, 
     NSForegroundColorAttributeName: textColor, 
     ] as [String : Any] 

    myText.draw(in: inRect, withAttributes: textFontAttributes) 
} 

コールこの機能は、例えばある描画します

let _rect = CGRect(x: 40, y: 100, width: 40, height: 40) 

drawMyText(myText: "Hello", textColor: UIColor.black, FontName: "Helvetica Bold", FontSize: 11 , inRect:_rect) 
+0

self.imageView.frame.size)。私の場合、あなたのコードはテキストを描画しません。 –

+0

drawMyText関数をuiviewサブクラスに置き、drawrect関数から呼び出します。さらに助けが必要な場合はお知らせください – Devesh

+1

私の問題は、描画テキストが前に描画したものを消去することです。上記の私の例では、テキストが表示されますが、私が前に描いた線は完全に消去されます。私が言うことができる限り、唯一のことは、あなたが私のために何もしていない点ではなく、rectでテキストを描くことを提案することです。 –

2

SWIFT 4

let paragraphStyle = NSMutableParagraphStyle() 
    paragraphStyle.alignment = .center 

    let attributes = [NSAttributedStringKey.paragraphStyle : paragraphStyle, 
         NSAttributedStringKey.font   : UIFont.systemFont(ofSize: 12.0), 
         NSAttributedStringKey.foregroundColor : UIColor.blue, 
         ] 

    let myText = "HELLO" 
    let attrString = NSAttributedString(string: myText, 
           attributes: attributes) 

    let rt = CGRect(x: W/2 - 50, y: border, width: 100, height: H) 
    attrString.draw(in: rt) 
関連する問題