Hadi Al Tinawi
Hadi Al Tinawi

Reputation: 429

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.

My code is in C# but I think it can be converted to swift or objective-c easily.

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;

enter image description here

Upvotes: 3

Views: 2861

Answers (5)

Yuri
Yuri

Reputation: 2038

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. Use the TintColor attribute like so:

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;

Upvotes: 1

Hadi Al Tinawi
Hadi Al Tinawi

Reputation: 429

I fixed it by implementing normal text view with my custom text color and the underline, On user click I open the browser with my url.

Upvotes: 3

Dhaval Soni
Dhaval Soni

Reputation: 56

enter image description hereSee 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;

Upvotes: 1

Inder_iOS
Inder_iOS

Reputation: 1656

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];

Upvotes: 2

Elstine P
Elstine P

Reputation: 370

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

Upvotes: 2

Related Questions