Is it possible to customize the prompt string at all?
I have hooked up the navigationBarItem to my ViewController and added a string as the "Prompt" via IB. There are no properties following the prompt class that allow me to alter the contents of the string, color, etc etc. I was wondering if anyone knows any other possible work around for this problem?
It is possible to customize the prompt appearance with one big limitation, the style of the prompt and of the title will always be the same.
Assuming you are in a UIViewController presented within a navigation controller with a navigation bar, you can do this:
guard let font = UIFont(name: "Helvetica", size: 17) else {
return
}
let navigationBar = navigationController?.navigationBar
navigationBar?.titleTextAttributes = [NSFontAttributeName: font,
NSForegroundColorAttributeName: UIColor.blue]
You can also use the UINavigationBar.appearance() method to have this configuration on all the navigation bar prompts in your app.
Related
I have an app and from inside of it I am changing its language.
Language getting properly updated to other screens by changing UIView appearance.
Localize.setCurrentLanguage("ar")
UIView.appearance().semanticContentAttribute = .forceRightToLeft
I'm not able to get control back in my tabcontroller file. TabBar is custom created. I checked with awakefromNib() but it is not getting invocated every time. Is there any tab bar method where I can get control when it appears every time or is there any way to change language of tab bar item titles?
After changing app language in your VC create and call function localizeTabBar():
func localizeTabBar() {
tabBar.items![0].title = "1tab".localized
tabBar.items![1].title = "2tab".localized
tabBar.items![2].title = "3tab".localized
tabBar.items![3].title = "4tab".localized
}
For .localized use this String Extension
Or something like that:
func localizeTabBar() {
tabBar.items?.enumerated().forEach{ (index, item) in
item.title = "\(index)tab".localized
}
}
For .localized use this String Extension
I gave my tableViewController a large title Style programmatically:
self.navigationController?.navigationBar.prefersLargeTitles = true
and in storyboard, I made tableView "Static Cell" content, and "Grouped" Style in Attributes inspector:
But largeTitle didn't work when I ran the app, if tableView style is "Plain", it works. How can I fix it?
I'm not sure if it's your case, but for me using tableView.bounces = false prevented the large-title from working. Removing this line did the trick.
Where did you put your self.navigationController?.navigationBar.prefersLargeTitles = true. I putted it in ViewDidload and it's works properly
It's not clear to me why this occurs. Quite possibly a bug. But here is a work around. In Document Outline. Select your Navigation Controller Scene -> Navigation Bar then select Prefer Large Titles (at the top) in Attribute Inspector like so:
After making this change, if you want other view controllers to have regular / plain titles, simply add this to viewDidLoad() on that particular V.C.
navigationItem.largeTitleDisplayMode = .never
Remove title from your storyboard
In your UIViewController over viewDidLoad function:
navigationItem.title = "Search title"
navigationController?.navigationBar.prefersLargeTitles = true
I am setting UIAlertAction label attributedText in order to set custom Font in UIAlertController.It works but when i tap on the UIAlertAction it changes its font to default for some time and then disappears. Here is the code
let lb = (action.value(forKey: "__representer") as AnyObject)
let label = lb.value(forKey: "label") as? UILabel
label?.attributedText = myMutableString
I think best idea would be to create your own custom alert controller instead of accessing private properties to set your values. Not sure if Apple would accept it or not.
I don't know the solution for your problem but I know the problem which is
Problem: When you press UIAlertAction's button it changes its state. It behaves just like the states of UIButton. So if you know which property you should set for its highlighted state then you can set that to solve this issue.
I have a UIPrintInteractionController that is created programatically. It is set to pull a pdf from my servers, and print said pdf. My question is how can I change the font of the view. I already have set the navbar font in my app delegate (so the font appears on all views), but this doesn't apply to the view. Any help is greatly appreciated. Thanks!
I figured it out. Create an extension of UILabel like so
extension UILabel {
var substituteFontName : String {
get { return self.font.fontName }
set { self.font = UIFont(name: newValue, size: self.font.pointSize) }
}
}
The in AppDelegate.swift, use this extension to change the font of every label in the entire application, INCLUDING the UIPrinterInteractionController.
UILabel.appearance().substituteFontName = "Font Name Here"
Killed two birds with one stone.
I am using a UITextView to make hashtags selectable. It detects taps perfectly. My issue is with the selection color. It looks black and I wanted the text to fade a bit on selection instead.
Here's what I've got now:
I've tried changing the tintColor, the NSForegroundColorAttributeName, and the NSBackgroundColorAttributeName but it doesn't work.
There is no documented property for the selected or highlighted color of detected links, but you should be able to achieve the same effect by overriding the delegate method textView:shouldInteractWithURL:inRange: and changing the color yourself.
From the UITextViewDelegate protocol reference:
The text view calls this method if the user taps or long-presses the
URL link. Implementation of this method is optional. By default, the
text view opens the application responsible for handling the URL type
and passes it the URL. You can use this method to trigger an
alternative action, such as displaying the web content at the URL in a
web view within the current application.
The last parameter is an NSRange object called characterRange, which represents the character range containing the tapped URL (or hashtag). Using that range, you should be able to add attributes such as NSForegroundColorAttributeName so as to change only the color of the particular hashtag that was tapped.
You'll probably want to revert any changes on touchesEnded and touchesCancelled.
Alternatively, you could make your own subclass and manually handle the above.
Although there is no public API to do this, I was curious and decided to dig through the private header for UITextField. I found that there is a class method on UITextField _sharedHighlightView which returns an instance of the private _UIHighlightView class. This is the class in charge of the highlighting. Swizzling _sharedHighlightView and changing its color will allow you to change the color of any data-detected links:
WARNING: This is a hack that uses method swizzling and private APIs/properties.
class MyTextView: UITextView {
var newHighlightView: AnyObject?
func changeHighlight() {
self.newHighlightView = UITextView.performSelector(Selector("_sharedHighlightView")).takeUnretainedValue()
self.newHighlightView?.setValue(UIColor.redColor().colorWithAlphaComponent(0.6), forKey: "_color")
let originalHighlightView = class_getClassMethod(MyTextView.self, Selector("_sharedHighlightView"))
let newHighlightView = class_getClassMethod(MyTextView.self, #selector(MyTextView.swizzleHighlightView))
method_exchangeImplementations(originalHighlightView, newHighlightView)
}
func swizzleHighlightView() -> AnyObject {
return self.newHighlightView!
}
}
In your View Controller:
let textView = MyTextView(frame: CGRect(x: 0, y: 40.0, width: 200.0, height: 200.0))
textView.dataDetectorTypes = .All
textView.editable = false
textView.text = "Some text. http://www.google.com/"
textView.changeHighlight()
self.view.addSubview(textView)
Result:
This could probably be cleaned up a little further by not force-unwrapping any optionals.
Work around for this can be done, you can use UIButton instead of textview. By using button you can have the same effect.