मैं अपने ऐप पर एक क्लिक करने योग्य लेबल बनाना चाहता हूं जो मुझे एक सफारी वेबपेज पर ले जाता है। मैं यह भी चाहता हूं कि उपयोगकर्ता केवल उन पर क्लिक करके नंबरों को फोन करने में सक्षम हो?URL/फ़ोन-क्लिक करने योग्य UILabel कैसे बनाएं?
अपने सलाह
मैं अपने ऐप पर एक क्लिक करने योग्य लेबल बनाना चाहता हूं जो मुझे एक सफारी वेबपेज पर ले जाता है। मैं यह भी चाहता हूं कि उपयोगकर्ता केवल उन पर क्लिक करके नंबरों को फोन करने में सक्षम हो?URL/फ़ोन-क्लिक करने योग्य UILabel कैसे बनाएं?
अपने सलाह
के लिए धन्यवाद आप एक UITextView
का उपयोग करें और लिंक, फोन नंबर और निरीक्षक में अन्य बातों के लिए जांच चुन सकते हैं।
आप कस्टम यूआईबटन और सेटटेक्स्ट को जो भी चाहें बना सकते हैं और उसके साथ विधि जोड़ सकते हैं।
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)];
[sampleButton setTitle:@"URL Text" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
-(void)buttonPressed:(id)sender{
// open url
}
https://github.com/mattt/TTTAttributedLabel
यह निश्चित रूप से आप क्या जरूरत है। आप अपने लेबल के लिए विशेषताओं को भी लागू कर सकते हैं, जैसे अंडरलाइन, और इसमें विभिन्न रंग लागू करें। बस क्लिक करने योग्य यूआरएल के लिए निर्देशों की जांच करें।
मुख्य रूप से, आप निम्न की तरह कुछ कार्य करें:
NSRange range = [label.text rangeOfString:@"me"];
[label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range]; // Embedding a custom link in a substring
यह शानदार है, धन्यवाद! कई तरीकों से UITextView का उपयोग करने से काफी बेहतर है। – FreeNickname
स्विफ्ट का उपयोग करना: 'रेंज = (लेबल.टेक्स्ट एनएसएसटींग के रूप में)। रेंजऑफस्ट्रिंग ("मुझे") ' – nodebase
उपयोग UITextView
बजाय UILabel
और यह एक संपत्ति अपने पाठ हाइपरलिंक करना
Objective C:
yourTextView.editable = NO;
yourTextView.dataDetectorTypes = UIDataDetectorTypeAll;
Swift:
yourTextView.editable = false;
yourTextView.dataDetectorTypes = UIDataDetectorTypes.All;
कृपया अधिक विवरण के लिए documentation पालन कन्वर्ट करने के लिए है। यह स्वचालित रूप से लिंक का पता लगाएगा।
आशा है कि यह आपकी मदद करेगा। धन्यवाद :)
इसके लिए धन्यवाद! – Rob
हमेशा आपका स्वागत है :) –
यह रंग नीला रंग देता है और इसे रेखांकित करता है, लेकिन कम से कम आईओएस 7 पर इसे क्लिक करने योग्य लगता है। –
उपयोग इस मैं इसे इतना पसंद आया के बाद से पूरे लेबल पाठ पर केवल नहीं विशेष पाठ करने के लिए नीले रंग के साथ लिंक बनाता है: FRHyperLabel
करने के लिए:
उपरोक्त लिंक से डाउनलोड करें और अपनी परियोजना में FRHyperLabel.h
, FRHyperLabel.m
कॉपी करें।
Storyboard
और परिभाषित कस्टम वर्ग निरीक्षक की पहचान के रूप में छवि में दिखाया गया में FRHyperLabel
के नाम पर खींचें ड्रॉप UILabel
।
@property (weak, nonatomic) IBOutlet FRHyperLabel *label;
`एनएसएसटींग * स्ट्रिंग = @" अपलोड करके मैं उपयोग की शर्तों से सहमत हूं "; NSDictionary * विशेषताएँ = @ {NSFontAttributeName: [UIFont पसंदीदाFontForTextStyle: UIFontTextStyleHeadline]};
_label.attributedText = [[NSAttributedString alloc]initWithString:string attributes:attributes];
[_label setFont:[_label.font fontWithSize:13.0]];
[_label setLinkForSubstring:@"Terms of Use" withLinkHandler:^(FRHyperLabel *label, NSString *substring){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
}];`
उपयोग हाइपरलिंक करना
स्विफ्ट कोड एक संपत्ति अपने पाठ परिवर्तित करने के लिए है: यदि आप चाहते हैं तो इस UILabel द्वारा नियंत्रित किया जा करने के लिए और नहीं UITextView तो
yourTextView.editable = false
yourTextView.dataDetectorTypes = UIDataDetectorTypes.All
//or
yourTextView.dataDetectorTypes = UIDataDetectorTypes.PhoneNumber
//or
yourTextView.dataDetectorTypes = UIDataDetectorTypes.Link
, आप UILabel उपवर्ग कर सकते हैं, इस तरह एक:
class LinkedLabel: UILabel {
fileprivate let layoutManager = NSLayoutManager()
fileprivate let textContainer = NSTextContainer(size: CGSize.zero)
fileprivate var textStorage: NSTextStorage?
override init(frame aRect:CGRect){
super.init(frame: aRect)
self.initialize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initialize()
}
func initialize(){
let tap = UITapGestureRecognizer(target: self, action: #selector(LinkedLabel.handleTapOnLabel))
self.isUserInteractionEnabled = true
self.addGestureRecognizer(tap)
}
override var attributedText: NSAttributedString?{
didSet{
if let _attributedText = attributedText{
self.textStorage = NSTextStorage(attributedString: _attributedText)
self.layoutManager.addTextContainer(self.textContainer)
self.textStorage?.addLayoutManager(self.layoutManager)
self.textContainer.lineFragmentPadding = 0.0;
self.textContainer.lineBreakMode = self.lineBreakMode;
self.textContainer.maximumNumberOfLines = self.numberOfLines;
}
}
}
func handleTapOnLabel(tapGesture:UITapGestureRecognizer){
let locationOfTouchInLabel = tapGesture.location(in: tapGesture.view)
let labelSize = tapGesture.view?.bounds.size
let textBoundingBox = self.layoutManager.usedRect(for: self.textContainer)
let textContainerOffset = CGPoint(x: ((labelSize?.width)! - textBoundingBox.size.width) * 0.5 - textBoundingBox.origin.x, y: ((labelSize?.height)! - textBoundingBox.size.height) * 0.5 - textBoundingBox.origin.y)
let locationOfTouchInTextContainer = CGPoint(x: locationOfTouchInLabel.x - textContainerOffset.x, y: locationOfTouchInLabel.y - textContainerOffset.y)
let indexOfCharacter = self.layoutManager.characterIndex(for: locationOfTouchInTextContainer, in: self.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
self.attributedText?.enumerateAttribute(NSLinkAttributeName, in: NSMakeRange(0, (self.attributedText?.length)!), options: NSAttributedString.EnumerationOptions(rawValue: UInt(0)), using:{
(attrs: Any?, range: NSRange, stop: UnsafeMutablePointer<ObjCBool>) in
if NSLocationInRange(indexOfCharacter, range){
if let _attrs = attrs{
UIApplication.shared.openURL(URL(string: _attrs as! String)!)
}
}
})
}}
इस वर्ग इस answer से पुन: उपयोग कोड द्वारा किया गया था । जिम्मेदार तार बनाने के लिए यह answer देखें। और here आप फोन यूआरएल कैसे बना सकते हैं।
सही। मुझे इसकी ही खोज थी! मैं आपका उत्तर पोस्ट करने के लिए आप पर इंतजार कर रहा हूं। –
extension UITapGestureRecognizer {
func didTapAttributedTextInLabel(label: UILabel, inRange targetRange: NSRange) -> Bool {
let layoutManager = NSLayoutManager()
let textContainer = NSTextContainer(size: CGSize.zero)
let textStorage = NSTextStorage(attributedString: label.attributedText!)
// Configure layoutManager and textStorage
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
// Configure textContainer
textContainer.lineFragmentPadding = 0.0
textContainer.lineBreakMode = label.lineBreakMode
textContainer.maximumNumberOfLines = label.numberOfLines
textContainer.size = label.bounds.size
// main code
let locationOfTouchInLabel = self.location(in: label)
let indexOfCharacter = layoutManager.characterIndex(for: locationOfTouchInLabel, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
let indexOfCharacterRange = NSRange(location: indexOfCharacter, length: 1)
let indexOfCharacterRect = layoutManager.boundingRect(forGlyphRange: indexOfCharacterRange, in: textContainer)
let deltaOffsetCharacter = indexOfCharacterRect.origin.x + indexOfCharacterRect.size.width
if locationOfTouchInLabel.x > deltaOffsetCharacter {
return false
} else {
return NSLocationInRange(indexOfCharacter, targetRange)
}
}
}
स्विफ्ट 4.0 संभव समाधान UIButton
का उपयोग कर phoneButton = UIButton(frame: CGRect(x: view.frame.width * 0, y: view.frame.height * 0.1, width: view.frame.width * 1, height: view.frame.height * 0.05))
phoneButton.setTitle("333-333-3333", for: .normal)
phoneButton.setTitleColor(UIColor(red: 0/255, green: 0/255, blue: 238/255, alpha: 1.0), for: .normal)
phoneButton.addTarget(self, action: #selector(self.callPhone), for: .touchUpInside)
@objc func callPhone(){
UIApplication.shared.open(URL(string:"tel://3333333333")!, options: [:] , completionHandler: nil)
}
की
संभावित डुप्लिकेट [नल करने योग्य "लिंक" एक UILabel की NSAttributedText में बनाएं?] (Http://stackoverflow.com/questions/1256887/create-tap-able-links-in-the-nsattributedtext-of-a-uilabel) – NAlexN