কীভাবে দ্রুতগতিতে কোনও ইউআইএলবেলকে আন্ডারলাইন করবেন?


99

কীভাবে সুইফটে আন্ডারলাইন করবেন UILabel? আমি অবজেক্টিভ-সিগুলি অনুসন্ধান করেছি কিন্তু এগুলি সুইফটে কাজ করতে বেশিরভাগই পেতে পারি নি।


7
NSAttributedString?
— লারমে

অপছন্দের সাথে কি? এটিবিটিগুলিতে পদ্ধতি কলগুলির মতো দেখতে আবেগের সাথে এখানে একটি স্পষ্ট বিভ্রান্তি রয়েছে
— Esqarrouth

এখানে আপনি সহজ উপায় স্ট্যাকওভারফ্লো.com
— কপিল বি

এখানে সহজ উপায় [ স্ট্যাকওভারফ্লো.com
— কপিল বি

উত্তর:


227

আপনি এনএসএট্রিবিউটেড স্ট্রিং ব্যবহার করে এটি করতে পারেন

উদাহরণ:

let underlineAttribute = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue]
let underlineAttributedString = NSAttributedString(string: "StringWithUnderLine", attributes: underlineAttribute)
myLabel.attributedText = underlineAttributedString

সম্পাদনা

একটি ইউআইএলবেলের সমস্ত পাঠ্যের জন্য একই বৈশিষ্ট্যগুলি পেতে, আমি আপনাকে ইউআইএলবেল এবং ওভাররাইডিং পাঠ্যটিকে সাবক্লাস করার পরামর্শ দিই:

সুইফট 4.2

class UnderlinedLabel: UILabel {

override var text: String? {
    didSet {
        guard let text = text else { return }
        let textRange = NSMakeRange(0, text.count)
        let attributedText = NSMutableAttributedString(string: text)
        attributedText.addAttribute(NSAttributedString.Key.underlineStyle , value: NSUnderlineStyle.single.rawValue, range: textRange)
        // Add other attributes if needed
        self.attributedText = attributedText
        }
    }
}

সুইফট 3.0

class UnderlinedLabel: UILabel {
    
    override var text: String? {
        didSet {
            guard let text = text else { return }
            let textRange = NSMakeRange(0, text.characters.count)
            let attributedText = NSMutableAttributedString(string: text)
            attributedText.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
            // Add other attributes if needed
            self.attributedText = attributedText
        }
    }
}

এবং আপনি আপনার পাঠ্যটি এভাবে রেখেছেন:

@IBOutlet weak var label: UnderlinedLabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        label.text = "StringWithUnderLine"
    }

পুরানো:

সুইফ্ট (2.0 থেকে 2.3):

class UnderlinedLabel: UILabel {
    
    override var text: String? {
        didSet {
            guard let text = text else { return }
            let textRange = NSMakeRange(0, text.characters.count)
            let attributedText = NSMutableAttributedString(string: text)
            attributedText.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
            // Add other attributes if needed
            
            self.attributedText = attributedText
        }
    }
}

সুইফট 1.2:

class UnderlinedLabel: UILabel {
    
