Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for TypefaceSpan #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions FlowTextView.iml
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<module external.linked.project.id="FlowTextView" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

</module>
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ Add the dependency:
compile 'com.github.deano2390:FlowTextView:2.0.5'
```

Install to local maven, from `flowtextview/`:
```
gradle clean build publishToMavenLocal
```


License
-------
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.android.tools.build:gradle:2.3.0'
}
}

Expand Down
10 changes: 5 additions & 5 deletions flowtextview-sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 21
buildToolsVersion '21.1.2'
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
applicationId 'com.dean.flowtextviewsampleapp.sample'
minSdkVersion 8
targetSdkVersion 19
minSdkVersion 9
targetSdkVersion 25
versionCode 1
versionName '1.0'
}
Expand All @@ -22,6 +22,6 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(':flowtextview')
}
25 changes: 22 additions & 3 deletions flowtextview/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "source"
}

publishing {
publications {
maven(MavenPublication) {
groupId 'com.github.deano2390'
artifactId 'FlowTextView'
version '2.0.8'

artifact(sourceJar)
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
}
}
}

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
compileSdkVersion 25
buildToolsVersion '25.0.2'

defaultConfig {
minSdkVersion 8
targetSdkVersion 15
targetSdkVersion 25
}

buildTypes {
Expand All @@ -16,6 +34,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}

}


5 changes: 0 additions & 5 deletions flowtextview/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@
package="uk.co.deanwild.flowtextview.flowtextview"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

</manifest>
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.co.deanwild.flowtextview;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Canvas;
Expand Down Expand Up @@ -92,6 +91,7 @@ private void init(Context context, AttributeSet attrs) {
mLinkPaint.setColor(Color.BLUE);
mLinkPaint.setUnderlineText(true);
this.setBackgroundColor(Color.TRANSPARENT);
setOnTouchListener(mClickHandler);
}

private void readAttrs(Context context, AttributeSet attrs) {
Expand Down Expand Up @@ -139,14 +139,22 @@ protected void onDraw(Canvas canvas) {

mSpanParser.reset();

boolean lastBlockWasCountedAsEmptyLine = false;

for (int block_no = 0; block_no <= blocks.length - 1; block_no++) // at the highest level we iterate through each 'block' of text
{
String thisBlock = blocks[block_no];
if (thisBlock.length() <= 0) { //is a line break
lineIndex++; // we need a new line
charOffsetEnd += 2;
charOffsetStart = charOffsetEnd;
if (lastBlockWasCountedAsEmptyLine) {
lastBlockWasCountedAsEmptyLine = false;
} else {
lineIndex++; // we need a new line
charOffsetEnd += 2;
charOffsetStart = charOffsetEnd;
lastBlockWasCountedAsEmptyLine = true;
}
} else { // is some actual text
lastBlockWasCountedAsEmptyLine = false;

while (thisBlock.length() > 0) { // churn through the block spitting it out onto seperate lines until there is nothing left to render
lineIndex++; // we need a new line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import uk.co.deanwild.flowtextview.listeners.OnLinkClickListener;
Expand All @@ -12,13 +11,13 @@
/**
* Created by Dean on 24/06/2014.
*/
public class ClickHandler implements View.OnTouchListener{
public class ClickHandler implements View.OnTouchListener {

private final SpanParser mSpanParser;
private OnLinkClickListener mOnLinkClickListener;
private OnLinkClickListener mOnLinkClickListener = OnLinkClickListener.DEFAULT;

private double distance = 0;
private float x1,y1,x2,y2 = 0f;
private float x1, y1, x2, y2 = 0f;

public ClickHandler(SpanParser spanParser) {
this.mSpanParser = spanParser;
Expand All @@ -29,30 +28,30 @@ public boolean onTouch(View view, MotionEvent event) {

int event_code = event.getAction();

if(event_code == MotionEvent.ACTION_DOWN){
if (event_code == MotionEvent.ACTION_DOWN) {
distance = 0;
x1 = event.getX();
y1 = event.getY();
}

if(event_code == MotionEvent.ACTION_MOVE){
if (event_code == MotionEvent.ACTION_MOVE) {
x2 = event.getX();
y2 = event.getY();
distance = getPointDistance(x1, y1, x2, y2);
}

if(distance < 10) { // my random guess at an acceptable drift distance to regard this as a click
if (distance < 10) { // my random guess at an acceptable drift distance to regard this as a click
if (event_code == MotionEvent.ACTION_UP) {
// if the event is an "up" and we havn't moved far since the "down", then it's a click
return onClick(event.getX(), event.getY()); // process the click and say whether we consumed it
return onClick(view, event.getX(), event.getY()); // process the click and say whether we consumed it
}
return true;
}

return false;
}

private boolean onClick(float x, float y){
private boolean onClick(final View view, float x, float y) {

List<HtmlLink> links = mSpanParser.getLinks();

Expand All @@ -62,10 +61,10 @@ private boolean onClick(float x, float y){
float brX = link.xOffset + link.width;
float brY = link.yOffset + link.height;

if(x > tlX && x < brX){
if(y > tlY && y < brY){
if (x > tlX && x < brX) {
if (y > tlY && y < brY) {
// collision
onLinkClick(link.url);
onLinkClick(view, link.url);
return true; // the click was consumed
}
}
Expand All @@ -74,12 +73,12 @@ private boolean onClick(float x, float y){
return false;
}

private void onLinkClick(String url){
if(mOnLinkClickListener!=null) mOnLinkClickListener.onLinkClick(url);
private void onLinkClick(final View view, String url) {
if (mOnLinkClickListener != null) mOnLinkClickListener.onLinkClick(view, url);
}

private static double getPointDistance(float x1, float y1, float x2, float y2){
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1- y2, 2));
private static double getPointDistance(float x1, float y1, float x2, float y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}

public OnLinkClickListener getOnLinkClickListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.text.Spannable;
import android.text.TextPaint;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.URLSpan;

import java.util.ArrayList;
Expand Down Expand Up @@ -112,11 +113,12 @@ public float parseSpans(List<HtmlObject> objects, Object[] spans, int lineStart,
}

private HtmlObject parseSpan(Object span, String content, int start, int end){

if(span instanceof URLSpan){
return getHtmlLink((URLSpan) span, content, start, end, 0);
}else if(span instanceof StyleSpan){
}else if(span instanceof StyleSpan) {
return getStyledObject((StyleSpan) span, content, start, end, 0);
} else if (span instanceof TypefaceSpan) {
return getTypefaceObject((TypefaceSpan) span, content, start, end, 0);
}else{
return getHtmlObject(content, start, end, 0);
}
Expand All @@ -135,6 +137,19 @@ private HtmlObject getStyledObject(StyleSpan span, String content, int start, in
return obj;
}

private HtmlObject getTypefaceObject(final TypefaceSpan span, final String content, final int start, final int end, final float thisXOffset) {
TextPaint paint = mPaintHelper.getPaintFromHeap();
paint.setTextSize(mFlowTextView.getTextsize());
paint.setColor(mFlowTextView.getColor());

span.updateDrawState(paint);
span.updateMeasureState(paint);

HtmlObject obj = new HtmlObject(content, start, end, thisXOffset, paint);
obj.recycle = true;
return obj;
}

private HtmlObject getHtmlObject(String content, int start, int end, float thisXOffset){
return new HtmlObject(content, start, end, thisXOffset, mFlowTextView.getTextPaint());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package uk.co.deanwild.flowtextview.listeners;

import android.content.Intent;
import android.net.Uri;
import android.view.View;

/**
* Created by Dean on 24/06/2014.
*/
* Created by Dean on 24/06/2014.
*/
public interface OnLinkClickListener {
public void onLinkClick(String url);

OnLinkClickListener DEFAULT = new OnLinkClickListener() {
@Override
public void onLinkClick(View view, String url) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
};

void onLinkClick(View view, String url);
}
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Jan 30 11:01:50 CET 2015
#Wed Mar 08 10:15:45 EET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip