UITextView
電話番号とリンクを検出するがありましたが、これは私の電話番号を上書きしてfontColor
、に変更しblueColor
ます。自動検出されたリンクの色をフォーマットする方法はありますか、またはこの機能の手動バージョンを試す必要がありますか?
11 に答える
tintColor
iOS 7では、のを設定できますUITextView
。リンクの色だけでなく、カーソルの線と選択したテキストの色にも影響します。
iOS 7では、linkedに新しいプロパティも追加されましたUITextView
。linkTextAttributes
これにより、リンクスタイルを完全に制御できるようになります。
UIAppearance
プロトコルを使用して、すべてのテキストビューに変更を適用できます。
Swift 4.x:
UITextView.appearance().linkTextAttributes = [ .foregroundColor: UIColor.red ]
Swift 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()
変更されますが、、またはviewDidLoad()
viewControllerを呼び出すと変更されません。
アプリケーション全体の外観を設定したい場合は、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 /Swift4の例を次に示します。
// 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]];
Swift5回答
素晴らしくてシンプル
myTextView.linkTextAttributes = [.foregroundColor: UIColor.white]
Webビューを使用せずに実際に別の方法を見つけましたが、これはプライベート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では、色合いの色を設定することで、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;
}
次に、クラス'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>
これは私がSwift5を使用してそれを行った方法です:
let attributedString = NSMutableAttributedString(string: myTextView.text ?? "")
myTextView.linkTextAttributes = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.whiteColor] as [NSAttributedString.Key: Any]?
myTextView.attributedText = attributedString