Как изменить цвет ссылки в UITextView в xamarin.ios - PullRequest
0 голосов
/ 01 октября 2018

Мы создали интерактивный текст в UITextView, используя этот код

   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;

, но по умолчанию отображается синий цвет для ссылки и подчеркнутого текста.Мы хотим изменить цвет текста, а также удалить подчеркивание.Пожалуйста, направьте / помогите мне реализовать это.

1 Ответ

0 голосов
/ 01 октября 2018

Вы можете изменить эти свойства, просто добавив UIStringAttributeKey.ForegroundColor и UIStringAttributeKey.UnderlineStyl в свой словарь и установите для него свойство WeakLinkTextAttributes

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;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...