From 375ba1e0992b241128ed97d77db4e80643c3f4f2 Mon Sep 17 00:00:00 2001 From: Anastasiia Stroganova Date: Wed, 20 May 2020 16:44:22 +0300 Subject: [PATCH] Add new attrs calendar_month_text_size and calendar_month_text_style. --- .../ru/cleverpumpkin/calendar/CalendarView.kt | 16 ++++++++++++++++ .../calendar/adapter/CalendarAdapter.kt | 5 +++++ .../calendar/style/CalendarStyleAttributes.kt | 5 +++++ .../style/CalendarStyleAttributesReader.kt | 10 ++++++++++ crunchycalendar/src/main/res/values/attrs.xml | 5 +++++ sample/src/main/res/values/dimens.xml | 4 ++++ 6 files changed, 45 insertions(+) create mode 100644 sample/src/main/res/values/dimens.xml diff --git a/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/CalendarView.kt b/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/CalendarView.kt index 2e49e8f..19c5be1 100644 --- a/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/CalendarView.kt +++ b/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/CalendarView.kt @@ -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. */ diff --git a/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/adapter/CalendarAdapter.kt b/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/adapter/CalendarAdapter.kt index d34e497..8e39798 100644 --- a/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/adapter/CalendarAdapter.kt +++ b/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/adapter/CalendarAdapter.kt @@ -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 @@ -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 diff --git a/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/style/CalendarStyleAttributes.kt b/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/style/CalendarStyleAttributes.kt index a2d7d8d..f45e2b8 100644 --- a/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/style/CalendarStyleAttributes.kt +++ b/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/style/CalendarStyleAttributes.kt @@ -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 @@ -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, diff --git a/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/style/CalendarStyleAttributesReader.kt b/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/style/CalendarStyleAttributesReader.kt index 9d24fe8..591e678 100644 --- a/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/style/CalendarStyleAttributesReader.kt +++ b/crunchycalendar/src/main/java/ru/cleverpumpkin/calendar/style/CalendarStyleAttributesReader.kt @@ -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 diff --git a/crunchycalendar/src/main/res/values/attrs.xml b/crunchycalendar/src/main/res/values/attrs.xml index 432d080..dcd684e 100644 --- a/crunchycalendar/src/main/res/values/attrs.xml +++ b/crunchycalendar/src/main/res/values/attrs.xml @@ -15,6 +15,11 @@ + + + + + diff --git a/sample/src/main/res/values/dimens.xml b/sample/src/main/res/values/dimens.xml new file mode 100644 index 0000000..0bcd52f --- /dev/null +++ b/sample/src/main/res/values/dimens.xml @@ -0,0 +1,4 @@ + + + 16sp + \ No newline at end of file