変更したいので色の自動検出したリンクUITextView?
-
20-09-2019 - |
質問
あったか UITextView
を検出する電話番号、リンクが、この私のオーバーライド fontColor
に置き換えることができ blueColor
.があるので、フォーマットの色を自動検出のリンクをはっていマニュアル版で機能しているのでしょうか。
解決
のiOS 7上では、tintColor
のUITextView
を設定することができます。これは、リンクの色、並びにカーソル線と選択されたテキストの色に影響を与えます。
のiOS 7はまた、あなたが完全にリンクのスタイルを制御できるように思われると呼ばれるUITextView
をlinkTextAttributes
する新しいプロパティを追加します。
他のヒント
、私はの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];
あなたはすべてのテキストビューのために変更を適用するために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() ]
のObjective-C:
[UITextView appearance].linkTextAttributes = @{ NSForegroundColorAttributeName : UIColor.redColor };
UITextView
の外観は文書化されますが、うまく機能していません。
心のUIAppearance
ノートに保管してくださいます:
iOSのビューがウィンドウに入ったとき、それがウィンドウにすでにのビューの外観を変更しない外観の変更を適用します。ウィンドウ内の現在のビューの外観を変更するには、ビュー階層からビューを削除し、それを元に戻すます。
言い換えれば:
init()
にこのコードを呼び出し、またはinit(coder:)
法の の変更UIオブジェクトの外観でしょうが、 にしませんloadView()
、またはのViewControllerのviewDidLoad()
に呼び出す。
あなたは、アプリケーション全体の外観を設定したい場合は、application(_:didFinishLaunchingWithOptions:)
は、このようなコードを呼び出すための良い場所です。
次でのTextViewでハイパーリンクの色を変更することができます:
nibファイルでは、プロパティウィンドウに移動し、これまでにあなたがする色にどの色合いを変更することができます。
か、また、以下のコードを使用してプログラム的にそれを行うことができます。
[YOURTEXTVIEW setTintColor:[UIColor whiteColor]];
UITextViewのlinkTextAttributes
の問題は、それがすべて自動的に検出されたリンクに適用されることです。あなたがしたい場合は、別のリンクが異なる属性を持っているか?
これは結局のところトリックがあります:。、のテキストビューの帰属テキストの一部としてリンクを設定し、空の辞書にlinkTextAttributes
を設定するの
ここでiOSの11 /スウィフト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 = [:]
色合いの色も、ストーリーボードで行うことができる
また実際には別の方法で を使用せずにwebview がることに注意してください使用 プライベートAPI や断りすることにappstore:
編集: 私のアプリ承認されたappleは、プライベート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さWebiviewなどによdivsできても、なお一層の色のそれぞれのリンクタイプ別
<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きの変更が容易にリンク色のUITextViews設定により色合い。きませ------------
- (id)contentAsHTMLString;
もうだ。ない場合は以下のサポートしたいのiOS7以下
- (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;
}
現在利用中のクラス'headercopy"をごhtmlページにこのように-
<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>