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()
}
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"
)
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")
}
}
}