123

UITextView電話番号とリンクを検出するがありましたが、これは私の電話番号を上書きしてfontColor、に変更しblueColorます。自動検出されたリンクの色をフォーマットする方法はありますか、またはこの機能の手動バージョンを試す必要がありますか?

4

11 に答える 11

184

tintColoriOS 7では、のを設定できますUITextView。リンクの色だけでなく、カーソルの線と選択したテキストの色にも影響します。

iOS 7では、linkedに新しいプロパティも追加されましたUITextViewlinkTextAttributesこれにより、リンクスタイルを完全に制御できるようになります。

于 2013-09-27T10:30:22.043 に答える
44

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:)そのようなコードを呼び出すのに適しています。

于 2016-04-25T16:01:32.233 に答える
37

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];
于 2010-03-15T23:53:35.197 に答える
20

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 = [:]
于 2017-10-03T18:28:05.710 に答える
15

次の方法で、TextViewのハイパーリンクの色を変更できます。

Nibファイルで、[プロパティ]ウィンドウに移動し、色合いを任意の色に変更できます。

または、以下のコードを使用してプログラムで実行することもできます

[YOURTEXTVIEW setTintColor:[UIColor whiteColor]];
于 2013-11-08T06:30:11.997 に答える
12

Swift5回答

素晴らしくてシンプル

myTextView.linkTextAttributes = [.foregroundColor: UIColor.white]
于 2019-12-06T14:45:02.543 に答える
5

絵コンテでも色合いが作れます

ここに画像の説明を入力してください

于 2018-04-15T14:45:09.427 に答える
3

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

}
}
于 2013-06-25T10:01:54.047 に答える
0

編集: でそれをしないでください、代わり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 />

これにより、クリック機能で必要な色で電話番号が表示されます。

于 2010-10-23T08:46:08.797 に答える
0

このコードは、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&nbsp;&nbsp;0232 963 959</a></div>
于 2015-06-29T09:21:15.913 に答える
-1

これは私が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
于 2019-12-02T17:16:16.957 に答える