如何在 xamarin.ios 中的 UITextView 中更改链接颜色 [英] How to change link color in UITextView in xamarin.ios

查看:33
本文介绍了如何在 xamarin.ios 中的 UITextView 中更改链接颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用这段代码在 UITextView 中创建了一个可点击的文本

We have created a clickable text in UITextView by using this code

   var urlString = @"<a href=""https://www.google.com"" >Google</a>";
        var documentAttributes = new NSAttributedStringDocumentAttributes { DocumentType = NSDocumentType.HTML };
        NSError error = null;
        var attributedString = new NSAttributedString(NSData.FromString(urlString, NSStringEncoding.UTF8), documentAttributes, ref error);
        // Should really check the NSError before applying

        MyTextView.AttributedText = attributedString;

但它默认为链接和带下划线的文本显示蓝色.我们要更改文本的颜色并删除下划线.请指导/帮助我实现这一点.

but its showing by default blue color for the link and underlined text. We want to change the color for the text and also remove underline. Please guide/help me to implement this.

推荐答案

您可以更改这些属性,只需将 UIStringAttributeKey.ForegroundColor 和 UIStringAttributeKey.UnderlineStyl 添加到您的字典并将其设置为 WeakLinkTextAttributes 属性

You can change these properties just adding UIStringAttributeKey.ForegroundColor and UIStringAttributeKey.UnderlineStyl to your dictionary and set it to WeakLinkTextAttributes property

var key1 = UIStringAttributeKey.ForegroundColor;
var value1 = UIColor.Red;

var key2 = UIStringAttributeKey.UnderlineStyle;
var value2 = new NSNumber(0); // 0 without underline 1 with underline

var dict = new NSDictionary(key1, value1, key2, value2);

var urlString = @"<a href=""https://www.google.com"" >Google</a>";
var documentAttributes = new NSAttributedStringDocumentAttributes { 
           DocumentType = NSDocumentType.HTML };
NSError error = null;
var attributedString = new NSAttributedString(NSData.FromString(urlString, NSStringEncoding.UTF8), documentAttributes, ref error);

yourTextView.AttributedText = attributedString;
yourTextView.WeakLinkTextAttributes = dict;

这篇关于如何在 xamarin.ios 中的 UITextView 中更改链接颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