1565240584
Often, we want to style elements that contain content. How about when an element has no children or text at all? Easy, you can use the :empty selector đ¤Š
<p> </p><!-- NOT empty: note the blank space --> <p></p><!-- YES empty: nothing inbetween -->
p::before { font-family: "FontAwesome"; content: "\f240"; }p:empty::before {
content: â\f244â;
}
p {
color: silver;
}p:empty {
color: red;
}
When I first encounter this, there was a few confusion about what this property considers as empty. Letâs stick with MDNâs definition here:
The :empty CSS pseudo-class represents any element that has no children. Children can be either element nodes or text (including whitespace). Comments, processing instructions, and CSS content do not affect whether an element is considered empty.
Is empty
As long as there is no whitespace, itâs an empty element.
<p></p>
Comment in between is also considered an empty element. As long as there is no whitespace.
<p><!-- comment --></p>
Not empty
Whitespace is considered not empty. Even in a new line, there is whitespace, so not empty! Emphasizing on this, cause I made the same mistake đ
<p> </p><p>
<!-- comment -->
</p>
Having children element also counts as not empty
<p><span></span></p>
The good news is â in Selectors Level 4, whitespace would be considered empty! This will make it similar to act like :-moz-only-whitespace. In other words, this would considered empty:
<!-- Considered Empty in CSS Selectors Level 4 -->
<p> </p>
â ď¸ BUT, donât do this yet. Currently no browser supports this.
Examples using :empty
Okay, letâs take a look at some real-life examples of using :empty.
Using :empty in Form Error Message
This is the example that made me first discovered :empty. So I wanted to prepend a â icon on my error message. But the problem is the icon appeared even when I had no error message. But then no problem, I can just use the :empty to only append the icon when there IS an error message đ
CSS
.error:before {
color: red;
content: "\0274c "; /* â icon */
}
HTML
<!-- No error message -->
<div class=âerrorâ></div><!-- Yes error message -->
<div class=âerrorâ>Missing Email</div>
Output
Without Empty
â
â Missing Email
With :empty
â Missing Email
Hereâs another example using :empty to hide empty state.
.alert {
background: pink;
padding: 10px;
}
.alert:empty {
display: none;
}
HTML
<div class=âalertâ></div>
<div class=âalertâ>Alert Message</div>
Output
Without empty
Alert Message
With :empty
Alert Message
Support on this is actually really good. It supports all the way to Internet Explorer 9 đ
Community Examples
I discovered :empty when I was trying to style empty error messages in a form. <div class=âerrorâ></div>. No JS required anymore, I can use pure CSS đ
Thanks for reading â¤
If you liked this post, share it with all of your programming buddies!
Follow me on Facebook | Twitter
â The Web Developer Bootcamp
â Build Responsive Real World Websites with HTML5 and CSS3
â Advanced CSS and Sass: Flexbox, Grid, Animations and More!
â Web Design for Beginners: Real World Coding in HTML & CSS
â HTML5 and CSS3 Fundamentals: Development for Absolute Beginners
â How to build and send a HTML Email 2019
â Using Web Workers to Real-time Processing
â How to build Desktop Apps with HTML, CSS and Javascript ?
â 34 Free HTML And CSS Books
â Building a Simple URL Shortener With HTML and Javascript
#css
1657254050
In this tutorial, we'll summarise what the top 9+ CSS mistakes are and how to avoid them.
Itâs easy to get tripped up with CSS. Here are some common CSS mistakes we all make.
Web browsers are our fickle friends. Their inconsistencies can make any developer want to tear their hair out. But at the end of the day, theyâre what will present your website, so you better do what you have to do to please them.
One of the sillier things browsers do is provide default styling for HTML elements. I suppose you canât really blame them: what if a âwebmasterâ chose not to style their page? There has to be a fallback mechanism for people who choose not to use CSS.
In any case, thereâs rarely a case of two browsers providing identical default styling, so the only real way to make sure your styles are effective is to use a CSS reset. What a CSS reset entails is resetting (or, rather, setting) all the styles of all the HTML elements to a predictable baseline value. The beauty of this is that once you include a CSS reset effectively, you can style all the elements on your page as if they were all the same to start with.
Itâs a blank slate, really. There are many CSS reset codebases on the web that you can incorporate into your work. I personally use a modified version of the popular Eric Meyer reset and Six Revisions uses a modified version of YUI Reset CSS.
You can also build your own reset if you think it would work better. What many of us do is utilizing a simple universal selector margin/padding reset.
* { margin:0; padding:0; }
Though this works, itâs not a full reset.
You also need to reset, for example, borders, underlines, and colors of elements like list items, links, and tables so that you donât run into unexpected inconsistencies between web browsers. Learn more about resetting your styles via this guide: Resetting Your Styles with CSS Reset.
Being overly specific when selecting elements to style is not good practice. The following selector is a perfect example of what Iâm talking about:
ul#navigation li a { ... }
Typically the structure of a primary navigation list is a <ul>
(usually with an ID like #nav
or #navigation
) then a few list items (<li>
) inside of it, each with its own <a>
tag inside it that links to other pages.
This HTML structure is perfectly correct, but the CSS selector is really what Iâm worried about. First things first: Thereâs no reason for the ul
before #navigation
as an ID is already the most specific selector. Also, you donât have to put li
in the selector syntax because all the a
elements inside the navigation are inside list items, so thereâs no reason for that bit of specificity.
Thus, you can condense that selector as:
#navigation a { ... }
This is an overly simplistic example because you might have nested list items that you want to style differently (i.e. #navigation li a
is different from #navigation li ul li a
); but if you donât, then thereâs no need for the excessive specificity.
I also want to talk about the need for an ID in this situation. Letâs assume for a minute that this navigation list is inside a header div (#header
). Let us also assume that you will have no other unordered list in the header besides the navigation list.
If that is the case, we can even remove the ID from the unordered list in our HTML markup, and then we can select it in CSS as such:
#header ul a { ... }
Hereâs what I want you to take away from this example: Always write your CSS selectors with the very minimum level of specificity necessary for it to work. Including all that extra fluff may make it look more safe and precise, but when it comes to CSS selectors, there are only two levels of specificity: specific, and not specific enough.
Take a look at the following property list:
#selector { margin-top: 50px; margin-right: 0; margin-bottom: 50px; margin-left 0; }
What is wrong with this picture? I hope that alarm bells are ringing in your head as you notice how much weâre repeating ourselves. Fortunately, there is a solution, and itâs using CSS shorthand properties.
The following has the same effect as the above style declaration, but weâve reduced our code by three lines.
#selector { margin: 50px 0; }
Check out this list of properties that deals with font styles:
font-family: Helvetica; font-size: 14px; font-weight: bold; line-height: 1.5;
We can condense all that into one line:
font: bold 14px/1.5 Helvetica;
We can also do this for background
properties. The following:
background-image: url(background.png); background-repeat: repeat-y; background-position: center top;
Can be written in shorthand CSS as such:
background: url(background.png) repeat-y center top;
Say you want to add a 20px margin to the bottom of an element. You might use something like this:
#selector { margin: 20px 0px 20px 0px; }
Donât. This is excessive.
Thereâs no need to include the px
after 0
. While this may seem like Iâm nitpicking and that it may not seem like much, when youâre working with a huge file, removing all those superfluous px
can reduce the size of your file (which is never a bad thing).
Declaring red
for color values is the lazy manâs #FF0000
. By saying:
color: red;
Youâre essentially saying that the browser should display what it thinks red is. If youâve learned anything from making stuff function correctly in all browsers â and the hours of frustration youâve accumulated because of a stupid list-bullet misalignment that can only be seen in IE7 â itâs that you should never let the browser decide how to display your web pages.
Instead, you should go to the effort to find the actual hex value for the color youâre trying to use. That way, you can make sure itâs the same color displayed across all browsers. You can use a color cheatsheet that provides a preview and the hex value of a color.
This may seem trivial, but when it comes to CSS, itâs the tiny things that often lead to the big gotchas.
My process for writing styles is to start with all the typography, and then work on the structure, and finally on styling all the colors and backgrounds. Thatâs what works for me. Since I donât focus on just one element at a time, I commonly find myself accidentally typing out a redundant style declaration.
I always do a final check after Iâm done so that I can make sure that I havenât repeated any selectors; and if I have, Iâll merge them. This sort of mistake is fine to make while youâre developing, but just try to make sure they donât make it into production.
Similar to the one above, I often find myself having to apply the same properties to multiple selectors. This could be styling an <h5>
in the header to look exactly like the <h6>
in the footer, making the <pre>
âs and <blockquote>
âs the same size, or any number of things in between. In the final review of my CSS, I will look to make sure that I havenât repeated too many properties.
For example, if I see two selectors doing the same thing, such as this:
#selector-1 { font-style: italic; color: #e7e7e7; margin: 5px; padding: 20px } .selector-2 { font-style: italic; color: #e7e7e7; margin: 5px; padding: 20px }
I will combine them, with the selectors separated by a comma (,
):
#selector-1, .selector-2 { font-style: italic; color: #e7e7e7; margin: 5px; padding: 20px }
I hope youâre seeing the trend here: Try to be as terse and as efficient as possible. It pays dividends in maintenance time and page-load speed.
In a perfect world, every computer would always have every font you would ever want to use installed. Unfortunately, we donât live in a perfect world. @font-face aside, web designers are pretty much limited to the few so called web-safe fonts (e.g.
Arial, Georgia, serif, etc.). There is a plus side, though. You can still use fonts like Helvetica that arenât necessarily installed on every computer.
The secret lies in font stacks. Font stacks are a way for developers to provide fallback fonts for the browser to display if the user doesnât have the preferred font installed. For example:
#selector { font-family: Helvetica; }
Can be expanded with fallback fonts as such:
#selector { font-family: Helvetica, Arial, sans-serif; }
Now, if the user doesnât have Helvetica, they can see your site in Arial, and if that doesnât work, itâll just default to any sans-serif font installed.
By defining fallback fonts, you gain more control as to how your web pages are rendered.
When it comes to trying to reduce your CSS file sizes for performance, every space counts. When youâre developing, itâs OK to format your code in the way that youâre comfortable with. However, there is absolutely no reason not to take out excess characters (a process known as minification) when you actually push your project onto the web where the size of your files really counts.
Too many developers simply donât minify their files before launching their websites, and I think thatâs a huge mistake. Although it may not feel like it makes much of a difference, when you have huge CSS files
When youâre writing CSS, do yourself a favor and organize your code. Through comments, you can insure that the next time you come to make a change to a file youâll still be able to navigate it.
I personally like to organize my styles by how the HTML that Iâm styling is structured. This means that I have comments that distinguish the header, body, sidebar, and footer. A common CSS-authoring mistake I see is people just writing up their styles as soon as they think of them.
The next time you try to change something and canât find the style declaration, youâll be silently cursing yourself for not organizing your CSS well enough.
This oneâs subjective, so bear with me while I give you my perspective. I am of the belief, as are others, that it is better to split stylesheets into a few different ones for big sites for easier maintenance and for better modularity. Maybe Iâll have one for a CSS reset, one for IE-specific fixes, and so on.
By organizing CSS into disparate stylesheets, Iâll know immediately where to find a style I want to change. You can do this by importing all the stylesheets into a stylesheet like so:
@import url("reset.css"); @import url("ie.css"); @import url("typography.css"); @import url("layout.css");
Let me stress, however, that this is what works for me and many other developers. You may prefer to squeeze them all in one file, and thatâs okay; thereâs nothing wrong with that.
But if youâre having a hard time maintaining a single file, try splitting your CSS up.
In order to style your site on pages that will be printed, all you have to do is utilize and include a print stylesheet. Itâs as easy as:
<link rel="stylesheet" href="print.css" media="print" />
Using a stylesheet for print allows you to hide elements you donât want printed (such as your navigation menu), reset the background color to white, provide alternative typography for paragraphs so that itâs better suited on a piece of paper, and so forth. The important thing is that you think about how your page will look when printed.
Too many people just donât think about it, so their sites will simply print the same way you see them on the screen.
No matter how long you've been writing code, it's always a good time to revisit the basics. While working on a project the other day, I made 2 beginner mistakes with the CSS I was writing. I misunderstood both CSS specificity and how transform:scale affects the DOM!
Stack Overflow about transform:scale - https://stackoverflow.com/questions/32835144/css-transform-scale-does-not-change-dom-size
CSS Specificity - https://www.w3schools.com/css/css_specificity.asp
#css
1622879640
The goal isn't to just write CSS that works. Code CSS that is efficient and easy to maintain
Here are some common mistakes that most web developers make, and how identifying and avoiding them can help you write better and more efficient CSS!
1. Using Color Names Instead of Hexadecimal
When you say color: blue; you're essentially telling the computer to display whatever shade of color it thinks blue is. By doing this, youâre giving the browser control over how your web page should be displayed, and as a developer, this is something you should never do. By vaguely naming the color as blue, it can easily differ from the shade of blue that you had in mind, and worse it can also vary from browser to browser.
Using hexadecimal values eg. color: #4169E1; hence becomes something that all developers should adopt. It ensures specificity, is supported by all browsers, and gives back to you the control to decide exactly how you want your web page to be displayed.
Note: An efficient way for finding and using hexadecimal values is by first putting in the name of the closest color to the desired shade, and then inspecting the element to find the hex value using the color dropper.
2. Hard Coding px Instead of Relative Units
While it sometimes becomes imperative to use absolute px values, you should always use relative measurements such as em, % (percent), rem (root-Em), and others whenever possible.
This ensures that the website scales proportionally according to the userâs choice of zoom level and screen/browser size.
So, instead of declaring the sizes in absolutes,
p {
font-size: 16px;
line-height: 20px;
margin-bottom: 8px;
}
do this instead:
p {
font-size: 1rem;
line-height: 1.25em;
margin-bottom: 0.5rem;
}
3. Not Using Font Fallbacks
No matter how beautiful a particular font makes your page look, or how much it catches the eye, you always have to consider that not all font types are supported by all computers. If youâre using a font that some browsers do not support, it means that your web page might not be as beautiful and eye-catching as youâre designing it to be for all users.
So, after you use your favorite font, say Helvetica, always make sure to list fallback fonts that the browser can use in case it isn't supported.
Instead of writing this,
#selector {
font-family: Helvetica;
}
expand the code by font fallbacks such as:
#selector {
font-family: Helvetica, Arial, sans-serif;
}
Now, even if the browser doesn't support Helvetica, it would fall back to the second most preferred option Arial before going to the browser default.
4. Not Using CSS Shorthands
Take a look at the CSS below:
font-family: Helvetica;
font-size: 14px;
font-weight: bold;
line-height: 1.5;
This can easily be condensed into a single line using the CSS shorthand for font:
font: bold 14px/1.5 Helvetica;
Similarly, this list of properties for a background image:
background-image: url(background.png);
background-repeat: repeat-y;
background-position: center top;
can be written as:
background: url(background.png) repeat-y center top;
Why are we doing this? The reason is simple. It not only reduces your CSS file size but also makes the stylesheet easier to read and more organized.
Hereâs a list of CSS shorthands to help you write cleaner code. Itâs all about ingraining it into your coding habits, but once you do it, there's no going back!
5. Over Qualifying Selectors
Just like every other thing in excess, too much specificity is a bad thing. And more often than not, it is not even necessary. Take a look at the CSS below:
header #nav ul li a {...}
First of all, the header specification is absolutely unnecessary since an ID, having the highest specificity, has already been used (IDs are unique and only associated with one element). Moreover, an unordered list (ul) always has list items (li) within. So having to mention that becomes pointless. The same selector can now be written as:
#nav ul a {...}
With CSS there are only two levels of specificity â specific and not specific enough. Including all those extra elements might make it look âsafeâ but are actually unnecessary and are only adding to the length of your stylesheet.
As a general rule, your selectors should be as short as possible. Be just as specific as is necessary for it to work.
6. Using IDâs instead of Classes
The most cogent argument against using IDâs is that it has a much higher specificity than classes, which is why it becomes hard to overwrite and extend your styles. A class on its own canât overwrite styles belonging to an ID. To âbeatâ the ID, you would need either more IDs or to use !important, which can begin specificity wars in your stylesheets.
Class selectors can also be used for several HTML elements on the same page, unlike IDs which are unique to each element. Being able to reuse styles is one of the advantages of CSS.
To maintain a consistent convention, use only the class attributes to define styles and IDs while integrating interactivity with Javascript instead.
7. Not Using CSS Reset
If you have ever displayed an HTML page with no CSS styling, you know that the web browser itself âstylesâ the page using some default values as a fallback. The text has a particular font size and style, the margin and padding are set to certain values.
While this is a good thing for someone who does not use CSS, it is important to first reset these values when you put your own styles into the page. These values vary from browser to browser, hence a CSS Reset is the only way to ensure that all your styles are uniform and effective.
This entails resetting all the styles of all the HTML elements to a predictable baseline value. Once you do this, you can style all the elements on your page as if they were the same to start with. A blank slate.
An easier but incomplete way to do this is by resetting the margin and padding using a universal selector:
* {margin:0; padding:0;}
For a complete reset, however, you can use the Eric Meyer reset (modifying it as per your choice), to reset borders, underlines, and colors of elements like list items, links, and tables so that you donât run into unexpected inconsistencies between web browsers.
8. Repetitive Code (Redundant Selectors and Properties)
In general, repeating yourself while coding is not considered a good practice. CSS is no different. Take a look at the code below:
#selector-1 {
font-style: italic;
color: #e7e7e7;
margin: 5px;
padding: 20px
}
.selector-2 {
font-style: italic;
color: #e7e7e7;
margin: 5px;
padding: 20px
}
A better way to write this is by combining them, with the selectors separated by a comma (,):
#selector-1, .selector-2 {
font-style: italic;
color: #e7e7e7;
margin: 5px;
padding: 20px
}
This is not just more efficient, but also reduces maintenance time and page-load speed.
9. Not Separating Design from Layout
The job of CSS is to provide styling, and the job of HTML is to provide structure. Generally, HTML should be written in a way that captures the information hierarchy of the page, ignoring any design concerns. Afterward, CSS can be added to make things âlook nice.â
However, while HTML provides structure, it cannot always position elements on the exact spot of a page you want it to appear, which is where we use CSS to scaffold the layout of the page. Once an element is put into the right place on the page, itâs easy to style it without worrying about the display and position. This is why Layout CSS should be separated from Design CSS.
Instead of putting the layout as well as design properties together,
.article {
display: inline-block;
width: 50%;
margin-bottom: 1em;
font-family: sans-serif;
border-radius: 1rem;
box-shadow: 12px 12px 2px 1px rgba(0, 0, 0, .2);
}
.sidebar {
width: 25%;
margin-left: 5px;
}
<div class="article"></div>
<div class="article sidebar"></div>
Separate the design and layout of elements:
/* layout */
.article, .sidebar {
display: inline-block;
}
.article {
width: 50%;
margin-bottom: 1em;
}
.sidebar {
width: 25%;
margin-left: 5px;
}
/* display */
.card {
font-family: sans-serif;
border-radius: 1rem;
box-shadow: 12px 12px 2px 1px rgba(0, 0, 0, .2);
}
<div class="article card"></div>
<div class="sidebar card"></div>
This ensures separation of concerns, which is a common software engineering principle that helps keep our code maintainable and easy to understand.
10. Writing Unorganized CSS
Instead of writing your styles just as you think of them, do yourself a favor and organize your code neatly. This will ensure that next time you come to make a change to your file, youâre still able to navigate it.
#css #programming #developer
1662112200
Please â this library.
Now, you don't have to download different libraries to include different font icons. This SwiftIcons library helps you use icons from any of the following font icons.
SwiftIcons supports different objects from the object library.
CocoaPods is a dependency manager for Cocoa projects.
Make sure you have the latest version of CocoaPods by running:
$ gem install cocoapods
# (or if the above fails)
$ sudo gem install cocoapods
Update your local specs repo by running:
$ pod repo update
Add the following lines to your Podfile
:
target 'YourProject' do
use_frameworks!
pod 'SwiftIcons', '~> 3.0'
end
Then run the following command
$ pod install
You can start using the library by importing it wherever you want
import SwiftIcons
Carthage is a decentralized dependency manager for Cocoa projects.
Install the latest version of Carthage.
Add this line to your Cartfile
:
github "ranesr/SwiftIcons" ~> 3.0
or for master
,
github "ranesr/SwiftIcons" "master"
Then run carthage update --platform ios
and add the built framework to your project by following these instructions from Carthage.
Copy all the files from Source
folder. Link to files.
Check to import all ttf files in project, "Project" > "Target" > "Copy Bundle Resources"
You can check library reference documentation here.
There are different font types for each of the font icons
Font Icons | Version | Font Types | Icons |
---|---|---|---|
Dripicons | 2.0 | dripicons | dripicons |
Emoji | emoji | emoji | |
FontAwesome | 5.1.0 | fontAwesome | fontAwesome |
Icofont | 1.0.0 Beta | icofont | icofont |
Ionicons | 2.0.1 | ionicons | ionicons |
Linearicons | 1.0.0 | linearIcons | linearIcons |
Map-icons | 3.0.2 | mapicons | mapicons |
Material icons | 2.2.0 | googleMaterialDesign | googleMaterialDesign |
Open iconic | 1.1.1 | openIconic | openIconic |
State face icons | state | state | |
Weather icons | 2.0.10 | weather | weather |
TypIcons | 2.0.7 | TypIcons | Typicons |
When setting an icon to any object, you have to mention which font type it is and then select which icon you want to set from that particular font icon.
import SwiftIcons
UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35))
// Icon with colors
UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35), textColor: .red)
UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35), textColor: .white, backgroundColor: .red)
// Stacked icons with bigger background
UIImage.init(bgIcon: .fontAwesomeRegular(.circle), topIcon: .fontAwesomeRegular(.square))
// Stacked icons with smaller background
UIImage.init(bgIcon: .fontAwesomeSolid(.camera), topIcon: .fontAwesomeSolid(.ban), topTextColor: .red, bgLarge: false)
// Stacked icons with custom size
UIImage.init(bgIcon: .fontAwesomeSolid(.camera), topIcon: .fontAwesomeSolid(.ban), topTextColor: .red, bgLarge: false, size: CGSize(width: 50, height: 50))
import SwiftIcons
// Setting icon to image view
imageView.setIcon(icon: .weather(.rainMix))
// Icon with colors
imageView.setIcon(icon: .mapicons(.amusementPark), textColor: .white, backgroundColor: .blue, size: nil)
import SwiftIcons
// Setting icon to label
label.setIcon(icon: .ionicons(.paintbrush), iconSize: 70)
// Icon with colors
label.setIcon(icon: .googleMaterialDesign(.rowing), iconSize: 70, color: .white, bgColor: textColor)
// Icon with text around it
label.setIcon(prefixText: "Bus ", icon: .linearIcons(.bus), postfixText: " icon", size: 20)
// Icon with color & colored text around it
label.setIcon(prefixText: "Medal ", prefixTextColor: .red, icon: .ionicons(.ribbonA), iconColor: .red, postfixText: "", postfixTextColor: .red, size: nil, iconSize: 40)
// Icon with text with different fonts around it
label.setIcon(prefixText: "Font ", prefixTextFont: font1!, icon: .fontAwesomeSolid(.font), postfixText: " icon", postfixTextFont: font2!)
// Icon with text with different fonts & colors around it
label.setIcon(prefixText: "Bike ", prefixTextFont: font1!, prefixTextColor: .red, icon: .mapicons(.bicycling), iconColor: textColor, postfixText: " icon", postfixTextFont: font2!, postfixTextColor: .blue, iconSize: 30)
import SwiftIcons
// Setting icon to button
button.setIcon(icon: .linearIcons(.phone), forState: .normal)
// Icon with size and color
button.setIcon(icon: .openIconic(.clipboard), iconSize: 70, color: .blue, forState: .normal)
// Icon with text around it
button.setIcon(prefixText: "Please ", icon: .googleMaterialDesign(.print), postfixText: " print", forState: .normal)
// Icon with color & colored text around it
button.setIcon(prefixText: "Lock ", prefixTextColor: .red, icon: .googleMaterialDesign(.lock), iconColor: .yellow, postfixText: " icon", postfixTextColor: .blue, forState: .normal, textSize: 15, iconSize: 20)
// Icon with text with different fonts around it
button.setIcon(prefixText: "Happy ", prefixTextFont: font1!, icon: .ionicons(.happy), postfixText: " face", postfixTextFont: font2!, forState: .normal)
// Icon with text with different fonts & colors around it
button.setIcon(prefixText: "Pulse ", prefixTextFont: font1!, prefixTextColor: .darkGray, icon: .openIconic(.pulse), iconColor: .red, postfixText: " icon", postfixTextFont: font2!, postfixTextColor: .purple, forState: .normal, iconSize: 40)
// Icon with title below icon
button.setIcon(icon: .emoji(.ferrisWheel), title: "Ferris Wheel", color: .red, forState: .normal)
// Icon with title below icon with different color & custom font
button.setIcon(icon: .weather(.rainMix), iconColor: .yellow, title: "RAIN MIX", titleColor: .red, font: font!, backgroundColor: .clear, borderSize: 1, borderColor: .green, forState: .normal)
import SwiftIcons
// Setting icon at particular index
segmentedControl.setIcon(icon: .linearIcons(.thumbsUp), forSegmentAtIndex: 0)
segmentedControl.setIcon(icon: .linearIcons(.thumbsDown), forSegmentAtIndex: 1)
// Icons with sizes & colors
segmentedControl.setIcon(icon: .fontAwesomeSolid(.male), color: .red, iconSize: 50, forSegmentAtIndex: 0)
segmentedControl.setIcon(icon: .fontAwesomeSolid(.female), color: .purple, iconSize: 50, forSegmentAtIndex: 1)
import SwiftIcons
// Setting icon to tab bar item
tabBar.items?[0].setIcon(icon: .fontAwesomeSolid(.font), size: nil, textColor: .lightGray)
// Stacked icons for tab bar item
tabBar.items?[1].setIcon(bgIcon: .fontAwesomeRegular(.circle), bgTextColor: .lightGray, topIcon: .fontAwesomeSolid(.square), topTextColor: .lightGray, bgLarge: true, size: nil)
import SwiftIcons
// Change minimum & maximum value icons
slider.setMaximumValueIcon(icon: .emoji(.digitNine))
slider.setMinimumValueIcon(icon: .emoji(.digitZero))
// Change minimum & maximum value icons with colors
slider.setMaximumValueIcon(icon: .linearIcons(.pointerUp), customSize: nil, textColor: .red, backgroundColor: .clear)
slider.setMinimumValueIcon(icon: .linearIcons(.pointerDown), customSize: nil, textColor: .blue, backgroundColor: .clear)
import SwiftIcons
// Setting icon to bar button item
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30)
// Icon with colors
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30, color: textColor)
// Icon with custom cgRect
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30, color: textColor, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), target: self, action: #selector(barButtonItem(sender:)))
// Icon with text around it
barButtonItem.setIcon(prefixText: "Please ", icon: .ionicons(.iosDownload), postfixText: " download", cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), size: 23, target: self, action: #selector(barButtonItem(sender:)))
// Icon with color & colored text around it
barButtonItem.setIcon(prefixText: "Blue ", prefixTextColor: .red, icon: .ionicons(.iosFootball), iconColor: .blue, postfixText: " football", postfixTextColor: .green, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), size: 20, iconSize: 30, target: self, action: #selector(barButtonItem(sender:)))
// Icon with text with different fonts around it
barButtonItem.setIcon(prefixText: "Digit ", prefixTextFont: font1!, icon: .emoji(.digitOne), postfixText: " One", postfixTextFont: font2!, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), target: self, action: #selector(barButtonItem(sender:)))
// Icon with text with different fonts & colors around it
barButtonItem.setIcon(prefixText: "", prefixTextFont: font1!, prefixTextColor: .red, icon: .ionicons(.iosFootball), iconColor: .blue, postfixText: " football", postfixTextFont: font2!, postfixTextColor: .green, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), iconSize: 24, target: self, action: #selector(barButtonItem(sender:)))
import SwiftIcons
// Setting icon to the title
self.setTitleIcon(icon: .emoji(.animalHorse), iconSize: 30, color: .red)
import SwiftIcons
// Setting left view icon
textfield.setLeftViewIcon(icon: .fontAwesomeSolid(.search))
// Left view icon with colors & leftViewMode
textfield.setLeftViewIcon(icon: .state(.TX), leftViewMode: .always, textColor: .blue, backgroundColor: .clear, size: nil)
textfield.setLeftViewIcon(icon: .googleMaterialDesign(.plusOne), leftViewMode: .unlessEditing, textColor: .green, backgroundColor: .clear, size: nil)
// Setting right view icon
textfield.setRightViewIcon(icon: .openIconic(.questionMark))
// Right view icon with colors & rightViewMode
textfield.setRightViewIcon(icon: .weather(.rainMix), rightViewMode: .always, textColor: .red, backgroundColor: .clear, size: nil)
import SwiftIcons
// Setting icons
stepper.setDecrementIcon(icon: .ionicons(.iosPause), forState: .normal)
stepper.setIncrementIcon(icon: .ionicons(.iosPlay), forState: .normal)
Please check out the SwiftIcons App. In the demo project, if you click on any object, you will see the method description in the logs for the icon of that object.
If you are using SwiftIcons in your app and want to be listed here, simply create a new issue here.
I am always curious who is using my projects đ
Saurabh Rane
Special thanks to Patrik Vaberer and his initial work on Font-Awesome-Swift library
Author: ranesr
Source code: https://github.com/ranesr/SwiftIcons
License: MIT license
#swift
1675107840
Swift Library for Font Icons
Please â this library.
Now, you don't have to download different libraries to include different font icons. This SwiftIcons library helps you use icons from any of the following font icons.
SwiftIcons supports different objects from the object library.
CocoaPods is a dependency manager for Cocoa projects.
Make sure you have the latest version of CocoaPods by running:
$ gem install cocoapods
# (or if the above fails)
$ sudo gem install cocoapods
Update your local specs repo by running:
$ pod repo update
Add the following lines to your Podfile
:
target 'YourProject' do
use_frameworks!
pod 'SwiftIcons', '~> 3.0'
end
Then run the following command
$ pod install
You can start using the library by importing it wherever you want
import SwiftIcons
Carthage is a decentralized dependency manager for Cocoa projects.
Install the latest version of Carthage.
Add this line to your Cartfile
:
github "ranesr/SwiftIcons" ~> 3.0
or for master
,
github "ranesr/SwiftIcons" "master"
Then run carthage update --platform ios
and add the built framework to your project by following these instructions from Carthage.
Copy all the files from Source
folder. Link to files.
Check to import all ttf files in project, "Project" > "Target" > "Copy Bundle Resources"
You can check library reference documentation here.
There are different font types for each of the font icons
Font Icons | Version | Font Types | Icons |
---|---|---|---|
Dripicons | 2.0 | dripicons | dripicons |
Emoji | emoji | emoji | |
FontAwesome | 5.1.0 | fontAwesome | fontAwesome |
Icofont | 1.0.0 Beta | icofont | icofont |
Ionicons | 2.0.1 | ionicons | ionicons |
Linearicons | 1.0.0 | linearIcons | linearIcons |
Map-icons | 3.0.2 | mapicons | mapicons |
Material icons | 2.2.0 | googleMaterialDesign | googleMaterialDesign |
Open iconic | 1.1.1 | openIconic | openIconic |
State face icons | state | state | |
Weather icons | 2.0.10 | weather | weather |
TypIcons | 2.0.7 | TypIcons | Typicons |
When setting an icon to any object, you have to mention which font type it is and then select which icon you want to set from that particular font icon.
import SwiftIcons
UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35))
// Icon with colors
UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35), textColor: .red)
UIImage.init(icon: .emoji(.airplane), size: CGSize(width: 35, height: 35), textColor: .white, backgroundColor: .red)
// Stacked icons with bigger background
UIImage.init(bgIcon: .fontAwesomeRegular(.circle), topIcon: .fontAwesomeRegular(.square))
// Stacked icons with smaller background
UIImage.init(bgIcon: .fontAwesomeSolid(.camera), topIcon: .fontAwesomeSolid(.ban), topTextColor: .red, bgLarge: false)
// Stacked icons with custom size
UIImage.init(bgIcon: .fontAwesomeSolid(.camera), topIcon: .fontAwesomeSolid(.ban), topTextColor: .red, bgLarge: false, size: CGSize(width: 50, height: 50))
import SwiftIcons
// Setting icon to image view
imageView.setIcon(icon: .weather(.rainMix))
// Icon with colors
imageView.setIcon(icon: .mapicons(.amusementPark), textColor: .white, backgroundColor: .blue, size: nil)
import SwiftIcons
// Setting icon to label
label.setIcon(icon: .ionicons(.paintbrush), iconSize: 70)
// Icon with colors
label.setIcon(icon: .googleMaterialDesign(.rowing), iconSize: 70, color: .white, bgColor: textColor)
// Icon with text around it
label.setIcon(prefixText: "Bus ", icon: .linearIcons(.bus), postfixText: " icon", size: 20)
// Icon with color & colored text around it
label.setIcon(prefixText: "Medal ", prefixTextColor: .red, icon: .ionicons(.ribbonA), iconColor: .red, postfixText: "", postfixTextColor: .red, size: nil, iconSize: 40)
// Icon with text with different fonts around it
label.setIcon(prefixText: "Font ", prefixTextFont: font1!, icon: .fontAwesomeSolid(.font), postfixText: " icon", postfixTextFont: font2!)
// Icon with text with different fonts & colors around it
label.setIcon(prefixText: "Bike ", prefixTextFont: font1!, prefixTextColor: .red, icon: .mapicons(.bicycling), iconColor: textColor, postfixText: " icon", postfixTextFont: font2!, postfixTextColor: .blue, iconSize: 30)
import SwiftIcons
// Setting icon to button
button.setIcon(icon: .linearIcons(.phone), forState: .normal)
// Icon with size and color
button.setIcon(icon: .openIconic(.clipboard), iconSize: 70, color: .blue, forState: .normal)
// Icon with text around it
button.setIcon(prefixText: "Please ", icon: .googleMaterialDesign(.print), postfixText: " print", forState: .normal)
// Icon with color & colored text around it
button.setIcon(prefixText: "Lock ", prefixTextColor: .red, icon: .googleMaterialDesign(.lock), iconColor: .yellow, postfixText: " icon", postfixTextColor: .blue, forState: .normal, textSize: 15, iconSize: 20)
// Icon with text with different fonts around it
button.setIcon(prefixText: "Happy ", prefixTextFont: font1!, icon: .ionicons(.happy), postfixText: " face", postfixTextFont: font2!, forState: .normal)
// Icon with text with different fonts & colors around it
button.setIcon(prefixText: "Pulse ", prefixTextFont: font1!, prefixTextColor: .darkGray, icon: .openIconic(.pulse), iconColor: .red, postfixText: " icon", postfixTextFont: font2!, postfixTextColor: .purple, forState: .normal, iconSize: 40)
// Icon with title below icon
button.setIcon(icon: .emoji(.ferrisWheel), title: "Ferris Wheel", color: .red, forState: .normal)
// Icon with title below icon with different color & custom font
button.setIcon(icon: .weather(.rainMix), iconColor: .yellow, title: "RAIN MIX", titleColor: .red, font: font!, backgroundColor: .clear, borderSize: 1, borderColor: .green, forState: .normal)
import SwiftIcons
// Setting icon at particular index
segmentedControl.setIcon(icon: .linearIcons(.thumbsUp), forSegmentAtIndex: 0)
segmentedControl.setIcon(icon: .linearIcons(.thumbsDown), forSegmentAtIndex: 1)
// Icons with sizes & colors
segmentedControl.setIcon(icon: .fontAwesomeSolid(.male), color: .red, iconSize: 50, forSegmentAtIndex: 0)
segmentedControl.setIcon(icon: .fontAwesomeSolid(.female), color: .purple, iconSize: 50, forSegmentAtIndex: 1)
import SwiftIcons
// Setting icon to tab bar item
tabBar.items?[0].setIcon(icon: .fontAwesomeSolid(.font), size: nil, textColor: .lightGray)
// Stacked icons for tab bar item
tabBar.items?[1].setIcon(bgIcon: .fontAwesomeRegular(.circle), bgTextColor: .lightGray, topIcon: .fontAwesomeSolid(.square), topTextColor: .lightGray, bgLarge: true, size: nil)
import SwiftIcons
// Change minimum & maximum value icons
slider.setMaximumValueIcon(icon: .emoji(.digitNine))
slider.setMinimumValueIcon(icon: .emoji(.digitZero))
// Change minimum & maximum value icons with colors
slider.setMaximumValueIcon(icon: .linearIcons(.pointerUp), customSize: nil, textColor: .red, backgroundColor: .clear)
slider.setMinimumValueIcon(icon: .linearIcons(.pointerDown), customSize: nil, textColor: .blue, backgroundColor: .clear)
import SwiftIcons
// Setting icon to bar button item
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30)
// Icon with colors
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30, color: textColor)
// Icon with custom cgRect
barButtonItem.setIcon(icon: .ionicons(.iosFootball), iconSize: 30, color: textColor, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), target: self, action: #selector(barButtonItem(sender:)))
// Icon with text around it
barButtonItem.setIcon(prefixText: "Please ", icon: .ionicons(.iosDownload), postfixText: " download", cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), size: 23, target: self, action: #selector(barButtonItem(sender:)))
// Icon with color & colored text around it
barButtonItem.setIcon(prefixText: "Blue ", prefixTextColor: .red, icon: .ionicons(.iosFootball), iconColor: .blue, postfixText: " football", postfixTextColor: .green, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), size: 20, iconSize: 30, target: self, action: #selector(barButtonItem(sender:)))
// Icon with text with different fonts around it
barButtonItem.setIcon(prefixText: "Digit ", prefixTextFont: font1!, icon: .emoji(.digitOne), postfixText: " One", postfixTextFont: font2!, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), target: self, action: #selector(barButtonItem(sender:)))
// Icon with text with different fonts & colors around it
barButtonItem.setIcon(prefixText: "", prefixTextFont: font1!, prefixTextColor: .red, icon: .ionicons(.iosFootball), iconColor: .blue, postfixText: " football", postfixTextFont: font2!, postfixTextColor: .green, cgRect: CGRect(x: 0, y: 0, width: 30, height: 30), iconSize: 24, target: self, action: #selector(barButtonItem(sender:)))
import SwiftIcons
// Setting icon to the title
self.setTitleIcon(icon: .emoji(.animalHorse), iconSize: 30, color: .red)
import SwiftIcons
// Setting left view icon
textfield.setLeftViewIcon(icon: .fontAwesomeSolid(.search))
// Left view icon with colors & leftViewMode
textfield.setLeftViewIcon(icon: .state(.TX), leftViewMode: .always, textColor: .blue, backgroundColor: .clear, size: nil)
textfield.setLeftViewIcon(icon: .googleMaterialDesign(.plusOne), leftViewMode: .unlessEditing, textColor: .green, backgroundColor: .clear, size: nil)
// Setting right view icon
textfield.setRightViewIcon(icon: .openIconic(.questionMark))
// Right view icon with colors & rightViewMode
textfield.setRightViewIcon(icon: .weather(.rainMix), rightViewMode: .always, textColor: .red, backgroundColor: .clear, size: nil)
import SwiftIcons
// Setting icons
stepper.setDecrementIcon(icon: .ionicons(.iosPause), forState: .normal)
stepper.setIncrementIcon(icon: .ionicons(.iosPlay), forState: .normal)
Please check out the SwiftIcons App. In the demo project, if you click on any object, you will see the method description in the logs for the icon of that object.
If you are using SwiftIcons in your app and want to be listed here, simply create a new issue here.
I am always curious who is using my projects đ
Author: Ranesr
Source Code: https://github.com/ranesr/SwiftIcons
License: MIT license
1596530868
Want to develop a website or re-design using CSS Development?
We build a website and we implemented CSS successfully if you are planning to Hire CSS Developer from HourlyDeveloper.io, We can fill your Page with creative colors and attractive Designs. We provide services in Web Designing, Website Redesigning and etc.
For more detailsâŚ!!
Consult with our experts:- https://bit.ly/3hUdppS
#hire css developer #css development company #css development services #css development #css developer #css