PhotoshopでUIBezierPath(2018/03/02版)

PhotoshopでUIBezierPathで作った時とSwiftの仕様がまた変わっているようなので新しいものをば。
多分今はSwift3.0? 新しいものがどんどん出てくるのでついていくのがちょっときついですね…^^;

とりあえずSwift3.0用に書き直してみました
それと、いまたまたま気づいたんですがPhothoshopの環境設定で「定規」の単位をpixelにしてないとうまく動きませんね…
何か回避する方法はあるのかしらん…

// Only run if we're in a supported version of Photoshop
// Note, removing this will not make the script work, as it depends on
// core application code, it just makes it fail better.
if (!app.featureEnabled("ImageStack Creation")) {
	throw (new Error(localize("$$$/JavaScripts/Statistics/CommandNotAvailable=The Statistics feature is not available in this version of Photoshop.")));
}
 
try{
	makePath();
}catch(err){
	alert(err);
}
 
function makePath(){
//	preferences.rulerUnits = Units.PIXELS;
	height = activeDocument.height.value
	width = activeDocument.width.value
	var str="//let height : CGFloat = "+String(height)+";\n";
	str += "//let width : CGFloat = "+String(width)+";\n";
	if(activeDocument.pathItems.length == 0){
		throw "パスを選択してください。";
	}
	for(var i=0; i<activeDocument.pathItems.length; i+=1){
		var activePath = activeDocument.pathItems[i];
		str += "let "+activePath.name+" : UIBezierPath = UIBezierPath();\n";
		for(var j=0; j<activePath.subPathItems.length; j+=1){
			for(var k=0; k<activePath.subPathItems[j].pathPoints.length; k+=1){
				var activePathPoints = activePath.subPathItems[j].pathPoints;
				if(k==0){
					str += activePath.name+".move(to: CGPoint(x: "+String(activePathPoints[k].anchor[0])+", y: "+String(	activePathPoints[k].anchor[1])+"));\r\n";
//					str += activePath.name+".move(to: CGPoint(x: "+String(activePathPoints[k].anchor[0])+", y: "+String(height-	activePathPoints[k].anchor[1])+"));\r\n";
				}else{
					str += activePath.name+".addCurve(to: CGPoint(x: "+String(activePathPoints[k].anchor[0])+", y: "+String(activePathPoints[k].anchor[1])+"),\r\n";
					str += "controlPoint1: CGPoint(x: "+String(activePathPoints[k-1].leftDirection[0])+", y: "+String(activePathPoints[k-1].leftDirection[1])+"),\r\n";
					str += "controlPoint2: CGPoint(x: "+String(activePathPoints[k].rightDirection[0])+", y: "+String(activePathPoints[k].rightDirection[1])+"));\n";
//					str += activePath.name+".addCurveToPoint(CGPoint(x: "+String(activePathPoints[k].anchor[0])+", y: "+String(height-activePathPoints[k].anchor[1])+"),\r\n";
//					str += "controlPoint1: CGPoint(x: "+String(activePathPoints[k-1].leftDirection[0])+", y: "+String(height-activePathPoints[k-1].leftDirection[1])+"),\r\n";
//					str += "controlPoint2: CGPoint(x: "+String(activePathPoints[k].rightDirection[0])+", y: "+String(height-activePathPoints[k].rightDirection[1])+"));\r\n";
				}
			}
				str += activePath.name+".addCurve(to: CGPoint(x: "+String(activePathPoints[0].anchor[0])+", y: "+String(activePathPoints[0].anchor[1])+"),\r\n";
				str += "controlPoint1: CGPoint(x: "+String(activePathPoints[activePath.subPathItems[j].pathPoints.length-1].leftDirection[0])+", y: "+String(activePathPoints[activePath.subPathItems[j].pathPoints.length-1].leftDirection[1])+"),\r\n";
				str += "controlPoint2: CGPoint(x: "+String(activePathPoints[0].rightDirection[0])+", y: "+String(activePathPoints[0].rightDirection[1])+"));\r\n";
		}
	}
	
	str += activePath.name+".close();\r\n"
	str += "//UIColor.black.setStroke();\r\n"
	str += "//"+activePath.name+".lineWidth = 2.0;\r\n"
	str += "//"+activePath.name+".stroke();\r\n"
	str += "//UIColor.red.setFill();\r\n"
	str += "//"+activePath.name+".fill();\r\n"
 
	txtLayer = activeDocument.artLayers.add();
	txtLayer.kind = LayerKind.TEXT;
	txtLayer.textItem.kind = TextType.PARAGRAPHTEXT;
	txtLayer.textItem.contents = str; //文字列
	txtLayer.textItem.position = Array(10, 30); //位置 Array(x, y)
	txtLayer.textItem.width = "100 px";
	txtLayer.textItem.height = "100 px";
	txtLayer.textItem.size = 3; //フォントサイズ
 
	//alert(str);
}

前回と同じくPlaygroundでみてみます。
inspectorからPlatformをiOSに変更して一旦開き直さないと「UIKitなんてモジュールはないよ!」って怒られるみたいです。

コメントアウトされてる最後の数行をいじれば完成。

import UIKit
import XCPlayground

class Shape : UIView {
    override func draw(_ rect: CGRect) {
        //Photoshopからコピペ
    }
}

let shape : Shape = Shape(frame: CGRect(x:0, y:0, width:427, height:438));
shape.backgroundColor = UIColor.white;
shape.draw(shape.frame)
XCPlaygroundPage.currentPage.liveView = shape

無事出力できました。


カテゴリ: その他. Bookmark the permalink.

コメントを残す

メールアドレスが公開されることはありません。