Sketch
Sketch copied to clipboard
Add Text feature
Hello a great new feature can be add text to a specific place (touched)
@Brujo2018 Oh, I certainly want that function. I'll think about it.
@daihase Can you please add text to specific position feature ASAP. Thanks for sharing the this POD file.
@lakshmanbob I'll try my best!
@daihase Please add a function of vertical text drawing also.
@bj1024 It seems to be difficult... I have to do it from where I display characters on the screen first.
@daihase as per my requirement, User click on any where in the "Sketch view" and text using the keyboard. I think horizontal text adding is enough for my requirement. if possible can you please work on it. thanks...
@daihase Can be add a text to specific touch position.like any where in the "Sketch view" add the horizontal or vertical text ..
@daihase Can you add the new feature of text where ever click on the sketch view Thank you
@daihase can be add a text to specific touch position
@daihase , Thanks for working on Text adding future, May i know when we are excepting the code. Because we are eagerly waiting for this future adding in my project. Thanks.
@daihase, Can you please update Add to text future on this sketch POD. I am eagerly waiting for your response. Once again thanks working on this enhancement.
@daihase can you please check this code. I think it is easy to add the text to image. `func textToImage(drawText text: String, inImage image: UIImage, atPoint point: CGPoint) -> UIImage { let textColor = UIColor.white let textFont = UIFont(name: "Helvetica Bold", size: 12)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
let textFontAttributes = [
NSAttributedStringKey.font: textFont,
NSAttributedStringKey.foregroundColor: textColor,
] as [NSAttributedStringKey : 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!
}`
if possible can you please check this below url, You will get the better code for this add to text for this sketchview https://stackoverflow.com/questions/28906914/how-do-i-add-text-to-an-image-in-ios-swift
Thanks
@lakshmanbob I'm sorry for the late reply. Actually, I'm currently busy with work and I cannot develop sketch's new functions. I looked at the reference code you posted. It’s possible to capture and render text, perhaps like a stamp.
I'm also thinking about the solution itself in another way, so I'd like to respond if I can do the time.
@daihase I'm crate TextTool, when you tap on screen text "Hello" draw and you can move left/right and top/bottom
class TextTool: SketchTool {
var lineWidth: CGFloat
var lineColor: UIColor
var lineAlpha: CGFloat
var firstPoint: CGPoint
var lastPoint: CGPoint
init() {
lineWidth = 1.0
lineAlpha = 1.0
lineColor = .blue
firstPoint = CGPoint(x: 0, y: 0)
lastPoint = CGPoint(x: 0, y: 0)
}
internal func setInitialPoint(_ firstPoint: CGPoint) {
self.firstPoint = firstPoint
}
internal func moveFromPoint(_ startPoint: CGPoint, toPoint endPoint: CGPoint) {
self.lastPoint = endPoint
}
internal func draw() {
let context: CGContext = UIGraphicsGetCurrentContext()!
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let attributes = [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 22.0),
NSAttributedString.Key.foregroundColor: lineColor
]
let myText = "HELLO"
let attributedString = NSAttributedString(string: myText, attributes: attributes)
var txtPointX = lastPoint.x
var txtPointY = lastPoint.y
let stringRect = CGRect(x: txtPointX, y: txtPointY, width: 100, height: 100)
attributedString.draw(in: stringRect)
context.saveGState()
attributedString.draw(in: stringRect)
context.restoreGState()
context.strokePath()
}
func angleWithFirstPoint(first: CGPoint, second: CGPoint) -> Float {
let dx: CGFloat = second.x - first.x
let dy: CGFloat = second.y - first.y
let angle = atan2f(Float(dy), Float(dx))
return angle
}
func pointWithAngle(angle: CGFloat, distance: CGFloat) -> CGPoint {
let x = Float(distance) * cosf(Float(angle))
let y = Float(distance) * sinf(Float(angle))
return CGPoint(x: CGFloat(x), y: CGFloat(y))
}
}
@ibrahimsu Thanks! I'll use this code as a reference when I implement text feature.
@daihase did you implement the text feature? I am using your library and it is great.