Merge branch 'charlesmchen/textLayerMeasurement'

pull/2/head
Matthew Chen 6 years ago
commit 13f24002c6

@ -500,8 +500,14 @@ public class ImageEditorCanvasView: UIView {
// using the image width as reference. // using the image width as reference.
let fontSize = item.font.pointSize * imageFrame.size.width / item.fontReferenceImageWidth let fontSize = item.font.pointSize * imageFrame.size.width / item.fontReferenceImageWidth
let text = item.text.filterForDisplay ?? ""
let attributedString = NSAttributedString(string: text,
attributes: [
NSAttributedStringKey.font: item.font.withSize(fontSize),
NSAttributedStringKey.foregroundColor: item.color.color
])
let layer = EditorTextLayer(itemId: item.itemId) let layer = EditorTextLayer(itemId: item.itemId)
layer.string = item.text layer.string = attributedString
layer.foregroundColor = item.color.cgColor layer.foregroundColor = item.color.cgColor
layer.font = CGFont(item.font.fontName as CFString) layer.font = CGFont(item.font.fontName as CFString)
layer.fontSize = fontSize layer.fontSize = fontSize
@ -523,13 +529,10 @@ public class ImageEditorCanvasView: UIView {
let maxSize = CGSize(width: maxWidth, height: CGFloat.greatestFiniteMagnitude) let maxSize = CGSize(width: maxWidth, height: CGFloat.greatestFiniteMagnitude)
// TODO: Is there a more accurate way to measure text in a CATextLayer? // TODO: Is there a more accurate way to measure text in a CATextLayer?
// CoreText? // CoreText?
let textBounds = (item.text as NSString).boundingRect(with: maxSize, let textBounds = attributedString.boundingRect(with: maxSize,
options: [ options: [
.usesLineFragmentOrigin, .usesLineFragmentOrigin,
.usesFontLeading .usesFontLeading
],
attributes: [
.font: item.font.withSize(fontSize)
], ],
context: nil) context: nil)
// The text item's center is specified in "image unit" coordinates, but // The text item's center is specified in "image unit" coordinates, but
@ -537,7 +540,7 @@ public class ImageEditorCanvasView: UIView {
// is the bounds of the image specified in "canvas" coordinates, // is the bounds of the image specified in "canvas" coordinates,
// so to transform we can simply convert from image frame units. // so to transform we can simply convert from image frame units.
let centerInCanvas = item.unitCenter.fromUnitCoordinates(viewBounds: imageFrame) let centerInCanvas = item.unitCenter.fromUnitCoordinates(viewBounds: imageFrame)
let layerSize = CGSizeCeil(textBounds.size) let layerSize = textBounds.size.ceil
layer.frame = CGRect(origin: CGPoint(x: centerInCanvas.x - layerSize.width * 0.5, layer.frame = CGRect(origin: CGPoint(x: centerInCanvas.x - layerSize.width * 0.5,
y: centerInCanvas.y - layerSize.height * 0.5), y: centerInCanvas.y - layerSize.height * 0.5),
size: layerSize) size: layerSize)

@ -245,6 +245,10 @@ public extension CGSize {
var asPoint: CGPoint { var asPoint: CGPoint {
return CGPoint(x: width, y: height) return CGPoint(x: width, y: height)
} }
var ceil: CGSize {
return CGSizeCeil(self)
}
} }
public extension CGRect { public extension CGRect {

Loading…
Cancel
Save