मेरे पास एक UITextView
फोन नंबर और लिंक का पता लगा था , लेकिन यह मेरी fontColor
और इसे बदल देता है blueColor
। क्या ऑटो का पता लगाने वाले लिंक के रंग को प्रारूपित करने का एक तरीका है, या मुझे इस फ़ंक्शन का मैन्युअल संस्करण आज़माना चाहिए?
मेरे पास एक UITextView
फोन नंबर और लिंक का पता लगा था , लेकिन यह मेरी fontColor
और इसे बदल देता है blueColor
। क्या ऑटो का पता लगाने वाले लिंक के रंग को प्रारूपित करने का एक तरीका है, या मुझे इस फ़ंक्शन का मैन्युअल संस्करण आज़माना चाहिए?
जवाबों:
IOS 7 पर आप सेट कर सकते हैं tintColor
की UITextView
। यह लिंक रंग के साथ-साथ कर्सर लाइन और चयनित टेक्स्ट रंग को भी प्रभावित करता है।
iOS 7 ने एक नई प्रॉपर्टी भी जोड़ी है UITextView
जिसे कॉल linkTextAttributes
किया जाएगा जो आपको लिंक शैली को पूरी तरह से नियंत्रित करने के लिए प्रकट होगा।
NSFontAttributeName
साथ ओवरराइड करने में सक्षम नहीं था linkTextAttributes
। मुझे फ़ॉन्ट को मैन्युअल रूप से उसी सीमा में निर्दिष्ट करना था जैसे कि मेरीNSLinkAttributeName
UITextView का उपयोग करने के बजाय, मैंने UIWebView का उपयोग किया और "ऑटो-डिटेक्ट लिंक" को सक्षम किया। लिंक का रंग बदलने के लिए, बस टैग के लिए एक नियमित सीएसएस बनाया।
मैंने कुछ इस तरह इस्तेमाल किया:
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() ]
उद्देश्य सी:
[UITextView appearance].linkTextAttributes = @{ NSForegroundColorAttributeName : UIColor.redColor };
के लिए उपस्थिति UITextView
प्रलेखित नहीं है, लेकिन अच्छी तरह से काम करती है।
ध्यान रखें UIAppearance
:
जब कोई विंडो में प्रवेश करता है, तो iOS उपस्थिति परिवर्तन लागू करता है, यह उस दृश्य का स्वरूप नहीं बदलता है जो पहले से ही विंडो में है। उस दृश्य का स्वरूप बदलने के लिए जो वर्तमान में एक विंडो में है, दृश्य पदानुक्रम से दृश्य निकालें और फिर उसे वापस रख दें।
दूसरे शब्दों में: इस कोड को कॉल init()
या init(coder:)
विधियों में UI ऑब्जेक्ट उपस्थिति बदल जाएगी , लेकिन कॉलिंग loadView()
या viewDidLoad()
व्यू कॉन्ट्रोलर में नहीं होगा ।
यदि आप पूरे एप्लिकेशन के लिए उपस्थिति सेट करना चाहते हैं, application(_:didFinishLaunchingWithOptions:)
तो ऐसे कोड को कॉल करने के लिए अच्छी जगह है।
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 = [:]
मैंने वेबव्यू का उपयोग किए बिना वास्तव में एक और तरीका पाया लेकिन ध्यान रखें कि यह निजी एपीआई का उपयोग करता है और इसे ऐपस्टोर में अस्वीकार किया जा सकता है:
संपादित करें: मेरे ऐप को ऐप्पल द्वारा अनुमोदित किया गया है, हालांकि निजी एपीआई उपयोग!
पहले तरीकों के साथ 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];
}
}
स्विफ्ट 5 उत्तर
अच्छा और सरल
myTextView.linkTextAttributes = [.foregroundColor: UIColor.white]
संपादित करें:
इसके साथ यह मत करो 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;
}
अब आप 'htmlcopy' वर्ग का उपयोग html पेज में इस तरह करें-
<b>Fax:</b><a href="tel:646.200.7535" class="headercopy"> 646-200-7535</a><br />
यह उस फ़ोन नंबर को प्रदर्शित करेगा जिस रंग में आपको क्लिक कार्यक्षमता के साथ की आवश्यकता है।
यह कोड एक आई-फोन पर एक फोन नंबर का रंग सेट करेगा, लेकिन स्वचालित कॉल लिंक से बचता है।
<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>
इस तरह मैंने इसे स्विफ्ट 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