    override var text: String! {
        didSet {
            let textRange = NSMakeRange(0, count(text))
            let attributedText = NSMutableAttributedString(string: text)
            attributedText.addAttribute(NSUnderlineStyleAttributeName, value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
            // Add other attributes if needed
            
            self.attributedText = attributedText
        }
    }
}

অ-রেখাঙ্কিত করার সর্বোত্তম উপায় কী হবে?
— এন ডের

আমি দীর্ঘদিন ধরেই ভাবছিলাম: অন্যথায় এটি ক্র্যাশ হয় কেন আমাদের কাঁচা মূল্য ব্যবহার করতে হবে?
— ব্রুনো মুনিজ

UTF16আপনার পাঠ্যরঞ্জ তৈরি করার সময় আপনার অক্ষর গণনার পরিবর্তে গণনাটি পাস করা উচিতNSRange
— লিও ডাবাস

আমি আরম্ভের উপর আন্ডারলাইন যুক্ত করার জন্য এই সমাধানটি পরিবর্তন করেছি, এভাবে স্টোরিবোর্ডগুলির সাহায্যে আমরা সহজেই এটি ব্যবহার করতে পারিrequired init?(coder: NSCoder) { super.init(coder: coder) self.addUnderline() // your code }
— সেরা

114

সুইফট 5 এবং 4.2 একটি লাইনার:

label.attributedText = NSAttributedString(string: "Text", attributes:
    [.underlineStyle: NSUnderlineStyle.single.rawValue])

সুইফট 4 ওয়ান লাইনার:

label.attributedText = NSAttributedString(string: "Text", attributes:
    [.underlineStyle: NSUnderlineStyle.styleSingle.rawValue])

সুইফট 3 ওয়ান লাইনার:

label.attributedText = NSAttributedString(string: "Text", attributes:
      [NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue])

4
NSUnderlineStyle.styleSingle.rawValue এ নতুন নাম পরিবর্তন করে NSUnderlineStyle.single.rawValue 4.2
— স্কাল

আমি কীভাবে ডি-আন্ডারলাইন করব?
— এন ডের

@ এন ডের আবার লেবেলে একটি সাধারণ পাঠ্য সেট করুন
— জীবন

15

যদি আপনি উত্তরাধিকার ছাড়াই এটি করার উপায় খুঁজছেন:

সুইফট 5

extension UILabel {
    func underline() {
        if let textString = self.text {
          let attributedString = NSMutableAttributedString(string: textString)
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle,
                                          value: NSUnderlineStyle.single.rawValue,
                                          range: NSRange(location: 0, length: attributedString.length))
          attributedText = attributedString
        }
    }
}

সুইফট 3/4

// in swift 4 - switch NSUnderlineStyleAttributeName with NSAttributedStringKey.underlineStyle
extension UILabel {
    func underline() {
        if let textString = self.text {
          let attributedString = NSMutableAttributedString(string: textString)
          attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: attributedString.length))
          attributedText = attributedString
        }
    }
}


extension UIButton {
  func underline() {
    let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!)
    attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSRange(location: 0, length: (self.titleLabel?.text!.characters.count)!))
    self.setAttributedTitle(attributedString, for: .normal)
  }
}

UTF16আপনার তৈরি করার সময় আপনার অক্ষর গণনার পরিবর্তে গণনা পাস করা উচিতNSRange
— লিও ডাবাস

11

সুইফট 5:

1- গুণিত টেক্সট পেতে একটি স্ট্রিং এক্সটেনশন তৈরি করুন

extension String {

    var underLined: NSAttributedString {
        NSMutableAttributedString(string: self, attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue])
    }

}

2- এটি ব্যবহার করুন

বোতামগুলিতে:

button.setAttributedTitle(yourButtonTitle.underLined, for: .normal)

লেবেলে:

label.attributedText = yourLabelTitle.underLined

বা স্টয়বোর্ড সংস্করণ


8

সুইফট 4 এবং এক্সকোড 9- এ শ্লোম উত্তরের জন্য সামান্য ফিক্স ।

extension UILabel {
    func underline() {
        if let textString = self.text {
            let attributedString = NSMutableAttributedString(string: textString)
            attributedString.addAttribute(NSAttributedStringKey.underlineStyle,
                                          value: NSUnderlineStyle.styleSingle.rawValue,
                                          range: NSRange(location: 0, length: attributedString.length - 1))
            attributedText = attributedString
        }
    }
}

    extension UIButton {
        func underline() {
            let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!)
            attributedString.addAttribute(NSAttributedStringKey.underlineStyle,
                                          value: NSUnderlineStyle.styleSingle.rawValue,
                                          range: NSRange(location: 0, length: (self.titleLabel?.text!.count)!))
            self.setAttributedTitle(attributedString, for: .normal)
        }
    }

UTF16আপনার তৈরি করার সময় আপনার অক্ষর গণনার পরিবর্তে গণনা পাস করা উচিতNSRange
— লিও ডাবাস

7

আপনি UILabelইন্টারফেস বিল্ডার ব্যবহার করে পাঠ্যটিকে আন্ডারলাইন করতে পারেন ।

আমার উত্তরের লিঙ্কটি এখানে: স্টোরিবোর্ডে আংশিক পাঠ্য UILabel এর আন্ডারলাইন বৈশিষ্ট্য যুক্ত করা


4
আপনি যদি লেবেলে পাঠ্যটি ফেরত পাঠান তবে এই পদ্ধতিটি ব্যর্থ।
— এরিক এইচ

@ এরিক আপনার অর্থ কি?
— অর্থ-বিষয়গুলি

4

সুইফটে একই উত্তর 4.2

জন্য UILable

extension UILabel {
    func underline() {
        if let textString = self.text {
            let attributedString = NSMutableAttributedString(string: textString)
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle,
                                          value: NSUnderlineStyle.single.rawValue,
                                          range: NSRange(location: 0, length: textString.count))
            self.attributedText = attributedString
        }
    }
}

নীচের মতো ইউআইএলবেলের জন্য কল করুন

myLable.underline()

জন্য UIButton

extension UIButton {
    func underline() {
        if let textString = self.titleLabel?.text {

            let attributedString = NSMutableAttributedString(string: textString)
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle,
                                          value: NSUnderlineStyle.single.rawValue,
                                          range: NSRange(location: 0, length: textString.count))
            self.setAttributedTitle(attributedString, for: .normal)
        }

    }
}

নীচের মত ইউআইবাটন জন্য কল করুন

myButton.underline()

আমি উপরের উত্তরগুলিতে সন্ধান করেছি এবং সেগুলির মধ্যে কয়েকটি জোর করে পাঠানো মানকে আবদ্ধ করা হচ্ছে । আমি নিরাপদে আন-র্যাপিং করে মান পাওয়ার পরামর্শ দেব। এটি শূন্যমূল্যের ক্ষেত্রে ক্রাশ এড়ানো হবে। আশাকরি এটা সাহায্য করবে :)


কেবল সুন্দর এবং সহজ
— জানু বার্গস্ট্রোম

UTF16আপনার তৈরি করার সময় আপনার অক্ষর গণনার পরিবর্তে গণনা পাস করা উচিতNSRange
— লিও ডাবাস

আপনার যদি ইতিমধ্যে ইউআইএলবেলটির জন্য এক্সটেনশন থাকে তবে আইএমও এটি myButton.titleLabel? .Undline () কল করা সহজ, বা কমপক্ষে ইউআইবাটনের এক্সটেনশনে এটি আন্ডারলাইন () ফাংশনের অভ্যন্তরে ব্যবহার করুন।
— বোহেরনা

4

সুইফট 4, 4.2 এবং 5।

  @IBOutlet weak var lblUnderLine: UILabel!

আমাকে ইউআইএলবেলে নির্দিষ্ট পাঠ্যরেখাটি আন্ডারলাইন করা দরকার। সুতরাং, পরিসীমা এবং সেট বৈশিষ্ট্যগুলি সন্ধান করুন।

    let strSignup = "Don't have account? SIGNUP NOW."
    let rangeSignUp = NSString(string: strSignup).range(of: "SIGNUP NOW.", options: String.CompareOptions.caseInsensitive)
    let rangeFull = NSString(string: strSignup).range(of: strSignup, options: String.CompareOptions.caseInsensitive)
    let attrStr = NSMutableAttributedString.init(string:strSignup)
    attrStr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.white,
                           NSAttributedString.Key.font : UIFont.init(name: "Helvetica", size: 17)! as Any],range: rangeFull)
    attrStr.addAttributes([NSAttributedString.Key.foregroundColor : UIColor.white,
                           NSAttributedString.Key.font : UIFont.init(name: "Helvetica", size: 20)!,
                          NSAttributedString.Key.underlineStyle: NSUnderlineStyle.thick.rawValue as Any],range: rangeSignUp) // for swift 4 -> Change thick to styleThick
    lblUnderLine.attributedText = attrStr

আউটপুট

এখানে চিত্র বর্ণনা লিখুন


3

একটি বাক্যে একাধিক স্ট্রিংয়ে আন্ডারলাইন করুন।

extension UILabel {
    func underlineMyText(range1:String, range2:String) {
        if let textString = self.text {

            let str = NSString(string: textString)
            let firstRange = str.range(of: range1)
            let secRange = str.range(of: range2)
            let attributedString = NSMutableAttributedString(string: textString)
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: firstRange)
            attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: secRange)
            attributedText = attributedString
        }
    }
}

এইভাবে ব্যবহার করুন।

    lbl.text = "By continuing you agree to our Terms of Service and Privacy Policy."
    lbl.underlineMyText(range1: "Terms of Service", range2: "Privacy Policy.")

4
কিভাবে আপনি স্পর্শ ট্র্যাক করবে?
— কার্তিকেইয়ান

2

সুইফট 4 পরিবর্তন। NSUnderlineStyle.styleSingle এর পরিবর্তে NSUnderlineStyle.styleSingle.rawValue ব্যবহার করার জন্য স্মরণকারী ।

   'let attributedString = NSAttributedString(string: "Testing")
    let textRange = NSMakeRange(0, attributedString.length)
    let underlinedMessage = NSMutableAttributedString(attributedString: attributedString)
    underlinedMessage.addAttribute(NSAttributedStringKey.underlineStyle,
                                   value:NSUnderlineStyle.styleSingle.rawValue,
                                   range: textRange)
    label.attributedText = underlinedMessage

`


2

আপনি যদি আন্ডারলাইন হিসাবে লেবেলের কেবলমাত্র অর্ধেক অংশ অর্জন করতে চান তবে আপনি এটি ব্যবহার করতে পারেন: - // সুইফট 4.0+ এর জন্য

let attributesForUnderLine: [NSAttributedString.Key: Any] = [
            .font: UIFont(name: AppFont.sourceSansPro_Regular, size: 12) ?? UIFont.systemFont(ofSize: 11),
            .foregroundColor: UIColor.blue,
            .underlineStyle: NSUnderlineStyle.single.rawValue]

        let attributesForNormalText: [NSAttributedString.Key: Any] = [
            .font: UIFont(name: AppFont.sourceSansPro_Regular, size: 12) ?? UIFont.systemFont(ofSize: 11),
            .foregroundColor: AppColors.ColorText_787878]

        let textToSet = "Want to change your preferences? Edit Now"
        let rangeOfUnderLine = (textToSet as NSString).range(of: "Edit Now")
        let rangeOfNormalText = (textToSet as NSString).range(of: "Want to change your preferences?")

        let attributedText = NSMutableAttributedString(string: textToSet)
        attributedText.addAttributes(attributesForUnderLine, range: rangeOfUnderLine)
        attributedText.addAttributes(attributesForNormalText, range: rangeOfNormalText)
        yourLabel.attributedText = attributedText

1

উপরের উত্তরটি আমার বিল্ড পরিবেশে ত্রুটি ঘটায়।

এটি সুইফট ৪.০ এ কাজ করে না:

attributedText.addAttribute(NSUnderlineStyleAttributeName, 
                            value: NSUnderlineStyle.styleSingle.rawValue, 
                            range: textRange)

পরিবর্তে এটি চেষ্টা করুন:

attributedText.addAttribute(NSAttributedStringKey.underlineStyle,
                            value: NSUnderlineStyle.styleSingle.rawValue,
                            range: textRange)

আশা করি এটি কাউকে সাহায্য করবে



0

সুইফট ২.৩ এর জন্য

extension UIButton {
    func underline() {
        let attributedString = NSMutableAttributedString(string: (self.titleLabel?.text!)!)
        attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: NSRange(location: 0, length: (self.titleLabel?.text!.characters.count)!))
        self.setAttributedTitle(attributedString, forState: .Normal)
    }
}

এবং ভিউকন্ট্রোলারে

@IBOutlet var yourButton: UIButton!

মধ্যে ViewDidLoadপদ্ধতি অথবা আপনার ফাংশন শুধু লেখ

yourButton.underline()

এটি আপনার বোতামের শিরোনামটিকে আন্ডারলাইন করবে


0

সুইফট 5 এর জন্য ইউআইবিউটনের জন্য আন্ডারলাইন সেট করা এবং মুছে ফেলার জন্য একটি শ্রেণি I আমি আশা করি এটি সাহায্য করবে

import Foundation
   import UIKit

   class UiUtil {

       static let underlineThickness = 2
    
       class func removeUnderlineFromButton( _ button:UIButton ) {
          if let str = button.titleLabel?.attributedText {
            let attributedString = NSMutableAttributedString( attributedString: str )
            attributedString.removeAttribute(.underlineStyle, range: 
   NSRange.init(location: 0, length: attributedString.length))
            button.setAttributedTitle(attributedString, for: .normal)
         }
      }

    class func setUnderlineFromButton( _ button:UIButton ) {
        if let str = button.titleLabel?.attributedText {
            let attributedStringUnderline = NSMutableAttributedString( attributedString: 
    str  )
              attributedStringUnderline.addAttribute(
                NSAttributedString.Key.underlineStyle,
                value: underlineThickness,
                range: NSRange.init(location: 0, length: attributedStringUnderline.length)
              )
              button.setAttributedTitle(attributedStringUnderline, for: .normal)
           }
      }

   }
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.