के साथ NSAttributedString में रंग विशेषता को अनदेखा किया गया है NSAttributedString
में, अक्षरों की एक श्रृंखला में एक लिंक विशेषता और एक कस्टम रंग विशेषता है।NSLinkAttributeName
स्विफ्ट 2 के साथ Xcode 7 में, यह काम करता है:
स्विफ्ट 3 के साथ Xcode 8 में, लिंक के लिए कस्टम जिम्मेदार ठहराया रंग हमेशा (यह स्क्रीनशॉट में नारंगी होना चाहिए) नजरअंदाज कर दिया है।
यहाँ के परीक्षण के लिए कोड है।
स्विफ्ट 2, Xcode 7:
import Cocoa
import XCPlayground
let text = "Hey @user!"
let attr = NSMutableAttributedString(string: text)
let range = NSRange(location: 4, length: 5)
attr.addAttribute(NSForegroundColorAttributeName, value: NSColor.orangeColor(), range: range)
attr.addAttribute(NSLinkAttributeName, value: "http://somesite.com/", range: range)
let tf = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 50))
tf.allowsEditingTextAttributes = true
tf.selectable = true
tf.stringValue = text
tf.attributedStringValue = attr
XCPlaygroundPage.currentPage.liveView = tf
स्विफ्ट 3, Xcode 8:
import Cocoa
import PlaygroundSupport
let text = "Hey @user!"
let attr = NSMutableAttributedString(string: text)
let range = NSRange(location: 4, length: 5)
attr.addAttribute(NSForegroundColorAttributeName, value: NSColor.orange, range: range)
attr.addAttribute(NSLinkAttributeName, value: "http://somesite.com/", range: range)
let tf = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 50))
tf.allowsEditingTextAttributes = true
tf.isSelectable = true
tf.stringValue = text
tf.attributedStringValue = attr
PlaygroundPage.current.liveView = tf
मैं एप्पल के लिए एक बग रिपोर्ट भेज दिया है, लेकिन इस बीच में किसी के लिए एक विचार है, तो एक्सकोड 8 में एक फिक्स या वर्कअराउंड, यह बहुत अच्छा होगा।
जब आप का उपयोग 'NSTextView', आप' linkTextAttributes' संपत्ति सेट करना होगा। जब एक 'NSTextField' केंद्रित होता है और संपादित किया जा सकता है, तो पाठ' NSTextView' में दिखाया गया है जो इसके 'linkTextAttributes' (डिफ़ॉल्ट नीले, रेखांकित और एक हाथ कर्सर) का उपयोग करता है। – Willeke
@ विलेके धन्यवाद, दुर्भाग्य से मैंने लिंकटाक्स्ट एट्रिब्यूट को ओवरराइड करने का प्रयास कर लिया है, यह एक अच्छा समाधान है लेकिन यह जिम्मेदार स्ट्रिंग में सभी लिंक पर समान गुण लागू करता है, और मुझे अलग-अलग सेट करने में सक्षम होना चाहिए। बात यह है कि, * मेरे दूसरे प्रश्न का आपका जवाब हाल ही में काम करता है *, यहां बड़ी बड़ी बहन है कि आपने जिस सटीक कोड का उत्तर दिया है वह अब और काम नहीं करता है। :) – Moritz
ओह। लिंक एक्सकोड 7 में नारंगी है। यह एक्सकोड 8 में नीला है। मैं एक रडार दाखिल कर रहा हूं ... आपको इसके साथ परेशान करने के लिए खेद है, @ विलेके ... – Moritz