How to add multiple line text in UIActionSheet
Asked Answered
D

2

5

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")
    })
Dombrowski answered 5/9, 2019 at 13:35 Comment(0)
Z
3

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

Zebapda answered 5/9, 2019 at 13:58 Comment(2)
Thanks chirag for your valuable reply.this is work for multiple line can you please suggest me how to add multiple font..Dombrowski
Okay I will add.Dombrowski
C
3

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

Cornel answered 27/4, 2020 at 18:41 Comment(6)
Btw, I see another problem. #61216917Cornel
unable to access label propertyBrickey
@KedarSukerkar You mean guard let label = (phoneNumberAction.value(forKey: "__representer")as? NSObject)?.value(forKey: "label") as? UILabel else { return } does not work or label.attributedText = attributedText?Cornel
Yes.....sorry that was mistake from my side.....i was accessing label before presenting alert. Thats why was getting nil... Also, i wasn't able to change the font property..any solutions for it.Brickey
@KedarSukerkar It's easy. You can just add the normal front.Cornel
yeah, i had added similar to this..attributedText.addAttribute(NSAttributedString.Key.font, value: myFont, range: range)....but there was no effectBrickey

© 2022 - 2024 — McMap. All rights reserved.