Border around every word of UILabel

后端 未结 4 583
天命终不由人
天命终不由人 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:40

    SWIFT 4 Version

    This is the swift 4 version of vikingosegundo Answer using a UITextView including a few other tweaks including:
    • list of words coming from an array
    • Centering in the textView
    • Adjustable word limit
    • Custom font

    //: Playground - noun: a place where people can play
    
    import UIKit
    
    var array = [String]()
    
    array = ["WORD1", "WORD2", "WORD3", "WORD4", "WORD4", "LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONGWORD", "WORD5"]
    
    var stringarr = String()
    
    for i in 0...(array.count-1) {
    
    if array[i].count > 20 {
        let count = array[i].count
        let dropCount = count-20
        let dropCharacters = array[i].dropLast(dropCount)
        var shortenedWord = "\(dropCharacters)"
        shortenedWord = "\(shortenedWord)..."
        print(shortenedWord)
        array[i] = shortenedWord
    
        }
    }
    
    stringarr = array.joined(separator: " ")
    
    
    func frameOfTextInRange(range:NSRange, inTextView textView:UITextView) -> CGRect {
        let beginning = textView.beginningOfDocument
        let start = textView.position(from: beginning, offset: range.location)
        let end = textView.position(from: start!, offset: range.length)
        let textRange = textView.textRange(from: start!, to: end!)
        let rect = textView.firstRect(for: textRange!)
    
        return textView.convert(rect, from: textView)
    }
    
    let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 375, height: 200))
    textView.backgroundColor = UIColor.clear
    
    let lineSpacing: CGFloat = 0.0
    
    textView.attributedText = {
        let attributedString = NSMutableAttributedString(string: stringarr)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineHeightMultiple = 1.5
        paragraphStyle.lineSpacing = lineSpacing
        attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: stringarr.count))
        attributedString.addAttribute(.foregroundColor, value: UIColor.white, range: NSRange(location: 0, length: stringarr.count))
        attributedString.addAttribute(.font, value: UIFont(name: "AvenirNext-Regular", size: 10.0)!, range: NSRange(location: 0, length: stringarr.count))
    
    
    let regex = try! NSRegularExpression(pattern: "\\s", options: [])
    let matches = regex.matches(in: stringarr, options: [], range: NSRange(location: 0, length: stringarr.count))
    
    for m in matches {
        attributedString.addAttribute(.kern, value: 6, range: m.range)
    }
    
    return NSAttributedString(attributedString: attributedString)
    }()
    
    
    textView.textAlignment = .center
    
    let textViewBG = UIView(frame: textView.bounds)
    textViewBG.backgroundColor = UIColor.white
    
    let pattern = "[^ ]+"
    let regex = try! NSRegularExpression(pattern: pattern, options: [])
     let matches = regex.matches(in: stringarr, options: [], range: NSRange(location: 0, length: stringarr.count))
    
    for m in matches {
    textViewBG.addSubview({
        let range = m.range
        let frame = frameOfTextInRange(range: range, inTextView: textView).insetBy(dx: CGFloat(-2), dy: CGFloat(3)).offsetBy(dx: CGFloat(0), dy: CGFloat(3))
        let v = UIView(frame: frame)
        v.layer.cornerRadius = 0
        v.backgroundColor = UIColor.black
        return v
        }())
    }
    
    textViewBG.addSubview(textView)
    
    //: End of playground
    



    Result

    Hope that helps!

提交回复
热议问题