Swift iOS – How to keep line breaks when translating text with ML Kit on iOS

ML Kit translate is a powerful language translation tool, but when translating a passage of text containing line breaks, the returned translated text has the line breaks stripped out.

I’ve tried a bunch of different techniques to maintain line breaks through the translation, and here’s the best so far:

1. Replace line breaks with unicode $ value in text before translating

textToTranslate = textToTranslate.replacingOccurrences(of: "\n", with: "\u{24}")

2. Replace unicode $ value back to line break in returned text after translating

translatedText = translatedText.replacingOccurrences(of: "\u{24}", with: "\n")

The returned translated text now maintains the original line breaks, when displayed in a UITextView for example.

Xcode – How to install Pods (Cocoa Pods)

Xcode + developer tools must be installed on your Mac.

1. Open Terminal and run the following command to install Cocoa Pods for Xcode:

sudo gem install cocoapods

Enter your Mac password. The required packages will be downloaded and installed.

2. In Terminal, navigate to the root folder of your Xcode project i.e. the folder containing MyProject.xcodeproj

cd /Users/Robbamforth/Projects/MyProject

3. Run the following command to create an empty initial PodFile in your Xcode project folder.

pod init

Now you have a PodFile in your projects root directory.

4. Open the with TextEdit (or other editor), and add your pod details under the ‘#Pods for MyProject’ tag

  # Pods for MyProject
  pod 'GoogleMLKit/Translate', '3.1.0'

5. Install the pod using the following command in Terminal:

pod install

This will download the libraries required for your pod, install the necessary components, and create a new workspace named MyProject.xcworkspace.

From now on, open the .xcworkspace file from finder to work in xcode. If you open the .xcodeproj file, you will not be able to compile or run your project with cocoa pods references.

You can add additional pods to the PodFile in step 4, then repeat step 5 to install.

Swift iOS – How to detect Orientation change (landscape, portrait)

Add Notification Center observer in ViewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(orientationChange), name: UIDevice.orientationDidChangeNotification, object: nil)

Add methid to call when device rotated:

@objc func orientationChange() {
    //TODO
}

Finally, remove observer when finished with view controller (e.g. add to viewWillDisappear() method):

NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)

Swift iOS – Check model, check if iPad, check if iPhone, Other

How to check device type and run specific code for iPad or iPhone.

witch UIDevice.current.userInterfaceIdiom {
        case .phone:
            print("phone")
        case .pad:
            print("ipad")
        case .unspecified:
            print("unspecified")
        case .tv:
            print("tv")
        case .carPlay:
            print("carPlay")
        case .mac:
            print("mac")
        @unknown default:
            print("unknown")
 }

In this example the presented ViewController will display fullscreen for iPad, else use default presentation style for other devices, i.e. iPhone:

let popover = MyViewController()

switch UIDevice.current.userInterfaceIdiom {
        case .phone:
            print("phone")
        case .pad:
            print("ipad")
            popover.modalPresentationStyle = .fullScreen
        case .unspecified:
            print("unspecified")
        case .tv:
            print("tv")
        case .carPlay:
            print("carPlay")
        case .mac:
            print("mac")
        @unknown default:
            print("unknown")
 }

present(popover, animated: true)

Swift iOS – Add pinch gesture to UIImageView (imageview)

Create gesture and add it to your imageview in ViewDidLoad():

let standardPinch = UIPinchGestureRecognizer(target: self, action: #selector(pinch))
standardPinch.delegate = self
myImageView.isUserInteractionEnabled = true
myImageView.addGestureRecognizer(standardPinch)

Add pinch() method to handle resizing of UIImageView:

@objc func pinch(_ sender: UIPinchGestureRecognizer){
        guard let targetView = sender.view else {return}
        targetView.transform = targetView.transform.scaledBy(x: sender.scale, y: sender.scale)
        sender.scale = 1
 }