androidx.annotation.FontRes Java Examples

The following examples show how to use androidx.annotation.FontRes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FontsManager.java    From CommonUtils with Apache License 2.0 5 votes vote down vote up
@NonNull
public static Typeface get(@NonNull Context context, @FontRes int res) {
    if (instance == null) instance = new FontsManager();

    Typeface typeface = instance.cache.get(res);
    if (typeface == null) {
        typeface = ResourcesCompat.getFont(context, res);
        if (typeface == null) throw new IllegalArgumentException("Font doesn't exist!");
        instance.cache.put(res, typeface);
    }

    return typeface;
}
 
Example #2
Source File: ResourcesCompat.java    From Carbon with Apache License 2.0 5 votes vote down vote up
/**
 * Used by TintTypedArray.
 *
 * @hide
 */
@RestrictTo(LIBRARY_GROUP_PREFIX)
public static Typeface getFont(@NonNull Context context, @FontRes int id, TypedValue value,
                               int style, int weight, @Nullable androidx.core.content.res.ResourcesCompat.FontCallback fontCallback) throws NotFoundException {
    if (context.isRestricted()) {
        return null;
    }
    return loadFont(context, id, value, style, weight, fontCallback, null /* handler */,
            true /* isXmlRequest */);
}
 
Example #3
Source File: FontsManager.java    From CommonUtils with Apache License 2.0 4 votes vote down vote up
public static void set(@FontRes int res, @NonNull TextView... views) {
    for (TextView view : views) view.setTypeface(get(view.getContext(), res));
}
 
Example #4
Source File: FontsManager.java    From CommonUtils with Apache License 2.0 4 votes vote down vote up
public static void set(@NonNull Context context, @NonNull Paint paint, @FontRes int res) {
    paint.setTypeface(get(context, res));
}
 
Example #5
Source File: SuperTextView.java    From CommonUtils with Apache License 2.0 4 votes vote down vote up
public void setTypeface(@FontRes int res) {
    FontsManager.set(res, this);
}
 
Example #6
Source File: SuperTextView.java    From CommonUtils with Apache License 2.0 4 votes vote down vote up
public Builder typeface(@FontRes int res) {
    view.setTypeface(res);
    return this;
}
 
Example #7
Source File: ResourcesCompat.java    From Carbon with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a font Typeface associated with a particular resource ID.
 * <p>
 * This method will block the calling thread to retrieve the requested font, including if it
 * is from a font provider. If you wish to not have this behavior, use
 * {@link #getFont(Context, int, FontCallback, Handler)} instead.
 * <p>
 * Prior to API level 23, font resources with more than one font in a family will only load the
 * font closest to a regular weight typeface.
 *
 * @param context A context to retrieve the Resources from.
 * @param id      The desired resource identifier of a {@link Typeface},
 *                as generated by the aapt tool. This integer encodes the
 *                package, type, and resource entry. The value 0 is an invalid
 *                identifier.
 * @return A font Typeface object.
 * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
 * @see #getFont(Context, int, FontCallback, Handler)
 */
@Nullable
public static Typeface getFont(@NonNull Context context, @FontRes int id)
        throws NotFoundException {
    if (context.isRestricted()) {
        return null;
    }
    return loadFont(context, id, new TypedValue(), Typeface.NORMAL, 400, null /* callback */,
            null /* handler */, false /* isXmlRequest */);
}
 
Example #8
Source File: ResourcesCompat.java    From Carbon with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a font Typeface associated with a particular resource ID asynchronously.
 * <p>
 * Prior to API level 23, font resources with more than one font in a family will only load the
 * font closest to a regular weight typeface.
 * </p>
 *
 * @param context      A context to retrieve the Resources from.
 * @param id           The desired resource identifier of a {@link Typeface}, as generated by the aapt
 *                     tool. This integer encodes the package, type, and resource entry. The value 0 is an
 *                     invalid identifier.
 * @param fontCallback A callback to receive async fetching of this font. The callback will be
 *                     triggered on the UI thread.
 * @param handler      A handler for the thread the callback should be called on. If null, the
 *                     callback will be called on the UI thread.
 * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
 */
public static void getFont(@NonNull Context context, @FontRes int id,
                           @NonNull androidx.core.content.res.ResourcesCompat.FontCallback fontCallback, @Nullable Handler handler)
        throws NotFoundException {
    Preconditions.checkNotNull(fontCallback);
    if (context.isRestricted()) {
        fontCallback.callbackFailAsync(
                FontRequestCallback.FAIL_REASON_SECURITY_VIOLATION, handler);
        return;
    }
    loadFont(context, id, new TypedValue(), Typeface.NORMAL, 400, fontCallback, handler,
            false /* isXmlRequest */);
}