Actionsheet text is always grey (NSAttributedString.Key doesn't working) - ios

I'm using a custom actionsheet with some text displaying. With XCode 11.3 I can't use NSAttributedString.Key.foreground. It used to work before the update, but now I can't find a solution.
Screengrab with example of my problem
let attributedMessageText = NSMutableAttributedString(
string: description,
attributes: [
NSAttributedString.Key.paragraphStyle: paragraphStyle,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13.0),
NSAttributedString.Key.foregroundColor: UIColor.red
]
)
If I'm trying to change a background color, it turns gray too.
Maybe someone already faced this problem?

It's never a good idea to use private API as they might change without notice. I guess you use something like this:
actionSheet.setValue(message, forKey: "attributedTitle")
that's why this doesn't work on iOS13.3. Unfortunately there is no legal way to change those color with the public APIs. But you're not alone or here is good recommendation

Related

How to make a text range underlined and bold in **Swift 4** using OpenSans custom font

This is driving me nuts, I cant find a simple straightforward answer anywhere on how to make a text range bold, underlined with a custom font in Swift 4.
Every page I go to has a portion of the information but it is sending me round in spirals. I find the underline method for Swift 3, doesn't include custom font, the next one says to use the bold font custom version in Swift 4, etc but doesn't include underline. Very frustrating.
So. I have this text
Reset password email has been sent.
Didn’t get it? Send again
'Send again' needs to be bold and underlined. Its a complete string, in one UILabel.
Pretty sure the new method uses UIAttributedStringKey.
I know its pretty simple to be fair which is why I've found it frustrating and have lost my patience with it.
Oh, I'm currently on iOS 11, XCode 9.4.1. I am probably going to update in the next couple of days.
Thanks in advance
Edit
Hey dfd, thanks for your comments. This is basically the code I've got (useless in swift 4.0 although working in Swift 3.0 pretty much as I remember using the code perviously.
I've got to declare the font somewhere and also I haven't found code for the bold either but I do have some code for the range and the underline.
Cheers
var text = self.completeText.text
let textRange = NSMakeRange(50, (text?.count)!)
let attributedText = NSMutableAttributedString(string: text!)
let underlineAttribute = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
attributedText.addAttribute(underlineAttribute)
* Final Edit*
Heres working code.
let label = self.completeText
let labelString = self.completeText.text
let attributedText = NSMutableAttributedString(string: labelString!)
let rangeToUnderline = (labelString! as NSString).range(of: "Send again")
attributedText.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: rangeToUnderline)
I'm going to add the OpenSansBold custom font with these attrs and it should work ok.
Thanks Adrian, thanks for the suggestions everyone else. Much appreciated!
P.S I don't suppose anyone has got a decent reference for attributed strings on Swift 4.1? Would be nice.
If you're not seeing your Open Sans, that's a separate issue. To underline it, you'd make use of an NSMutableAttributedString (not NSAttributedString).
Here's how you'd underline a portion of your string:
let label = UILabel()
let labelString = "Didn’t get it? Send again"
let attributedText = NSMutableAttributedString(string: labelString)
let rangeToUnderline = (labelString as NSString).range(of: "Send again")
attributedText.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: rangeToUnderline)
As for bolding a portion of the string, here's how you'd do that. I'm too lazy to track down the font names, test it, etc., so
I'll leave it to you to fill in the bolding blank and the logic behind toggling between bold and non-bold.
On the label Attributed Inspector change the text from Plain to Attributed. Then you can edit the text and add underline and even change font to bold.

UIBarButtonItem tintColor issue while setting bold text on in accessibility

UIBarButtonItem tintColor is not getting change after setting Bold Text On in Settings -> Display & Brightness -> Bold Text.
i am facing this issue in iOS 11 & 12 both. haven't checked in previous versions.
The same question is already asked in apple developer forum but i didn't find any answer there.
https://forums.developer.apple.com/thread/89337
if some one have any work around to this please suggest me.
You can try to do it programmatically using NSAttributedString, I have never done it trough the storyboard, but programmatically using array of [NSAttributedString.Key.font: .systemFontOfSize(size: 25), NSAttributedString.Key.foregroundColor: .blue], etc., works like a charm.
One method that works for me is changing the Global Tint option on the storyboard, of course this is not the perfect solution.

Change Font for all UIAlertController in the app

