0

I'm writing an app that shows a list of items in a table view. You can tap each item in the table view to get a popup (via a standard UIAlertController) to get more information about that item.

One thing I'd like to include is ratings on a couple of different scales — so for example, if the rating is 3/5 cars, it would say Car Rating: . But I feel it would be better if the user knew this was a rating out of 5, which I could accomplish by adding two more "grayed-out" car emojis (i.e., alpha = 0.3), similar to the way Amazon would show 4 filled stars and 1 empty star for an item with a 4-star rating out of 5.

Then the question is: is there any way to set the alpha value of some of the text in the alert to 0.3 while the rest is 1 (I've done my research and can't find anything, so it looks like no)? If not, any recommended workarounds? Thanks!

Viktor
  • 33
  • 6

1 Answers1

0

If you want to apply different alpha values for text, you could make use of the attributed text as follows,

Instead of assigning text value assign the text to attributedText value as in the following example,

// MARK: - Different text style in alertController

@IBAction func buttonPressed(_ sender: Any) {
    // Sample attributed text
    let attributedText = applyDifferentStyle(forString: "Hello", inString: "Hello world")

    // Alert controller
    let alertController = UIAlertController(title: "Attributed texts example", message: nil, preferredStyle: .alert)

    // OK button in alert
    let okButton = UIAlertAction(title: "OK", style: .default, handler: nil)
    alertController.addAction(okButton)

    // Assign attributed text
    alertController.setValue(attributedText, forKey: "attributedMessage")

    // Show alert
    present(alertController, animated: true, completion: nil)
}

func applyDifferentStyle(forString: String, inString: String) -> NSMutableAttributedString {
    // Let your color be as follows with the alpha value as you wanted
    let greenColour = UIColor(red: 10/255, green: 190/255, blue: 50/255, alpha: 0.7)
    let redColor = UIColor.red

    // create the attribute as you want
    let customAttribute = [NSAttributedStringKey.foregroundColor : greenColour];

    // Other normal text attribute
    let normalTextAttributes = [NSAttributedStringKey.foregroundColor : redColor]

    let attString:NSMutableAttributedString = NSMutableAttributedString(string: inString
        , attributes: normalTextAttributes)

    // Apply attribute to texts
    let stringRange = (inString as NSString).range(of: forString)
    attString.setAttributes(customAttribute, range: stringRange)

    return attString
}

enter image description here

Simple demo project reference:
https://github.com/bharath-dev/StackoverflowSolutions/tree/master/AttributedAlertText

Bharath
  • 2,064
  • 1
  • 14
  • 39
  • I'm writing the app in Objective-C, not Swift, and am having a hard time figuring out the right syntax for your line `let normalTextAttributes = [NSForegroundColorAttributeName: redColor]`. How would one translate this to Objc? (I'll post my full Objc translation once I figure it out so others can have it, too!) – Viktor Oct 03 '17 at 17:39
  • Try NSDictionary * normalTextAttributes = @{NSForegroundColorAttributeName : redColor}; – Bharath Oct 03 '17 at 17:55
  • That worked, but UIAlertController won't take an NSMutableAttributedString for the message (crashes if you try to pass it in) – Viktor Oct 03 '17 at 19:58
  • @Viktor: Oh, For alert controller you can use the following syntax for updating the attributed text , alertController.setValue(YOUR_ATTRIBUTED_STRING, forKey: "attributedMessage") – Bharath Oct 03 '17 at 20:38
  • If assignment is the issue, please refer the following link it will give you a clear idea of customising UIAlertController, https://stackoverflow.com/questions/26460706/uialertcontroller-custom-font-size-color – Bharath Oct 03 '17 at 20:39
  • Also crashes: `*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteAttributedString rangeOfCharacterFromSet:]: unrecognized selector sent` – Viktor Oct 04 '17 at 14:40
  • @Viktor: I have added the entire code and the above code works fine for me. Make use of it. – Bharath Oct 04 '17 at 17:03
  • I don't recommend to mess with `UIAlertController`. It's using private attributes, and it may change in next iOS release and break all your code, or Apple may decide that they don't allow it anymore (it seems that they let it for now). – Larme Oct 04 '17 at 17:22
  • @Larme: Ya, Apple could decide that, Ok in that case what could be a better option for this question ? I think if not AlertController he has to go with custom views right ? – Bharath Oct 04 '17 at 17:28
  • Use a custom alert view of your own, or you can find some already working on CocoaPods/CocoaControls/GitHub. – Larme Oct 04 '17 at 17:29
  • @Viktor: Have the point conveyed by Larme in consideration and better choose a custom alert view library(check this out : https://github.com/bharath-dev/awesome-ios#alert--action-sheet) – Bharath Oct 04 '17 at 17:34