Information on inserting shadow characters into UIImage

Asked 2 years ago, Updated 2 years ago, 90 views

Thank you for your help.
I'm working on an iOS app that has the action of writing characters directly into UIImage, but when I tried to put a shadow on that character, it didn't work well.
The option to set the background color of the string works.

How can I resolve the above issues?
Thank you for your cooperation.

The development environment is

  • Xcode 8.3.3

  • Swift 3.1

  • iPhone 6s (iOS 10.1.1), iPhone 6 Plus (10.2.1)

Xcode 8.3.3

Swift 3.1

iPhone 6s (iOS 10.1.1), iPhone 6 Plus (10.2.1)

That's it.

The source code, arguments, and return values are as follows.

Arguments

  • text:String string to draw.

  • image:UIImage Draw a string on this image

text —String string to draw.

image:UIImage Draw a string on this image

Return Value

  • newImage The purpose is to substitute a character-drawing image here.

Source

let font=UIFont.boldSystemFont(ofSize:20)

    let imageWidth = image.size.width
    let imageHeight=image.size.height

    let rect = CGRect (x:0, y:0, width: imageWidth, height: imageHeight)

    UIGraphicsBeginImageContextWithOptions (image.size, false, 0)

    image.draw(in:rect)

    let textRect = CGRect (x:0, y:0, width: imageWidth, height: imageHeight)
    let textStyle=NSMutableParagramStyle.default.mutableCopy() as!NSMutableParagramStyle

    let shadow=NSShadow()
    show.shadowColor=UIColor.red
    shadow.shadowOffset = CGSize (width:50, height:30)
    shadow.shadowBlurRadius=1

    let textFontAttributes = [
        NSFontAttributeName:font,
        NSForegroundColorAttributeName:UIColor.black,
        NSParographStyleAttributeName:textStyle,
        NSShadowAttributeName:shadow
    ]

    text.draw (in:textRect, with Attributes:textFontAttributes)

    let newImage=UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext()

swift ios swift3 uiimage ios10

2022-09-30 17:35

1 Answers

I'm not sure why, but I was able to see that if the conditions listed in the comment include full-width characters (please include these conditions in the body of the question as well as in the comments) no longer draw shadows (on Playground in Xcode 8.3.3).

(By the way, in Xcode9GM seed, just matching your code to the correction in Swift4 has also shadowed strings that contain Japanese.)

Of course, there is no description in the document that certain attributes will be disabled under certain conditions, so it would be better to simply report Apple as a bug.

Now, What is the solution to the above problem? seems to be the subject of your question, so I'll post a code that says it worked in my environment.

let font=UIFont.boldSystemFont(ofSize:20)

let imageWidth = image.size.width
let imageHeight=image.size.height

let rect = CGRect (x:0, y:0, width: imageWidth, height: imageHeight)

UIGraphicsBeginImageContextWithOptions (image.size, false, 0)

image.draw(in:rect)

let textRect = CGRect (x:0, y:0, width: imageWidth, height: imageHeight)
let textStyle=NSMutableParagramStyle.default.mutableCopy() as!NSMutableParagramStyle

let shadow=NSShadow()
show.shadowColor=UIColor.red
shadow.shadowOffset = CGSize (width:5, height:3)
shadow.shadowBlurRadius=3

let textFontAttributes = [
    NSFontAttributeName:font,
    NSForegroundColorAttributeName:UIColor.black,
    NSParographStyleAttributeName:textStyle,
    NSShadowAttributeName:shadow
]

let attrText=NSAttributedString (string:text, attributes:textFontAttributes)
attrText.draw (in:textRect)

let newImage=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext()

The difference from your code is that instead of calling the draw(in:withAttributes:) method of String, you first create NSAttributedString and then call that draw(in:) method.

I haven't been able to investigate whether it's a build or a running environment issue, but what will happen in that environment?Try it.


2022-09-30 17:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.