使用 link IOS 更改属性文本颜色
Change attributed text color with link IOS
如何将属性文本颜色从蓝色更改为白色?
当我点击 UITextView 时,应用会打开 url,我只需要将颜色更改为白色。
我的代码是用 C# 编写的,但我认为它可以很容易地转换为 swift 或 objective-c。
我试过这种方法,但没有用:
NSMutableAttributedString footerText = new NSMutableAttributedString(myFooterText, new UIStringAttributes
{
ForegroundColor = UIColor.White,
Link = new NSUrl(myLinkString)
});
//Set footer text
MyTextView.AttributedText = footerText;
示例代码片段
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;
试试这个
检查 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];
看附图我觉得你想要这样的东西。使用以下代码片段。它正在为我服务
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;
我通过使用我的自定义文本颜色和下划线实现普通文本视图来修复它,在用户单击时我用我的 url.
打开浏览器
接受的答案更像是一种解决方法,而不是修复方法。
为了更改 link-颜色,我使用了 Dhaval 的代码并将其更改为 Xamarin.iOS。像这样使用 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;
如何将属性文本颜色从蓝色更改为白色?
当我点击 UITextView 时,应用会打开 url,我只需要将颜色更改为白色。
我的代码是用 C# 编写的,但我认为它可以很容易地转换为 swift 或 objective-c。
我试过这种方法,但没有用:
NSMutableAttributedString footerText = new NSMutableAttributedString(myFooterText, new UIStringAttributes
{
ForegroundColor = UIColor.White,
Link = new NSUrl(myLinkString)
});
//Set footer text
MyTextView.AttributedText = footerText;
示例代码片段
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;
试试这个
检查 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];
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;
我通过使用我的自定义文本颜色和下划线实现普通文本视图来修复它,在用户单击时我用我的 url.
打开浏览器接受的答案更像是一种解决方法,而不是修复方法。
为了更改 link-颜色,我使用了 Dhaval 的代码并将其更改为 Xamarin.iOS。像这样使用 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;