9

I'm trying to change the title fontSize in an UIAlertController, but I can't manage how to set my NSMutableAttributedString to the title-property.

So for I've been creating the NSMutableAttributedString with the following code:

let title = NSMutableAttributedString(string: user.fullName)
let range = NSRange(location: 0, length: title.length)
title.addAttribute(NSAttributedStringKey.font, value: UIFont.TextStyle.largeTitle, range: range)

Now the tricky part for me is how to figure out how to set the new title to the UIAlertController, because it's expecting a String? value.

I looked around and found out that I should probably create a UILabel within the completion block when presenting the UIAlertController. But how do I override the title-property in the UIAlertController with my own custom UILabel?

present(myUIAlertController, animated: true) {
    // Creating the UILabel depending on string length
    // Set the NSMutableAttributedString value to the custom UILabel and override title property.
}

Or maybe there's even an easier way to solve this?


My goal is to have like the image below:

UIAlertViewController with large title

Community
  • 1
  • 1
Jacob Ahlberg
  • 2,352
  • 6
  • 22
  • 44
  • 1
    Can you use a `UIView` or `UITableView` like action sheet? It can be easily customize according to your requirement. Even [custom action sheets](https://www.cocoacontrols.com/search?q=actionsheet) are available. – TheTiger Aug 30 '18 at 11:34
  • here is the answer https://stackoverflow.com/a/36136625/8417137 in this string "alertController.setValue(attributedString, forKey: "attributedTitle")" – Alex Kolovatov Aug 30 '18 at 11:35
  • 3
    Making any change in any inbuilt control without using the inbuilt functions is not a good idea. It can be damage in future if  makes any change in that API. So better to make your own custom control. – TheTiger Aug 30 '18 at 11:39
  • Check this one https://github.com/urbn/URBNAlert – Khushbu Desai Aug 30 '18 at 11:50

2 Answers2

14

You can make title and message of UIAlertController attributed by using this code. You can customize as per your need. You can see the result in the image. I am not sure you can put it on Appstore.

func showAlert() {
  let alert = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)        
  let titleAttributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 25)!, NSAttributedStringKey.foregroundColor: UIColor.black]
  let titleString = NSAttributedString(string: "Name Last name", attributes: titleAttributes)     
  let messageAttributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 17)!, NSAttributedStringKey.foregroundColor: UIColor.red]
  let messageString = NSAttributedString(string: "Company name", attributes: messageAttributes)
  alert.setValue(titleString, forKey: "attributedTitle")
  alert.setValue(messageString, forKey: "attributedMessage")
  let labelAction = UIAlertAction(title: "Label", style: .default, handler: nil)
  let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: nil)
  let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
  alert.addAction(labelAction)
  alert.addAction(deleteAction)
  alert.addAction(cancelAction)
  self.navigationController?.present(alert, animated: true, completion: nil)

}

enter image description here

Sunny
  • 821
  • 6
  • 17
0

There is no way except using of private API.

I can suggest you to make your AlertViewController with properties that you want to customize.

ezaji
  • 304
  • 2
  • 9
  • 3
    Private APIs should be avoided, since apps might be rejected by Apple upon release. – Giovanni Palusa Aug 30 '18 at 11:47
  • 1
    Yes, I fully agree with you. Using of private API is the reason to reject your app by Apple. But you can use it for educational purpose or something like that. – ezaji Aug 30 '18 at 11:49