How to change the font for all the UIAlertControllers in the app?
Is there any way like how we do it for other UIElements
Ex:
UINavigationBar.appearance().titleTextAttributes = [.font: UIFont.boldSystemFont(ofSize: 16)]
It's not possible. Also changing UIAlertViewController fonts, colors etc is private API. So no release at the AppStore
UIAlertController custom font, size, color

Custom Fonts Bug

TL;DR: Custom fonts couldn't be used programmatically before using them in a Storyboard/xib.
Note: I've checked out this, tried the answers and they didn't work. I've also made sure that they're in target membership.
I've noticed a strange bug while changing segment control title a custom font:
segmentedControl.titleTextAttributes = NSDictionary(objects: [UIFont.init(name: "Comfortaa-Regular",
size: UIFont.systemFontSize)!,
UIColor.white],
forKeys: [NSAttributedStringKey.font as NSCopying,
NSAttributedStringKey.foregroundColor as NSCopying]) as? [AnyHashable : Any]
It couldn't find the font, so unwrapping has failed. But the font could be seen in the Storyboard.
It's properly added to the project, here's the CopyBundle and InfoList:
So here's the catch; if I use it in the Storyboard, it is shown in the font families:
But if not, it's not shown -here I've changed to light and bold has disappeared-, and cannot be used programmatically.
I'm using Xcode Version 9.0 (9A235), and Swift 4. I've also checked OpenRadar and couldn't find a submission regarding this.
The mentioned font: Comfortaa
#EDUsta, I just tried with given font and its work ok, no issue in it, giving the step which i followed:
Add the Comfortaa-Bold.ttf, Comfortaa-Light.ttf, Comfortaa-Regular.ttf Font files to project.
2.Add the entries of Fonts in info.plist file
Make sure the font added in project target.
After it, apply the fonts on UILabel and UISegmentedControl using Swift code given below:
let fontFirst = UIFont(name: "Comfortaa-Bold", size: 16.0)!
segmentFont.setTitleTextAttributes([NSAttributedStringKey.font : fontFirst, NSAttributedStringKey.foregroundColor : UIColor.red],
for: .normal)
labelBold.font = UIFont(name: "Comfortaa-Bold", size: 16.0)
labelLight.font = UIFont(name: "Comfortaa-Light", size: 16.0)
labelRegular.font = UIFont(name: "Comfortaa-Regular", size: 16.0)
its work perfectly, you can check in below screenshot image:
For your reference i am adding the complete project here:
I have the same problem. I can't add exactly the same font family (Comfortaa) programatically - everytime it crashes, but once I add label in launchscreen and set font to Comfotaa-Bold, font loaded from code works fine and doesn't crash. So my solution is to add 3 labels with fonts such as - Comfortaa Bold, Comfortaa Regular, Comfortaa Light in launchscreen and set "hidden" flag on true. This way I'm able to use all of them programatically.

Changing UINavigationBar font in Swift

I have a UINavigationBar with a title in the middle. I have added a custom font ("Comic_Andy.ttf") to my app (I have checked info.plist to make sure it's listed, and I have checked the Copy Bundle Resources to make sure it has been added), and I would like the title of the UINavigationBar to be displayed in that font. From what I can gather it seems as though I'm supposed to use this in my ViewController:
myNavigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
I placed that method in the viewDidLoad function of the ViewController. I have also tried this in the didFinishLaunchingWithOptions function of the AppDelegate:
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
I am programming in Swift, in XCode 6 Beta 6. Many resources regarding this task have mentioned using a method called setTitleTextAttributes, which is nowhere to be seen. I can't figure it out for the life of me - I've probably spent close to 3 hours on it by now - I have checked every StackOverflow answer, every website, so please do not mark this as a duplicate. Many thanks in advance!
Instead of using myNavigationBar, try navigationController.navigationBar in the ViewController viewDidLoad function. It worked for me.
navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy", size: 22)]
If this doesn't work, try using the .ttf suffix.
navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Comic_Andy.ttf", size: 22)]
Good luck!
Can also be done for all NavigationBars with UINavigationBar.appearance().
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: Font.mediumFontName, size: 20)!]
Well, it took me the whole day, but I got it. My first problem was I didn't even have a NavigationController, so once I got that all sorted out this is what finally fixed it. Turns out you call the font like this: Comic Andy instead of Comic_Andy. EVEN THOUGH the font is referred to throughout the entire package as Comic_Andy, and the file is named Comic_Andy, you still use Comic Andy.

Resources