Update to better FontAwesome functions

https://github.com/finnmglas/fontawesome-android
This commit is contained in:
Finn M Glas 2020-05-21 12:55:26 +02:00
parent 53a43f3eb2
commit 68b690aa31
No known key found for this signature in database
GPG key ID: 25037A2E81AB459C
3 changed files with 58 additions and 82 deletions

View file

@ -7,87 +7,45 @@ import android.graphics.Typeface
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
/** [FontAwesome] is just a type of TextView with special functions:
*
* `setText(str)` can be used to change the icon
* `setIconType(Int)` changes the FontAwesome style ("solid", "regular" or "brand")
* `setTextColor(Int)` changes the color
* `setTextSize(Int, Float)` changes the icon size
*/
class FontAwesomeSolid : AppCompatTextView {
constructor(
context: Context?,
attrs: AttributeSet?,
defStyle: Int
) : super(context, attrs, defStyle) {
init()
class FontAwesome : AppCompatTextView {
var type = "" // "solid", "regular" or "brand"
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int)
: super(context, attrs, defStyle) { init(attrs) }
constructor(context: Context?, attrs: AttributeSet?)
: super(context, attrs) { init(attrs) }
constructor(context: Context?)
: super(context) { init(null) }
private fun init(attrs: AttributeSet?) {
if (attrs != null) {
val a = context!!.obtainStyledAttributes(attrs, R.styleable.FontAwesome)
if (a.hasValue(R.styleable.FontAwesome_type))
type = a.getString(R.styleable.FontAwesome_type)!!
a.recycle()
if (type == "") type = "solid"
}
setIconType(type)
}
constructor(context: Context?, attrs: AttributeSet?) : super(
context,
attrs
) {
init()
}
// Useful if you want to change between a regular and solid icon (example: star)
fun setIconType(iconType : String){
type = iconType
constructor(context: Context?) : super(context) {
init()
typeface = when (type) {
"regular" -> Typeface.createFromAsset(context!!.assets,"fontawesome/fa-regular-400.ttf")
"solid" -> Typeface.createFromAsset(context!!.assets,"fontawesome/fa-solid-900.ttf")
"brands" -> Typeface.createFromAsset(context!!.assets,"fontawesome/fa-brands-400.ttf")
else -> Typeface.createFromAsset(context!!.assets,"fontawesome/fa-solid-900.ttf")
}
private fun init() {
typeface = Typeface.createFromAsset(
context.assets,
"fontawesome/fa-solid-900.ttf"
)
}
}
class FontAwesomeRegular : AppCompatTextView {
constructor(
context: Context?,
attrs: AttributeSet?,
defStyle: Int
) : super(context, attrs, defStyle) {
init()
}
constructor(context: Context?, attrs: AttributeSet?) : super(
context,
attrs
) {
init()
}
constructor(context: Context?) : super(context) {
init()
}
private fun init() {
typeface = Typeface.createFromAsset(
context.assets,
"fontawesome/fa-regular-400.ttf"
)
}
}
class FontAwesomeBrand : AppCompatTextView {
constructor(
context: Context?,
attrs: AttributeSet?,
defStyle: Int
) : super(context, attrs, defStyle) {
init()
}
constructor(context: Context?, attrs: AttributeSet?) : super(
context,
attrs
) {
init()
}
constructor(context: Context?) : super(context) {
init()
}
private fun init() {
typeface = Typeface.createFromAsset(
context.assets,
"fontawesome/fa-brands-400.ttf"
)
}
}

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This declares attributes for the FontAwesome TextView -->
<resources>
<declare-styleable name="FontAwesome">
<attr name="type" format="string" />
</declare-styleable>
</resources>

View file

@ -1,11 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- icons that can be used with type="solid" -->
<string name="fas_bars" translatable="false">&#xf0c9;</string>
<string name="fas_home" translatable="false">&#xf015;</string>
<string name="fas_globe" translatable="false">&#xf0ac;</string>
<string name="fas_settings" translatable="false">&#xf013;</string>
<string name="fas_close_window" translatable="false">&#xf410;</string>
<string name="fas_star" translatable="false">&#xf005;</string>
<string name="fas_globe" translatable="false">&#xf0ac;</string>
<string name="fas_ad" translatable="false">&#xf641;</string>
<string name="fas_backspace" translatable="false">&#xf55a;</string>
<string name="fas_calendar" translatable="false">&#xf133;</string>
<string name="far_star" translatable="false">&#xf005;</string>
<!-- icons that can be used with type="brands" -->
<string name="fab_apple" translatable="false">&#xf179;</string>
<string name="fab_instagram" translatable="false">&#xf16d;</string>
<!-- icons that can be used with type="solid" and type="regular" -->
<string name="fa_close_window" translatable="false">&#xf410;</string>
<string name="fa_square" translatable="false">&#xf0c8;</string>
<string name="fa_star" translatable="false">&#xf005;</string>
<string name="fa_clipboard" translatable="false">&#xf328;</string>
</resources>