如何在Deeplink消息中添加自定义文本而不是URL

时间:2016-12-22 08:44:17

标签: android ios firebase deep-linking firebase-dynamic-links

我正在整合Firebase以支持应用中的Deeplink功能。我在屏幕示例建议(PFA)here中看到,我们可以添加自己的自定义文本,而不是显示深层链接网址。

enter image description here

我试图更新,但没有帮助。怎么做,有什么建议吗?

2 个答案:

答案 0 :(得分:0)

我的猜测是,您在用户输入文本时使用UITextView(在iOS中)。以下是如何在Swift中实现这一点:

class ViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var textField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonPress(_ sender: Any) {
        let range = textView.selectedRange
        let linkString = NSMutableAttributedString(string: textView.text)
        linkString.addAttribute(NSLinkAttributeName, value: textField.text ?? "", range: range)
        textView.attributedText = linkString
    }

}

extension ViewController: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        UIApplication.shared.open(URL, options: [:], completionHandler: nil)
        return false
    }
}

在Android中,我发现了几个似乎很好地处理这个主题的SO答案:

Make a hyperlink textview in android

来自user370305:

  

试试这个,让我知道发生了什么......

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));

Put html link in edittext android

来自RobinHood:

  

将你的html放在字符串@

<string url="link">&lt;a href="http://www.google.com">Google&lt;/a></string>
     

将String设置为editText @

youredittext.setText(Html.fromHtml(getResources().getString(R.string.url)));
     

单击,使用必要的操作@

设置LinkMovementMethod
youredittext.setMovementMethod(LinkMovementMethod.getInstance());

如果您尝试在HTML中完成此操作,请使用锚点(a)标记:

<p>Click on this <a href="https://stackoverflow.com/">Link</a></p>

答案 1 :(得分:0)

我在其中一个应用中做了同样的事情。请遵循以下代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/margin"
    tools:context="tdsolutions.com.clickabletext.MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World this text" />

</LinearLayout>

将Textview传递给下面的方法(setClickableText):

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  TextView text = (TextView) findViewById(R.id.text);
  setClickableText(text);
}

private void setClickableText(TextView textView) {
        String text = "this"; // the text you want to mark as a link

        SpannableString ss = new SpannableString(textView.getText());// full text
        final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.parseColor("#91ca46"));//link text color

        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                //What ever the code you want when click on text goes here
                Toast.makeText(MainActivity.this,"you clicked",Toast.LENGTH_LONG).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        };

        int start = textView.getText().toString().indexOf(text);
        int length = start + text.length();

        ss.setSpan(clickableSpan, start, length, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ss.setSpan(fcs, start, length, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        textView.setText(ss);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setHighlightColor(Color.TRANSPARENT);
    }

或者您可以使用以下代码:

添加如下文本视图,根据需要更改任何属性。

   <TextView
        android:id="@+id/textView"
        android:layout_centerInParent="true"
        android:linksClickable="true"
        android:textColorLink="#00f"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

然后添加以下代码:

TextView textView = (TextView) findViewById(R.id.textView);
Spanned result;
String html = "Hi sweetheart go to <a href=\"http://www.google.com\">this</a> link";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    result = Html.fromHtml(html ,Html.FROM_HTML_MODE_LEGACY);
} else {
    result = Html.fromHtml(html);
}
textView.setText(result);
textView. setMovementMethod(LinkMovementMethod.getInstance());

请确保在添加链接时将其替换为html标记,就像我在示例中所做的那样String html = "Hi sweetheart go to <a href=\"http://www.google.com\">this</a> link";