How to increase the character spacing in UILabel

后端 未结 6 648
孤独总比滥情好
孤独总比滥情好 2020-12-23 21:23

I am creating an app in >=iOS6. And I want to change the character spacing in UILabel. I have added the custom font \"FUTURABT HEAVY\" in my app but the Character are too cl

相关标签:
6条回答
  • 2020-12-23 21:45
       NSString *strDigit= @"001";
       NSString *strCrushImpact =[NSStringstringWithFormat:@"%d",[strDigit intValue]];
    
           // Set space in between character
       float spacing = 3.0f;
    
       NSMutableAttributedString *attributedStrDigit = [[NSMutableAttributedString alloc] initWithString:strWin];
       [strCrushImpact addAttribute:NSKernAttributeName value:@(spacing)
                 range:NSMakeRange(0, [strDigit length])];
     label.attributedText = attributedStrDigit;
    
    0 讨论(0)
  • 2020-12-23 21:50

    Swift 4

    extension UILabel {
    
       func setCharacterSpacing(characterSpacing: CGFloat = 0.0) {
    
            guard let labelText = text else { return }
    
            let attributedString: NSMutableAttributedString
            if let labelAttributedText = attributedText {
                attributedString = NSMutableAttributedString(attributedString: labelAttributedText)
            } else {
                attributedString = NSMutableAttributedString(string: labelText)
            }
    
            // Character spacing attribute
            attributedString.addAttribute(NSAttributedStringKey.kern, value: characterSpacing, range: NSMakeRange(0, attributedString.length))
    
            attributedText = attributedString
        }        
    
    }
    

    Swift 3

    let label = UILabel()
    let stringValue = "Sample text"
    let attrString = NSMutableAttributedString(string: stringValue)
    attrString.addAttribute(NSKernAttributeName, 2: style, range: NSRange(location: 0, length: stringValue.characters.count))
    label.attributedText = attrString
    
    0 讨论(0)
  • 2020-12-23 21:50

    Here the code for Swift 5 with a handy extension :

    extension UILabel {
        func addCharactersSpacing(spacing: CGFloat, txt: String) {
            let attributedString = NSMutableAttributedString(string: txt)
            attributedString.addAttribute(NSAttributedString.Key.kern, value: spacing, range: NSRange(location: 0, length: txt.count))
            self.attributedText = attributedString
        }
    }
    
    0 讨论(0)
  • 2020-12-23 21:54

    You should probably use NSAttributedString with NSKernAttributeName attribute

    Here is a small example:

    UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
    
    NSString *string = @"Some important text";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    
    float spacing = 5.0f;
    [attributedString addAttribute:NSKernAttributeName
                value:@(spacing)
                range:NSMakeRange(0, [string length])];
    
    label.attributedText = attributedString;
    [self.view addSubview:label];
    
    0 讨论(0)
  • 2020-12-23 21:58

    Swift 4.2 with UILabel extension and @IBInspectable

    extension UILabel {
    @IBInspectable var letterSpacing: CGFloat {
        get {
            var range:NSRange = NSMakeRange(0, self.text?.count ?? 0)
            let nr = self.attributedText?.attribute(NSAttributedString.Key.kern, at: 0, effectiveRange: &range) as! NSNumber
            return CGFloat(truncating: nr)
        }
    
        set {
            let range:NSRange = NSMakeRange(0, self.text?.count ?? 0)
    
            let attributedString = NSMutableAttributedString(string: self.text ?? "")
            attributedString.addAttribute(NSAttributedString.Key.kern, value: newValue, range: range)
            self.attributedText = attributedString
        }
    }
    

    }

    0 讨论(0)
  • 2020-12-23 22:02

    Swift extension for this

    extension UILabel {
        func addCharactersSpacing(spacing:CGFloat, text:String) {
            let attributedString = NSMutableAttributedString(string: text)
            attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSMakeRange(0, text.characters.count))
            self.attributedText = attributedString
        }
    }
    

    So you can use it

    MyLabel.addCharactersSpacing(5, text: "Some Text")
    
    0 讨论(0)
提交回复
热议问题