https://github.com/polok/TaggerString
TaggerString is very light library which allows to build dynamic string resource in much more readable way.
https://github.com/polok/TaggerString
Last synced: 6 months ago
JSON representation
TaggerString is very light library which allows to build dynamic string resource in much more readable way.
- Host: GitHub
- URL: https://github.com/polok/TaggerString
- Owner: polok
- Created: 2014-10-13T16:45:01.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-04-03T07:14:05.000Z (almost 11 years ago)
- Last Synced: 2024-08-04T10:08:29.338Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 374 KB
- Stars: 240
- Watchers: 10
- Forks: 16
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-android-ui - https://github.com/polok/TaggerString
- awesome-android-libraries - TaggerString
README
TaggerString
============
TaggerString is very light library which allows to build dynamic string resource in much more readable way.
[](https://www.apache.org/licenses/LICENSE-2.0) [](https://android-arsenal.com/details/1/1048)
I guess that every Android developer has seen or added similar piece of code in *string.xml* file:
```xml
I am %s and I am %d years old`
```
Some probably discovered that above code is a little bit not readable and extended such declarations to something like this:
```xml
I am $1%s and I am $2%d years old
```
And in source code:
```java
String userDesc = String.format(resources.getString(R.string.user_description_string), "John Kowalski", 27);
```
So far so good but maybe this could be a little bit more readable ? Yes, it can be (at least in my opinion) - **TaggerString**.
In source code above example will look like :
```java
TaggerString taggerString = TaggerString.from(getString(R.string.user_description_string));
taggerString.with("user_name", "John Kowalski");
taggerString.with("user_age", 27);
tvExampleOne.setText(taggerString.format());
```
And our string resource looks
```xml
My name is {user_name} and I am a {developer_role}
```
If you want to use **bold** (TaggerStyleType.BOLD), *italic* (TaggerStyleType.ITALIC) or underscore (TaggerStyleType.UNDERLINE), please go ahead:
```java
TaggerString taggerString = TaggerString.from(getString(R.string.user_description_string));
taggerString.with("user_name", "John Kowalski", TaggerStyleType.BOLD);
taggerString.with("user_age", 27, TaggerStyleType.UNDERLINE);
tvExampleOne.setText(taggerString.formatCustom());
```
Please note that in this case method *formatCustom()* returns *Spanned* object.
##### Multiple formatting (since version 1.1.0)
If you want to apply few tags for one string go ahead and do it:
```java
taggerString.with("developer_role", "Android Software Developer", TaggerStyleType.UNDERLINE, TaggerStyleType.BOLD, TaggerStyleType.ITALIC);
```
Usage
----------
If you are using gradle please add below:
```xml
dependencies {
compile 'com.github.polok.taggerstring:library:1.0.0'
}
```
####
Since version 1.1.0 the artefactId was changed from library to taggerstring:
```xml
dependencies {
compile 'com.github.polok.taggerstring:taggerstring:1.1.0'
}
```
Developed By
------------
Marcin Polak - mpolak87(at).gmail.com
License
----------
```
Copyright 2015 Marcin Polak
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```