Skip to content

Instantly share code, notes, and snippets.

@SachinR90
Last active February 5, 2020 10:22
Show Gist options
  • Save SachinR90/c1e6023088d5138c454ed5695bdacbe4 to your computer and use it in GitHub Desktop.
Save SachinR90/c1e6023088d5138c454ed5695bdacbe4 to your computer and use it in GitHub Desktop.
iOS - Text with Pattern
  • Add image named "pattern" to assets.
  • Enable slicing, to make the pattern/image repeatable.
    1. Click the image
    2. Select 2x
    3. Click 'Show Slicing'
    4. Select option with cross icon
    5. Drag all lines to 4 corners.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let pattern = UIImage(named: "pattern")
imageView.image = pattern
// Create the CATextLayer instance.
let textLayer = CATextLayer()
textLayer.frame = imageView.bounds
textLayer.rasterizationScale = UIScreen.main.scale
textLayer.contentsScale = UIScreen.main.scale
textLayer.fontSize = 60
textLayer.string = "Sachin\nRao";
textLayer.alignmentMode = .center
imageView.layer.mask = textLayer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment