मुझे पता चला है कि UILabel (here) में अक्षर अंतर कैसे सेट करें, लेकिन यह विधि UIButtons के लिए काम नहीं कर रही है। क्या कोई जानता है इसे कैसे करना है? 'NSConcreteAttributedString' (0x19e508660) to 'NSMutableAttributedString' (0x19e506a40).
स्विफ्ट में UIButton के अक्षर अंतर को कैसे बदलें?
उत्तर
NSAttributedString
बनाने की तरह सवाल में आप अपनेUIButton
पर लिंक
- कॉल
setAttributedTitle(_ ,forState:)
:यहाँ कोड मैं
let buttonString = agreementButton.attributedTitleForState(.Normal) as! NSMutableAttributedString buttonString.addAttribute(NSKernAttributeName, value: 1.0, range: NSMakeRange(0, buttonString.length)) agreementButton.setAttributedTitle(buttonString, forState: .Normal)
उपयोग कर रहा हूँ यह मुझे एक त्रुटि फेंकता है
इस (untested) का प्रयास करें:
let title = agreementButton.titleForState(.Normal)
let attributedTitle = NSAttributedString(string: title, attributes: [NSKernAttributeName: 1.0])
agreementButton.setAttributedTitle(attributedTitle, forState: .Normal)
हो गया। मैंने प्रश्न और त्रुटि के साथ प्रश्न अद्यतन किया है। –
नहीं, त्रुटि: प्रकार के मान 'UIButton' कोई सदस्य 'setAttributedTitleForState' –
मेरे बुरा है। केवल 'setAttributedTitle' होना चाहिए था। संपादित –
Code Different से समाधान पाठ रंग सेटिंग का अनुपालन नहीं करता है। स्टोरीबोर्ड में भी स्पेसिंग पैरामीटर उपलब्ध कराने के लिए UIButton क्लास को ओवरराइड कर सकता है। यहाँ एक अद्यतन स्विफ्ट 3 समाधान आता है:
स्विफ्ट 3
class UIButtonWithSpacing : UIButton
{
override func setTitle(_ title: String?, for state: UIControlState)
{
if let title = title, spacing != 0
{
let color = super.titleColor(for: state) ?? UIColor.black
let attributedTitle = NSAttributedString(
string: title,
attributes: [NSKernAttributeName: spacing,
NSForegroundColorAttributeName: color])
super.setAttributedTitle(attributedTitle, for: state)
}
else
{
super.setTitle(title, for: state)
}
}
fileprivate func updateTitleLabel_()
{
let states:[UIControlState] = [.normal, .highlighted, .selected, .disabled]
for state in states
{
let currentText = super.title(for: state)
self.setTitle(currentText, for: state)
}
}
@IBInspectable var spacing:CGFloat = 0 {
didSet {
updateTitleLabel_()
}
}
}
स्विफ्ट 3,0
extension UIButton{
func addTextSpacing(spacing: CGFloat){
let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!)
attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSRange(location: 0, length: (self.titleLabel?.text!.characters.count)!))
self.setAttributedTitle(attributedString, for: .normal)
}
}
btnRegister.addTextSpacing(spacing: 4.5)
extension UILabel{
func addTextSpacing(spacing: CGFloat){
let attributedString = NSMutableAttributedString(string: self.text!)
attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSRange(location: 0, length: self.text!.characters.count))
self.attributedText = attributedString
}
}
lblOne.addTextSpacing(spacing: 4.5)
extension UITextField{
func addPlaceholderSpacing(spacing: CGFloat){
let attributedString = NSMutableAttributedString(string: self.placeholder!)
attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSRange(location: 0, length: self.placeholder!.characters.count))
self.attributedPlaceholder = attributedString
}
}
txtUserName.addPlaceholderSpacing(spacing: 4.5)
extension UINavigationItem{
func addSpacing(spacing: CGFloat){
let attributedString = NSMutableAttributedString(string: self.title!)
attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSRange(location: 0, length: self.title!.characters.count))
let label = UILabel()
label.textColor = UIColor.black
label.font = UIFont.systemFont(ofSize: 15, weight: UIFontWeightBold)
label.attributedText = attributedString
label.sizeToFit()
self.titleView = label
}
}
navigationItem.addSpacing(spacing: 2.5)
- 1. स्विफ्ट में अक्षर अंतर कैसे बदलें?
- 2. UIButton शीर्षक कैसे बदलें?
- 3. स्विफ्ट - UIButton अधिभावी
- 4. अंतर? - स्विफ्ट
- 5. RStudio संपादक में अक्षर केस कैसे बदलें?
- 6. घुमाएँ UIButton 360 डिग्री - स्विफ्ट
- 7. बदलें/दो अक्षर
- 8. मैं UILabel में अक्षर अंतर कैसे बदलूं?
- 9. स्विफ्ट में UIBarButtonItem के tintColor को कैसे बदलें?
- 10. जावा में एक स्ट्रिंग के पहले अक्षर को बदलें?
- 11. स्विफ्ट: ऐप के अंदर भाषा कैसे बदलें?
- 12. स्विफ्ट में दोनों दिशाओं में फैले बिना UIButton के अंदर एक छवि को कैसे केन्द्रित करें?
- 13. स्विफ्ट में एनएसआईमेज का आकार कैसे बदलें?
- 14. java.awt.font अक्षर अंतर
- 15. जावास्क्रिप्ट स्ट्रिंग में आगे स्लैश "/" अक्षर बदलें?
- 16. स्विफ्ट 2: आईबीओलेट संग्रह [UIButton]! लीक मेमोरी
- 17. समस्या बाएं-संरेखित UIButton शीर्षक (आईओएस/स्विफ्ट)
- 18. आईओएस में बटन टेक्स्ट आकार को कैसे बदलें 8 स्विफ्ट
- 19. UIButton इवेंट ट्रैकिंग Google Analytics (स्विफ्ट)
- 20. स्विफ्ट UIAlertController के शीर्षक को कैसे बदलें रंग
- 21. स्विफ्ट में बटन प्रकार के साथ UIButton की init को ओवरराइड कैसे करें?
- 22. UIButton प्रकार प्रोग्रामेटिक रूप से बदलें
- 23. सीएसएस में किसी तत्व के अंतिम अक्षर के लिए मैं अक्षर-अंतर को कैसे हटा सकता हूं?
- 24. बदलें कार्थेज स्विफ्ट संस्करण
- 25. UIButton
- 26. रंग बदलें - Xamarin/स्विफ्ट
- 27. आईओएस में प्रोग्रामबेटिक रूप से UIButton में टेक्स्ट लेबल संपत्ति को कैसे बदलें?
- 28. आप मूल पाठ में अक्षर-अंतर/ट्रैकिंग कैसे बदलते हैं?
- 29. लिनक्स नाम बदलें अपरकेस पहला अक्षर
- 30. स्प्रेडशीट में प्रत्येक सेल में पहले अक्षर का केस बदलें
कहाँ वास्तव में समस्या है? कृपया वह कोड पोस्ट करें जो आपके लिए काम नहीं कर रहा है। टूटी हुई कोड देखने के लिए –
आपके लिए अपडेट किया गया। –