Border around every word of UILabel

后端 未结 4 704
天命终不由人
天命终不由人 2021-01-07 09:06

Is there a way i can draw border around every word of UILabel. Suppose UILabel contains the string \" This is the Line 1\".

I want 5 differ

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-07 09:52

    You will have to create one label for each word....do it programmatically! I did it now, test please! hope u enjoy :-)

    import UIKit

    class ViewController: UIViewController {
    
    var arrayStrings = [String]()
    var x : CGFloat = 0
    var labelReference = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    
        let space = " "
        let string = "This is the line 1"
        var word = string.componentsSeparatedByString(space)
        print (word[0]) // prints "This"
        print(word[1]) // print "is"
    
        for var i = 0; i < word.count ; i++ {
    
    
            arrayStrings.append(word[i])
    
    
            let characteresCount = word[i].characters.count
    
    
            // change de "9" based on your font size
            let label = UILabel(frame: CGRectMake(CGFloat(32 + x), 30, CGFloat(characteresCount * 9), 25))
            x += label.frame.size.width + 2
            label.text = word[i]
            label.layer
            label.layer.borderWidth = 1.0
            label.layer.cornerRadius = 10
            view.addSubview(label)
    
    
          }
    
       }
    
    }
    

提交回复
热议问题