How to change UIDocumentInteractionController text color? - ios

My app has an option for a dark theme, and when that is the case, I do something like this:
UILabel.appearance().textColor = .white
The problem is, the app provides the ability to export some text via UIDocumentInteractionController. When I do presentPreview it displays white text on a white background, which makes the text completely invisible. There doesn't seem to be any way to change the text color or background color of the UIDocumentInteractionController.
I tried this
UILabel.appearance(whenContainedInInstancesOf: [UIDocumentInteractionController.self]).textColor = .black
but that just gives Cannot convert value of type 'UIDocumentInteractionController.Type' to expected element type 'UIAppearanceContainer.Type'
I've also tried setting UILabel.appearance().textColor = .black before I create the UIDocumentInteractionController, but that doesn't help either - the text still appears white.
What's extra annoying, this works to change the nav bar title color:
let navBarTitleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.black,
]
UINavigationBar.appearance().titleTextAttributes = navBarTitleTextAttributes
self.dic = UIDocumentInteractionController()
...
But the label appearance is not responsive.

Related

Search Bar Left Image Tint Change iOS for Dark Mode

Trying to get the left most image icon within the search bar to change color for Dark Mode in iOS 13
Ive tried making a call directly to the searchBar and changing its background color, changing the text color, and finally I've tried a couple different iterations of the code below.
searchBar.backgroundColor = .clear
searchBar.backgroundImage = UIImage(named: "magnifyingglass")
if #available(iOS 13, *) {
searchBar.textField?.attributedPlaceholder =
NSAttributedString(string: (" " + NSLocalizedString("search_actions", comment: "")),
attributes: [NSAttributedString.Key.foregroundColor: UIColor.separator,
NSAttributedString.Key.strokeColor: UIColor.separator])
}

Is there a way I can change the background color of the whole input bar in Message Kit?

I wish to change the background color of the input accessory bar to a reddish color. In the example code for MessageKit, there isn't any to change the background color. And running this piece of code doesn't work:
messageInputBar.backgroundColor = .red
or
messageInputBar.backgroundView.backgroundColor = .red
In order to color the different parts on the InputBarAccessoryView, use these snippets:
Coloring the content view:
messageInputBar.contentView.backgroundColor = .red
Coloring the text input view:
messageInputBar.inputTextView.backgroundColor = .red
Coloring the full view:
messageInputBar.backgroundView.backgroundColor = .red
Have you tried this? :
messageInputBar.contentView.backgroundColor

NavigationBar color changes not carried through to other views with UINavigationBarAppearance()

I’m trying to work this out in Xcode 12.5.1 so I can move to Xcode 13. I have run this app in Xcode 13 Released and that’s how I discovered the problem. I know I’m a little late to the game but before Xcode 13… it wasn’t broken.
In my apps I have custom themes setup in three configurations, summer, fall and winter. Each theme sets the color attributes of the navBar as well as those of the rest of the controls in the app. The theme is chosen on the settings VC from a segmented control.
The setNavbarAttributes() code is run from AppDelegate in didFinishLaunchingWithOptions. It sets the correct navBar attributes when the app launches. In this case the code work as expected. I can verify this by navigating to the other views in the app and observing that the navBar shows the correct color attributes.
The setNavbarAttributes() code also runs when a theme is chosen from the segmented control on the settings VC. Here is the problem. When the theme is changed the navBar color attributes are not carried through to the navBar in the other views. Does anybody know why this isn’t working? I have a not so great workaround by putting the settings VC update code in an extension but that means touching every VC. That doesn't seem right.
It worked fine with my old code shown below but that’s broken in Xcode 13 with UINavigationBarAppearance().
let theNavebarProperties = UINavigationBar.appearance()
theNavebarProperties.barTintColor = Theme.current.navbarColor
theNavebarProperties.isTranslucent = false
theNavebarProperties.titleTextAttributes = [.foregroundColor: Theme.current.accentColor, .font: UIFont.systemFont(ofSize: gNavBarTitleTextSize, weight: .semibold)]
This sets the attributes of the navBar across the app.
class SetNarbar
{
static func setNavbarAttributes()
{
let theAppearance = UINavigationBarAppearance()
theAppearance.configureWithOpaqueBackground()
theAppearance.backgroundColor = Theme.current.navbarColor
theAppearance.titleTextAttributes = [.foregroundColor: Theme.current.accentColor, .font: UIFont.systemFont(ofSize: gNavBarTitleTextSize, weight: .semibold)]
UINavigationBar.appearance().standardAppearance = theAppearance
UINavigationBar.appearance().scrollEdgeAppearance = theAppearance
UINavigationBar.appearance().compactAppearance = theAppearance
}
}
This code is run in the settings VC on viewDidLoad and when the theme selector is tapped.
// Sets the controls on the settings VC to the currently selected theme. I have omitted code that works and does not pertain to setting the navBar.
func updateThemeOnSettingsVC()
{
setNeedsStatusBarAppearanceUpdate()
SetNarbar.setNavbarAttributes()
let navAppearance = UINavigationBarAppearance()
navAppearance.configureWithOpaqueBackground()
navAppearance.backgroundColor = Theme.current.navbarColor
navAppearance.titleTextAttributes = [.foregroundColor: Theme.current.accentColor, .font: UIFont.systemFont(ofSize: gNavBarTitleTextSize, weight: .semibold)]
navigationItem.standardAppearance = navAppearance
navigationItem.scrollEdgeAppearance = navAppearance
navigationItem.compactAppearance = navAppearance
}

How to change WKWebview Color to white

I am using WKWebview.
I want to change it's background to .white.
i tried -
wkWebView.backgroundColor = .white
wkWebView.scrollView.backgroundColor = .white
view.backgroundColor = .white
But it's not showing white background.
Try the following:
set view background to white
view.backgroundColor = .white
set WKWebView background to non-opaque & clear
wkWebView.backgroundColor = .clear
wkWebView.opaque = false
This will essentially make the webview see-thru (at least until it loads its own HTML content) and the background view will be visible.
A similar trick will help with dark mode support to avoid the white flash that occurs, but set view.backgroundColor = .systemBackgroundColor
Not quite an answer (but not enough rep to comment).
What I usually do to debug is set very odd colours to things to see what is actually being set, e.g. yellow, pink, blue. This way you can assess if the colour change you're trying to make is working, as well as breakpoints to make sure the line is getting called :)
What worked for me was:
webView.backgroundColor = .white

How can I set the normal color for a floating placeholder in a MDCMultilineTextField?

I'm having troubles changing the color of the placeholder of a MDCMultilineTextField in the non-active state. I'm using a MDCTextInputControllerUnderline and I've managed to personalize every other part of the textfield, with the exception of the floating placeholder when not active (see pictures below, I need it to be white).
I've tried setting the tintColor of the textfield, the textColor and the tintColor of the placeholderLabel, the normalColor, inlinePlaceholderColor, floatingPlaceholderNormalColor and floatingPlaceholderActiveColor of the controller, but nothing seems to work. What am I missing? What's the attribute to change to set the color of the placeholder?
As suggested in the comment, it was a matter of setting the Attributed Placeholder instead of the regular one.
So, in the end, it was just a matter of doing this:
let stringAttr = [NSAttributedStringKey.foregroundColor: UIColor.white]
let attributedPlaceholder = NSAttributedString(string: placeholder, attributes: stringAttr)
textField.attributedPlaceholder = attributedPlaceholder

Resources