Using svg or font icon in the ios applications - ios

In my iOS application we need to change the color of the icons based on the user configuration. It is one of our important business option.
One of my friend advised me for using .svg or font icon. And shared me this link
They are creating web applications while I am developing iOS application. Does iOS support this technique too? If yes, how can I use it?
Edit
I found SVGKit that is used for the iPhone/iPad apps. Is there any binding library for using it on the monotouch?

SVG fonts (paths) are kind of easy to convert into source code. Once you have this it's very simple to render them in different colours, sizes (iphone/ipad) or resolutions (e.g. retina).
That's what I did here. That's for an older version of FontAwesome, but it could be used as is (or updated to the latest) for your application.

you can use FontAwesome+iOS
all the icons available here: link

If you just need to change color of icons, you probably can solve this without svg. Just use tint colors.
Create a template image:
let templateImage = UIImage(named: "bla_bla")?.withRenderingMode(.alwaysTemplate)
Then add it to a button or an image view and set the tintColor:
button.setImage(templateImage, for: .normal)
button.setImage(templateImage, for: .highlighted)
button.tintColor = UIColor.blue
This solution works only for monochromatic icons!
Besides, if you want .svg, you can use WKWebView, it's really fast and will not cause overhead if you have limited amount of icons.

Related

How can I assign one of the built-in icons to a UIButton programatically?

In the storyboard of XCode 11.5 there are several icons in the "image" property of a UIButton. However, I can't find any references to these images in the documentation and when attempting to create a button programatically I can only find references to using images I put in the bundle myself. Is there a way of accessing these built-in images from the code? How?
They are SFSysmbol icons. If you're asking about an auto-complete in Xcode when adding images programmatically, there isn't one. But you can get the list of all the available SFSymbols from this app. And you can use UIImage(systemName:) initializer to access the available icons.
To access system icons:
let image = UIImage(systemName: "wrench")
button.setImage(image, for: .normal)

Adding Text and Images in the same field in Swift

I wanted to make a kind of a notes app where users can add images inline with text, similar to how iOS' native Notes app does. Any advice on approaches I could take for this?
I'm new to iOS development and have been trying to teach myself a bunch of necessary skills and was curious as to how someone would go about doing this.
Thanks!
You can use UITextField to add images and text in one field, It can be done by using storyboard also and to do it programmatically is also convenient. Find the below code to add image programmatically in a text field :
var imageView = UIImageView()
var image = UIImage(named: "email.png")
imageView.image = image
emailField.leftView = imageView
emailField.leftViewMode = UITextFieldViewMode.always
emailField.leftViewMode = .always
UITextView is your best bet. It is not the most friendly UI component to work with if you are just starting out, but it is what Apple almost certainly uses for their Notes App, and supports inlining of text, images, hyperlinks, styles etc.

How can I access UIImage dark appearance pre iOS13

I've just added dark mode to my app.
Asset catalogs support multiple appearances for each asset, on iOS 13 this means the correct asset is used when the system is running in dark or light mode.
These are not my real assets
I'm trying to support dark mode on older iOS versions.
In my attempts to do so, I have added an override to force dark mode which works for my custom colours and theming but not for the images.
Is it possible to access the dark appearance of an image programatically before iOS13?
For iOS12 I have tried using the following:
if #available(iOS 12.0, *) {
let traits = UITraitCollection(userInterfaceStyle: .dark)
let image = UIImage(
named: "Image",
in: bundle,
compatibleWith: traits
)
}
This only returns the normal appearance, and the method naming seems to suggest this only checks that the trait collection I've passed is compatible with the image.
As far as I know, there's no way to do that with just one asset. Below iOS 13 the system will always take the Any appearance. You would need to create two different image sets with different names and choose either one of them.
It is a bit confusing since UIUserInterfaceStyle is available in iOS 12+, but this is likely because macOS got Dark Mode that year.

Using PDFs for icon images in Xcode 7.2

I'm attempting to use PDF files as icons in an app I'm working on. The issue I'm encountering is I'm getting inconsistent tint colors.
If I set a button image from interface builder, the icon image shows up black at runtime. Every time. Regardless of what I attempt to set from interface builder.
I tried setting my button icon image via code and instead of showing up black, it's white:
let myGraphicFile = UIImage(named: "myPDFImage")
let myButtonImage = myGraphicFile?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
myButton.setImage(myButtonImage, forState: .Normal)
From code, regardless of what I attempt to set the tint to, it's always white from code.
I discovered this post relating to Xcode 6.x, but I think it might be dated, as I'm able to partially do it, but I can't set the tint.
Use PDF in XCode for an AppIcon (.appiconset collection)
I create the icons in Inkscape, save as PDF 1.5. I add the file to Images.xcassets. In Images.xcassets' attributes inspector, I'm setting:
Devices to Universal
Scale factor to Single Vector.
Summary: I can get it to show up and scale properly, but it's either black from interface builder or white from code. I suspect I'm missing something re: how to save the file from Inkscape.
Thank you for reading. If you have any suggestions, I welcome them.
I have figured out how to create vector icons with Inkscape. When you use PDFs to display icons in iOS, you need to alter the Attributes Inspector for your icon in xcAssets as follows:
1) Drag the PDF into xcAssets
2) Set devices (I did Universal and it worked fine)
3) If your PDF icon is under 1x, 2x, or 3x size class, drag it to Universal and delete the rest of them.
4) Set Scale Factors to Single Vector.
5) Render as Template Image.
Once it's configured there, then you just treat it was you would any other image in interface builder. It's essentially the same thing I was doing in code, but I don't think it gets done in code...it's gotta be done on xcAssets where the image lives. It's my understanding iOS renders vector images for the size class at run time. I think by attempting to tweak it in code wasn't working because the image had already been rendered.
If anyone has any questions on this, I found this link helpful in resolving my issue.
Additionally, this post covers the topic, too. https://stackoverflow.com/a/25804358/4475605

How to get system images programmatically? (example: disclosure chevron)

I want to create UIImageView with some system-image in it. (Example: the disclosure chevron image, etc.). How can I do so programmatically?
Note: I don't want to download the image and add it to the project, I want to fetch it from the user programmatically / from the Interface Builder.
From iOS 13 and Xcode 11 you can get system images by giving system name.
let image = UIImage(systemName: "info.circle")
Reference : UIImage Apple Documentation
If you mean you can to set the button to use the system disclosure icon, just do as #middaparka suggested and use this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
If you're asking how you can access the image directly, you can't do that. This is your only option: Use default apple icons for pdfs and docs in iOS app?
I think iOS-Artwork-Extractor is the tool you are looking for. It lets you extract the artwork into png files.
There is no clean programmatical way to access the images of system buttons. Quote from the documentation of the UIButton.imageView property:
The value of the property is nil for system buttons.
You may do an off screen rendering of the views and extract the resources on the fly but that's a very shaky approach. It's better to extract every asset you need and use them as intended. You'll suffer much more in the end with the programmatic way.

Resources