Skip to content

Commit

Permalink
Merge pull request #40 from CleverPumpkin/feature/new_custom_style
Browse files Browse the repository at this point in the history
Add new attrs calendar_month_text_size and calendar_month_text_style.
  • Loading branch information
Limanskaya authored May 21, 2020
2 parents 8c3f707 + 375ba1e commit f2b2df0
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,22 @@ class CalendarView @JvmOverloads constructor(
setMonthTextColor(getColorInt(colorRes))
}

/**
* Sets the month text size in SP.
*/
fun setMonthTextSize(size: Int) {
calendarStyleAttributes.monthTextSize = resources.getDimension(size)
calendarAdapter.notifyDataSetChanged()
}

/**
* Sets the month text style typeface.
*/
fun setMonthTextStyle(style: Int) {
calendarStyleAttributes.monthTextStyle = style
calendarAdapter.notifyDataSetChanged()
}

/**
* Sets a date cell background resource.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ru.cleverpumpkin.calendar.adapter

import android.content.Context
import android.graphics.Typeface
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -153,6 +156,8 @@ internal class CalendarAdapter(
val monthName = monthFormatter.format(monthItem.date.date)
holder.textView.text = monthName.capitalize()
holder.textView.setTextColor(styleAttributes.monthTextColor)
holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, styleAttributes.monthTextSize)
holder.textView.setTypeface(Typeface.DEFAULT, styleAttributes.monthTextStyle)
}

// endregion View Binding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ru.cleverpumpkin.calendar.style

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Typeface
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -37,6 +38,10 @@ internal class CalendarStyleAttributes(
@ColorInt
var monthTextColor: Int = context.getColorInt(R.color.calendar_month_text_color),

var monthTextSize: Float = context.resources.getDimension(R.dimen.calendar_month_text_size),

var monthTextStyle: Int = Typeface.NORMAL,

@DrawableRes
var dateCellBackgroundColorRes: Int = R.drawable.calendar_date_bg_selector,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ internal object CalendarStyleAttributesReader {
monthTextColor
)

monthTextSize = typedArray.getDimension(
R.styleable.CalendarView_calendar_month_text_size,
monthTextSize
)

monthTextStyle = typedArray.getInt(
R.styleable.CalendarView_calendar_month_text_style,
monthTextStyle
)

dateCellBackgroundColorRes = typedArray.getResourceId(
R.styleable.CalendarView_calendar_date_background,
dateCellBackgroundColorRes
Expand Down
5 changes: 5 additions & 0 deletions crunchycalendar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<attr name="calendar_year_selection_arrows_color" format="color" />
<attr name="calendar_year_selection_text_color" format="color" />
<attr name="calendar_month_text_color" format="color" />
<attr name="calendar_month_text_size" format="dimension" />
<attr name="calendar_month_text_style">
<enum name="normal" value="0" />
<enum name="bold" value="1" />
</attr>
<attr name="calendar_date_background" format="reference" />
<attr name="calendar_date_text_color" format="reference" />
</declare-styleable>
Expand Down
4 changes: 4 additions & 0 deletions sample/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="custom_calendar_month_text_size">16sp</dimen>
</resources>

0 comments on commit f2b2df0

Please sign in to comment.