How to change link color in UITextView in xamarin.ios

后端 未结 1 1287
滥情空心
滥情空心 2021-01-27 12:52

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

   var urlString = @\"Google\";
                


        
相关标签:
1条回答
  • 2021-01-27 13:15

    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;
    
    0 讨论(0)
提交回复
热议问题