简体   繁体   English

使用链接IOS更改属性文本颜色

[英]Change attributed text color with link IOS

How I can change the attributed text color from blue to white? 如何将属性文本颜色从蓝色更改为白色?

When I click on the UITextView the app opens the url, I just need to change the color to white. 当我点击UITextView应用程序打开网址时,我只需要将颜色更改为白色。

My code is in C# but I think it can be converted to swift or objective-c easily. 我的代码在C#中,但我认为它可以很容易地转换为swift或objective-c。

I have tried this way but it didn't work: 我试过这种方式,但它不起作用:

NSMutableAttributedString footerText = new NSMutableAttributedString(myFooterText, new UIStringAttributes
        {
            ForegroundColor = UIColor.White,

            Link = new NSUrl(myLinkString)
        });

        //Set footer text
        MyTextView.AttributedText = footerText;

在此输入图像描述

Sample code snippet 示例代码段

UIStringAttributes attrHyperlink= new UIStringAttributes();
attrHyperlink.UnderlineStyle = NSUnderlineStyle.Single;
attrHyperlink.ForegroundColor = UIColor.Purple.CGColor;


NSMutableAttributedString attString = new NSMutableAttributedString(StringValue);
attString.AddAttributes(attrHyperlink, new NSRange(0,StringValue.Length));
MyTextView.AttributedText = attString;

Try this 尝试这个

Check this Attribute string for Link 检查此链接的属性字符串

NSString *str = @"Link";
 NSMutableAttributedString *aStr = [[NSMutableAttributedString 
 alloc]initWithString:str attributes:nil];
[aStr addAttribute:NSLinkAttributeName value:@"Your Link here" 
range:[str rangeOfString:@"Link"]];
[UITextView appearance].linkTextAttributes = @{ NSForegroundColorAttributeName : UIColor.redColor };
[self.text_View setAttributedText:aStr];
[self.text_View setTextAlignment:NSTextAlignmentNatural];

我通过使用我的自定义文本颜色和下划线实现普通文本视图来修复它。在用户单击时我用我的URL打开浏览器。

在此输入图像描述 See the image attached i feel you want something like this. 看到附上的图片我觉得你想要这样的东西。 Use following code snippet. 使用以下代码段。 It is working on my end 它正在我的努力

self.infoTextView.text = @"I am text. I am link.";
  NSString *info = self.infoTextView.text;
    NSRange commaRange = [info rangeOfString:@"I am link."];

    NSMutableAttributedString *infoString = [[NSMutableAttributedString alloc] initWithString:info];
    [infoString addAttribute: NSLinkAttributeName value: @"http://www.google.com" range: commaRange];
    self.infoTextView.tintColor = [UIColor colorWithRed:255.0f/255.0f green:181.0f/255.0f blue:51.0f/255.0f alpha:1.0]; //link color 
    [infoString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:61.0/255.0 green:82.0/225.0 blue:96.0/255.0 alpha:1.0] range:(NSRange){0, [info length]}]; //original text color

    [infoString addAttribute:NSFontAttributeName value:self.infoTextView.font range:(NSRange){0, [info length]}];
    self.infoTextView.attributedText = infoString;

The accepted answer is more of a workaround than a fix. 接受的答案更多的是解决方法而不是修复方法。

To change the link-color I used the code from Dhaval and changed it to Xamarin.iOS. 要更改链接颜色,我使用了Dhaval中的代码并将其更改为Xamarin.iOS。 Use the TintColor attribute like so: 像这样使用TintColor属性:

var textColor = (Color)Element.GetValue(Label.TextColorProperty);
str.AddAttribute(UIStringAttributeKey.ForegroundColor, textColor.ToUIColor(Color.White), new NSRange(0, str.Length)); // Normal text color
str.AddAttribute(UIStringAttributeKey.Link, new NSUrl(item.Link), new NSRange(item.Start, item.Text.Length)); // Add a link.
            }
Control.TintColor = textColor.ToUIColor(Color.Wheat); // The LINK color
Control.AttributedText = str;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM