How to add multiple line text in UIActionSheet

2.5k Views Asked by At

enter image description here

How to add multi line text with custom font in UIActionSheet swift.I have tried \n.but this is not working.is this possible or not.

Here is the my code.

    alert.addAction(UIAlertAction(title: "line 1.\n %C line 2,\n %C line", style: .default , handler:{ (UIAlertAction)in
        print("User click Approve button")
    }))

    alert.addAction(UIAlertAction(title: "Edit", style: .default , handler:{ (UIAlertAction)in
        print("User click Edit button")
    }))

    alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in
        print("User click Delete button")
    }))

    alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler:{ (UIAlertAction)in
        print("User click Dismiss button")
    }))

    self.present(alert, animated: true, completion: {
        print("completion block")
    })
2

There are 2 best solutions below

2
chirag90 On

Try the below code.

let optionMenu = UIAlertController(title: "Choose Class", message: "", preferredStyle: .actionSheet)
let course1 = UIAlertAction(title: "Computer Science(1st year) \n Digital Electronics", style: .default)
let course2 = UIAlertAction(title: "Computer Science(2nd year) \n Digital Electronics", style: .default)
let cancel = UIAlertAction(title: "Cancel", style: .cancel)
optionMenu.addAction(course1)
optionMenu.addAction(course2)
optionMenu.addAction(cancel)
self.present(optionMenu, animated: true, completion: nil)

// Setting up the number of lines and doing a word wrapping        
UILabel.appearance(whenContainedInInstancesOf:[UIAlertController.self]).numberOfLines = 2
UILabel.appearance(whenContainedInInstancesOf:[UIAlertController.self]).lineBreakMode = .byWordWrapping

Output

Output Image

6
Capella On

I'm showing the 2 text values in UIAlertAction. To use multiple fonts, colors, and other attributes, we have to use attributedText property. I used the following code.

UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 0

let actionSheetController = UIAlertController(title: nil, message: "Call", preferredStyle: .actionSheet)
actionSheetController.view.tintColor = UIColor.red

for key in phoneNumbers.keys {
    let phoneNumber = phoneNumbers[key] ?? ""
    let actionTitle = "\(key)\n\(phoneNumber)"
    let phoneNumberAction = UIAlertAction(title: actionTitle, style: .default) { action -> Void in
        self.makeCall(phoneNumber)
    }
    actionSheetController.addAction(phoneNumberAction)

    let attributedText = NSMutableAttributedString(string: actionTitle)

    let range = NSRange(location: key.count, length: attributedText.length - key.count)

    attributedText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.gray, range: range)

    guard let label = (phoneNumberAction.value(forKey: "__representer")as? NSObject)?.value(forKey: "label") as? UILabel else { return }
    label.attributedText = attributedText
}

let cancelAction = UIAlertAction(title: LOCALSTR("Cancel"), style: .cancel) { action -> Void in

}
actionSheetController.addAction(cancelAction)

present(actionSheetController, animated: true, completion: nil)

enter image description here