Skip to content

Commit

Permalink
add underline style
Browse files Browse the repository at this point in the history
  • Loading branch information
hamasyou committed Apr 10, 2014
1 parent a336efe commit 880cfc2
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 134 deletions.
Binary file modified android/dist/android.jar
Binary file not shown.
Empty file added android/lib/.gitkeep
Empty file.
Binary file modified android/libs/armeabi-v7a/libcom.bongole.ti.alabel.so
Binary file not shown.
Binary file modified android/libs/armeabi/libcom.bongole.ti.alabel.so
Binary file not shown.
Binary file modified android/libs/x86/libcom.bongole.ti.alabel.so
Binary file not shown.
53 changes: 29 additions & 24 deletions android/src/com/bongole/ti/alabel/AttributedLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
Expand All @@ -31,25 +32,25 @@ public AttributedLabel(TiViewProxy proxy) {

private void setAttributedText(TextView tv, HashMap<String, Object> attro){
tv.setMovementMethod(LinkMovementMethod.getInstance());

String txt = (String)attro.get("text");
SpannableString stxt = new SpannableString(txt);

Object[] attributes = (Object[]) attro.get("attributes");
for( Object a : attributes ){
Object[] aa = (Object[])a;
Integer start = (Integer) aa[0];
Integer end = (Integer) aa[1];
HashMap<String, Object> attr_inner = (HashMap<String, Object>) aa[2];

if( attr_inner.containsKey("font") ){
HashMap<String, Object> font_attr = (HashMap<String, Object>) attr_inner.get("font");

String fontSize = null;
String fontWeight = null;
String fontFamily = null;
String fontColor = null;

if (font_attr.containsKey("fontSize")) {
fontSize = TiConvert.toString(font_attr, "fontSize");
}
Expand All @@ -62,24 +63,24 @@ private void setAttributedText(TextView tv, HashMap<String, Object> attro){
if (font_attr.containsKey("color")) {
fontColor = TiConvert.toString(font_attr, "color");
}

if( fontFamily != null ){
stxt.setSpan(new CustomTypefaceSpan(fontFamily, TiUIHelper.toTypeface(tv.getContext(),fontFamily)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

if( fontWeight != null ){
stxt.setSpan(new StyleSpan(TiUIHelper.toTypefaceStyle(fontWeight)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

if( fontSize != null ){
stxt.setSpan(new AbsoluteSizeSpan((int)TiUIHelper.getRawSize(fontSize, tv.getContext())), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

if( fontColor != null ){
stxt.setSpan(new ForegroundColorSpan(TiConvert.toColor(fontColor)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}

if( attr_inner.containsKey("a") ){
HashMap<String, Object> a_attr = (HashMap<String, Object>) attr_inner.get("a");
String href = (String)a_attr.get("href");
Expand All @@ -89,21 +90,25 @@ private void setAttributedText(TextView tv, HashMap<String, Object> attro){

@Override
public void onClick(View arg0) {
KrollDict event = new KrollDict();
KrollDict event = new KrollDict();
event.put("src", urlspan.getURL());

proxy.fireEvent("link", event);
}
});

stxt.setSpan(urlspan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}

if ( attr_inner.containsKey("u") ) {
stxt.setSpan(new UnderlineSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}

tv.setText(revertSpans(stxt));
}

final Spannable revertSpans(Spanned stext) {
Object[] spans = stext.getSpans(0, stext.length(), Object.class);
Spannable ret = Spannable.Factory.getInstance().newSpannable(stext.toString());
Expand All @@ -115,32 +120,32 @@ final Spannable revertSpans(Spanned stext) {

return ret;
}

@Override
public void processProperties(KrollDict d) {
super.processProperties(d);

TextView tv = (TextView)this.getNativeView();

if( d.containsKey("attributedText") ){
HashMap<String, Object> attro = d.getKrollDict("attributedText");
setAttributedText(tv, attro);

tv.invalidate();
}
}

@Override
public void propertyChanged(String key, Object oldValue, Object newValue,
KrollProxy proxy) {
super.propertyChanged(key, oldValue, newValue, proxy);

if( key.equals("attributedText")){
TextView tv = (TextView)this.getNativeView();

setAttributedText(tv, (HashMap<String, Object>) newValue);
tv.invalidate();

tv.invalidate();
}
}
}
5 changes: 5 additions & 0 deletions iphone/Classes/ComBongoleTiAlabelLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ -(void)setAttributedText_:(id)args
if (url) {
[mas setLink:[NSURL URLWithString:url] range:r];
}
},
@"u": ^(NSMutableAttributedString *mas, id arg, NSRange r){
[mas addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInteger:NSUnderlineStyleSingle]
range:r];
}
} retain];
}
Expand Down
Loading

0 comments on commit 880cfc2

Please sign in to comment.