use of android.text.SpannedString in project react-native-navigation by wix.
the class TextViewUtils method getTextSize.
public static float getTextSize(TextView view) {
SpannedString text = new SpannedString(view.getText());
AbsoluteSizeSpan[] spans = text.getSpans(0, text.length(), AbsoluteSizeSpan.class);
return spans.length == 0 ? -1 : spans[0].getSize();
}
use of android.text.SpannedString in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class SpannableStringUtils method concatWithNonParagraphSuggestionSpansOnly.
/**
* Returns a CharSequence concatenating the specified CharSequences, retaining their
* SuggestionSpans that don't have the PARAGRAPH flag, but not other spans.
*
* This code is almost entirely taken from {@link TextUtils#concat(CharSequence...)}, except
* it calls copyNonParagraphSuggestionSpansFrom instead of {@link TextUtils#copySpansFrom}.
*/
public static CharSequence concatWithNonParagraphSuggestionSpansOnly(CharSequence... text) {
if (text.length == 0) {
return "";
}
if (text.length == 1) {
return text[0];
}
boolean spanned = false;
for (int i = 0; i < text.length; i++) {
if (text[i] instanceof Spanned) {
spanned = true;
break;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length; i++) {
sb.append(text[i]);
}
if (!spanned) {
return sb.toString();
}
SpannableString ss = new SpannableString(sb);
int off = 0;
for (int i = 0; i < text.length; i++) {
int len = text[i].length();
if (text[i] instanceof Spanned) {
copyNonParagraphSuggestionSpansFrom((Spanned) text[i], 0, len, ss, off);
}
off += len;
}
return new SpannedString(ss);
}
use of android.text.SpannedString in project android_packages_apps_Dialer by LineageOS.
the class DialerBidiFormatter method format.
/**
* Divides the given text into segments, applies LTR formatting and adds TTS span to segments that
* are phone numbers, then reassembles the text.
*
* <p>Formatted phone numbers usually contain one or more whitespaces (e.g., "+1 650-253-0000",
* "(650) 253-0000", etc). The system mistakes such a number for tokens separated by whitespaces.
* Therefore, these numbers can't be correctly shown in a RTL context (e.g., "+1 650-253-0000"
* would be shown as "650-253-0000 1+".)
*
* <p>This method wraps phone numbers with Unicode formatting characters LRE & PDF to ensure phone
* numbers are always shown as LTR strings.
*
* <p>Note that the regex used to find phone numbers ({@link Patterns#PHONE}) will also match any
* number. As this method also adds TTS span to segments that match {@link Patterns#PHONE}, extra
* actions need to be taken if you don't want a number to be read as a phone number by TalkBack.
*/
public static CharSequence format(@Nullable CharSequence text) {
if (TextUtils.isEmpty(text)) {
return text;
}
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
// Find the start index and the end index of each segment matching the phone number pattern.
Matcher matcher = Patterns.PHONE.matcher(text.toString());
int currIndex = 0;
while (matcher.find()) {
int start = matcher.start();
int end = matcher.end();
// Handle the case where the input text doesn't start with a phone number.
if (currIndex < start) {
spannableStringBuilder.append(text.subSequence(currIndex, start));
}
// For a phone number, wrap it with Unicode characters LRE & PDF so that it will always be
// shown as a LTR string.
spannableStringBuilder.append(PhoneNumberUtils.createTtsSpannable(TextUtils.concat(String.valueOf(LRE), text.subSequence(start, end), String.valueOf(PDF))));
currIndex = end;
}
// Handle the case where the input doesn't end with a phone number.
if (currIndex < text.length()) {
spannableStringBuilder.append(text.subSequence(currIndex, text.length()));
}
return new SpannedString(spannableStringBuilder);
}
use of android.text.SpannedString in project AndroidLife by CaMnter.
the class Util method getTextViewImageSpanBitmap.
private static List<Pair<String, Bitmap>> getTextViewImageSpanBitmap(TextView textView) {
List<Pair<String, Bitmap>> bitmaps = new ArrayList<>();
try {
CharSequence text = textView.getText();
if (text instanceof SpannedString) {
Field mSpansField = Class.forName("android.text.SpannableStringInternal").getDeclaredField("mSpans");
mSpansField.setAccessible(true);
Object[] spans = (Object[]) mSpansField.get(text);
for (Object span : spans) {
if (span instanceof ImageSpan) {
bitmaps.add(new Pair<>("SpanBitmap", getDrawableBitmap(((ImageSpan) span).getDrawable())));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmaps;
}
use of android.text.SpannedString in project SmartNews by JavenLu.
the class SearchView method setHint.
private void setHint() {
SpannableString string = new SpannableString(textHint);
AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(hintTextSize, false);
string.setSpan(absoluteSizeSpan, 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
editTextClear.setHint(new SpannedString(string));
}
Aggregations