Skip to content

Commit

Permalink
feat(drawText): add support for text alignment (left, right)
Browse files Browse the repository at this point in the history
  • Loading branch information
guhungry committed Oct 23, 2024
1 parent 15c0344 commit 91c4072
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Sources/WCPhotoManipulator/Models/TextStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import UIKit
let color: UIColor
/// The font of the text.
let font: UIFont
/// The alignment of the text. Default value is left.
public var alignment: NSTextAlignment = .left
/// The thickness of the text. Default value is 0.
let thickness: CGFloat
/// The rotation angle of the text in degrees. Default value is 0.
Expand Down Expand Up @@ -135,6 +137,7 @@ import UIKit
TextStyle:
color: \(color)
font: \(font)
alignment: \(alignment)
thickness: \(thickness)
rotation: \(rotation)
shadowRadius: \(shadowRadius)
Expand Down
24 changes: 17 additions & 7 deletions Sources/WCPhotoManipulator/UIImage+PhotoManipulator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ public extension UIImage {

// Text
@objc func drawText(_ text: String, position: CGPoint, style: TextStyle, scale: CGFloat) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.size, false, scale)
if let context = UIGraphicsGetCurrentContext() {
context.translateBy(x: position.x, y: position.y)
context.rotate (by: -style.rotation * CGFloat.pi / 180.0) //45˚
context.translateBy(x: -position.x, y: -position.y)
}
let paragraphStyle = NSMutableParagraphStyle.init()
paragraphStyle.alignment = style.alignment
var textStyles: [NSAttributedString.Key: Any] = [
.font: style.font,
.foregroundColor: style.color,
.paragraphStyle: paragraphStyle
]
if (style.thickness > 0) {
textStyles[.strokeColor] = style.color;
Expand All @@ -63,8 +60,21 @@ public extension UIImage {
if (style.shadowRadius > 0 && style.shadowColor != nil) {
textStyles[.shadow] = style.shadow;
}
let textSize = (text as NSString).size(withAttributes: textStyles)

var adjustedPosition = position
if (style.alignment == .right) {
adjustedPosition = CGPoint(x: position.x - textSize.width, y: position.y)
}

UIGraphicsBeginImageContextWithOptions(self.size, false, scale)
if let context = UIGraphicsGetCurrentContext() {
context.translateBy(x: position.x, y: position.y)
context.rotate (by: -style.rotation * CGFloat.pi / 180.0) //45˚
context.translateBy(x: -position.x, y: -position.y)
}

(text as NSString).draw(at: position, withAttributes: textStyles)
(text as NSString).draw(in: CGRect(origin: adjustedPosition, size: textSize), withAttributes: textStyles)

let rotatedImageWithText = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ class UIImage_PhotoManipulatorSwiftTests: XCTestCase {
XCTAssertNotNil(image)
XCTAssertEqual(image.size, CGSize(width: 800, height: 530))
}

func testDrawText_WhenAlignRight_ShouldReturnCorrectly() throws {
image = UIImage.init(namedTest: "background.jpg")
XCTAssertNotNil(image)

let style = TextStyle(color: .blue, size: 42, thickness: 5, rotation: -30)
style.alignment = .right
image = image.drawText("Test Text To Align Right Draw", position: CGPoint(x: 400, y: 60), style: style, scale: 2)
image = image.drawText("Right", position: CGPoint(x: 400, y: 100), style: style, scale: 2)
image = image.drawText("Right Multiple\nLine", position: CGPoint(x: 400, y: 140), style: style, scale: 2)
style.alignment = .left
image = image.drawText("Test Text To Align Left Draw", position: CGPoint(x: 400, y: 60), style: style, scale: 2)
image = image.drawText("Left", position: CGPoint(x: 400, y: 100), style: style, scale: 2)
image = image.drawText("Left Multiple\nLine", position: CGPoint(x: 400, y: 140), style: style, scale: 2)
XCTAssertNotNil(image)
XCTAssertEqual(image.size, CGSize(width: 800, height: 530))
}

// Overlay Image
func testOverlayImage_WhenNoScale_ShouldReturnCorrectly() throws {
Expand Down

0 comments on commit 91c4072

Please sign in to comment.