我有一个UITextView
检测电话号码和链接的,但这会覆盖我fontColor
并将其更改为blueColor
. 有没有办法格式化自动检测到的链接的颜色,或者我应该尝试这个功能的手动版本?
11 回答
tintColor
在 iOS 7 上,您可以设置UITextView
. 它会影响链接颜色以及光标线和选定的文本颜色。
iOS 7 还为UITextView
调用添加了一个新属性,linkTextAttributes
看起来可以让您完全控制链接样式。
您可以使用UIAppearance
协议为所有文本视图应用更改:
斯威夫特 4.x:
UITextView.appearance().linkTextAttributes = [ .foregroundColor: UIColor.red ]
斯威夫特 3.x:
UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.red ]
斯威夫特 2.x:
UITextView.appearance().linkTextAttributes = [ NSForegroundColorAttributeName: UIColor.redColor() ]
目标-C:
[UITextView appearance].linkTextAttributes = @{ NSForegroundColorAttributeName : UIColor.redColor };
外观UITextView
没有记录,但效果很好。
请记住注意UIAppearance
事项:
iOS 在视图进入窗口时应用外观更改,它不会更改已经在窗口中的视图的外观。要更改当前位于窗口中的视图的外观,请从视图层次结构中删除该视图,然后将其放回原处。
换句话说:在init()
, orinit(coder:)
方法中调用此代码将改变 UI 对象的外观,但在viewController中调用loadView()
, or不会。
如果您想为整个应用程序设置外观,是调用此类代码的好地方。viewDidLoad()
application(_:didFinishLaunchingWithOptions:)
我没有使用 UITextView,而是使用了 UIWebView 并启用了“自动检测链接”。要更改链接颜色,只需为标签创建一个常规 CSS。
我用过这样的东西:
NSString * htmlString = [NSString stringWithFormat:@"<html><head><script> document.ontouchmove = function(event) { if (document.body.scrollHeight == document.body.clientHeight) event.preventDefault(); } </script><style type='text/css'>* { margin:0; padding:0; } p { color:black; font-family:Helvetica; font-size:14px; } a { color:#63B604; text-decoration:none; }</style></head><body><p>%@</p></body></html>", [update objectForKey:@"text"]];
webText.delegate = self;
[webText loadHTMLString:htmlString baseURL:nil];
UITextView 的问题linkTextAttributes
在于它适用于所有自动检测到的链接。如果您希望不同的链接具有不同的属性怎么办?
事实证明有一个技巧:将链接配置为文本视图的属性文本的一部分,并将 设置linkTextAttributes
为空字典。
这是 iOS 11 / Swift 4 中的示例:
// mas is the mutable attributed string we are forming...
// ... and we're going to use as the text view's `attributedText`
mas.append(NSAttributedString(string: "LINK", attributes: [
NSAttributedStringKey.link : URL(string: "https://www.apple.com")!,
NSAttributedStringKey.foregroundColor : UIColor.green,
NSAttributedStringKey.underlineStyle : NSUnderlineStyle.styleSingle.rawValue
]))
// ...
self.tv.attributedText = mas
// this is the important thing:
self.tv.linkTextAttributes = [:]
您可以通过以下方式更改 TextView 中的超链接颜色:
在 Nib 文件中,您可以转到属性窗口并将色调更改为您想要的任何颜色。
或者您也可以使用以下代码以编程方式进行
[YOURTEXTVIEW setTintColor:[UIColor whiteColor]];
斯威夫特 5 答案
漂亮又简单
myTextView.linkTextAttributes = [.foregroundColor: UIColor.white]
我确实找到了另一种不使用 webview 的方法,但请记住,这使用私有 API,并且可能在 appstore 中被拒绝:
编辑:我的应用程序得到了苹果的批准,尽管私人 api 使用!
首先使用方法在 UITextView 上声明一个类别
- (id)contentAsHTMLString;
- (void)setContentToHTMLString:(id)arg1;
他们只是在做以下事情:
- (id)contentAsHTMLString;
{
return [super contentAsHTMLString];
}
- (void)setContentToHTMLString:(id)arg1;
{
[super setContentToHTMLString:arg1];
}
现在写一个彩色链接的方法:
- (void) colorfillLinks;
{
NSString *contentString = [self.textViewCustomText contentAsHTMLString];
contentString = [contentString stringByReplacingOccurrencesOfString:@"x-apple-data-detectors=\"true\""
withString:@"x-apple-data-detectors=\"true\" style=\"color:white;\""];
[self.textViewCustomText setContentToHTMLString:contentString];
}
它确实在所有类型的链接上使用特定颜色设置样式属性。
UITextViews 像通过 div 一样呈现 Webiview,因此您甚至可以更进一步并分别为每个链接类型着色:
<div><a href="http://www.apple.com" x-apple-data-detectors="true" style="color:white;" x-apple-data-detectors-type="link" x-apple-data-detectors-result="0">http://www.apple.com</a></div>
这x-apple-data-detectors-type="link"
是链接的确切类型的指示器
编辑
iOS7
对此不再起作用。在 iOS7 中,您可以通过设置 tint color 轻松更改 UITextViews 的链接颜色。你不应该打电话
- (id)contentAsHTMLString;
再说了,你会得到一个例外。如果您想支持 iOS 7 及更低版本,请改为执行以下操作:
- (void) colorfillLinks;
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
self.tintColor = [UIColor colorWithRed:79.0/255.0
green:168.0/255.0
blue:224.0/255.0
alpha:1.0];
} else if(![self isFirstResponder ]) {
NSString *contentString = [self contentAsHTMLString];
contentString = [contentString stringByReplacingOccurrencesOfString:@"x-apple-data-detectors=\"true\""
withString:@"x-apple-data-detectors=\"true\" style=\"color:#DDDDDE;\""];
[self setContentToHTMLString:contentString];
}
}
编辑:
不要这样做UITextView
,而是使用UIWebView
。
您需要为此制作一个样式表。使用您需要的颜色组合在那里定义一个类-
.headercopy {
font-family: "Helvetica";
font-size: 14px;
line-height: 18px;
font-weight:bold;
color: #25526e;
}
a.headercopy:link {
color:#ffffff;
text-decoration:none;
}
a.headercopy:hover {
color:#00759B;
text-decoration:none;
}
a.headercopy:visited {
color:#ffffff;
text-decoration:none;
}
a.headercopy:hover {
color:#00759B;
text-decoration:none;
}
现在像这样在你的html页面中使用类'headercopy' -
<b>Fax:</b><a href="tel:646.200.7535" class="headercopy"> 646-200-7535</a><br />
这将使用单击功能以您需要的颜色显示电话号码。
此代码将设置 I-Phone 上电话号码的颜色,但会使自动呼叫链接无效。
<div><a href="#" x-apple-data-detectors="true" style="color:white;" x-apple-data-detectors-type="link" x-apple-data-detectors-result="0">p 0232 963 959</a></div>
这就是我使用 Swift 5 的方式:
let attributedString = NSMutableAttributedString(string: myTextView.text ?? "")
myTextView.linkTextAttributes = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.whiteColor] as [NSAttributedString.Key: Any]?
myTextView.attributedText = attributedString