绘制了核芯显卡文本斯威夫特3
背景:我吸取了核芯显卡一个UIImageView。我想最终在核心图形绘制上绘制一个文本字符串。绘制了核芯显卡文本斯威夫特3
This (hackingwithswift.com)方法我用文字绘制到的图像,我的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()
}
}
不工作。绘制文本字符串完全擦除栏。如果将条形图代码移动到字符串绘图代码下,那么两者都是可见的,但当然条形图会在字符串上显示。
夫特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)
这只是从我的代码不同之处在于你删除的UIGraphicsBeginImageContext( self.imageView.frame.size)。您的代码在我的情况下不会绘制任何文字。 –
把drawMyText功能UIView子类和drawRect中函数中调用它,请让我知道如果你需要进一步的帮助 – Devesh
我的问题是绘制文本删除了我以前画的画。在我上面的例子中,文本将出现,但它完全擦除了我以前绘制的线条。据我所知,你唯一的建议是我直接写文本而不是一点,这对我来说没有任何帮助。 –
迅速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)
你能在CATextLayer创建文本,并将其添加到图像视图? – dfd
你说的“我吸取了核芯显卡一个UIImageView”,以及在什么类,你运行你的代码是什么意思? –
我添加了一些contextualizing代码,希望它有帮助。我认为答案将存在于核心文本中,我将尝试自己寻找解决方案。比如像[链接](http://stackoverflow.com/questions/27052665/swift-playground-core-graphics-core-text-custom-view)看好 –