How to set kerning in iPhone UILabel

后端 未结 9 1094
Happy的楠姐
Happy的楠姐 2020-11-30 01:12

I am developing an iPhone app, and I want to set kerning in UILabel. The code I\'ve written (possibly around kCTKernAttributeName) seems to be in error. How mig

相关标签:
9条回答
  • 2020-11-30 02:00

    Old question, but you can do it now (easily).

    NSMutableAttributedString *attributedString;
    attributedString = [[NSMutableAttributedString alloc] initWithString:@"Please get wider"];
    [attributedString addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(10, 5)];
    [self.label setAttributedText:attributedString];
    

    enter image description here

    For Nov 2013, Just to expand on this great answer, here's some totally typical code. Usually you'd set the font as well. Note in the comments the old-fashioned way using ordinary old .text. Hope it helps someone

    NSString *yourText = @"whatever";
    
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
    
    // simple approach with no tracking...
    // label.text = yourText;
    // [label setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:24]];
    
    NSMutableAttributedString *attributedString;
    
    attributedString = [[NSMutableAttributedString alloc] initWithString:yourText];
    
    [attributedString addAttribute:NSKernAttributeName
                             value:[NSNumber numberWithFloat:2.0]
                             range:NSMakeRange(0, [yourText length])];
    
    [attributedString addAttribute:NSFontAttributeName
                             value:[UIFont fontWithName:@"HelveticaNeue-Light" size:24]
                             range:NSMakeRange(0, [yourText length])];
    
    label.attributedText = attributedString;
    
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    
    [label sizeToFit];
    
    0 讨论(0)
  • 2020-11-30 02:05

    Taking DBD's answer, I made a category on UILabel which allows setting the kerning if running on iOS6+ with graceful fall back to just setting text on previous iOS versions. Might be of help to others...

    UILabel+TextKerning.h

    #import <UIKit/UIKit.h>
    
    @interface UILabel (TextKerning)
    
    /**
     * Set the label's text to the given string, using the given kerning value if able.
     * (i.e., if running on iOS 6.0+). The kerning value specifies the number of points
     * by which to adjust spacing between characters (positive values increase spacing,
     * negative values decrease spacing, a value of 0 is default)
     **/
    - (void) setText:(NSString *)text withKerning:(CGFloat)kerning;
    
    /**
     * Set the kerning value of the currently-set text.  The kerning value specifies the number of points
     * by which to adjust spacing between characters (positive values increase spacing,
     * negative values decrease spacing, a value of 0 is default)
     **/
    - (void) setKerning:(CGFloat)kerning;
    
    @end
    

    UILabel+TextKerning.m

    #import "UILabel+TextKerning.h"
    
    @implementation UILabel (TextKerning)
    
    -(void) setText:(NSString *)text withKerning:(CGFloat)kerning
    {
        if ([self respondsToSelector:@selector(setAttributedText:)])
        {
            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
            [attributedString addAttribute:NSKernAttributeName
                                     value:[NSNumber numberWithFloat:kerning]
                                     range:NSMakeRange(0, [text length])];
            [self setAttributedText:attributedString];
        }
        else
            [self setText:text];
    }
    
    -(void) setKerning:(CGFloat)kerning
    {
        [self setText:self.text withKerning:kerning];
    }
    
    0 讨论(0)
  • 2020-11-30 02:05

    Just do this in Swift:

        let myTitle = "my title"
        let titleLabel = UILabel()
        let attributes: NSDictionary = [
            NSFontAttributeName:UIFont(name: "HelveticaNeue-Light", size: 20),
            NSForegroundColorAttributeName:UIColor.whiteColor(),
            NSKernAttributeName:CGFloat(2.0)
        ]
        let attributedTitle = NSAttributedString(string: myTitle, attributes: attributes as? [String : AnyObject])
    
        titleLabel.attributedText = attributedTitle
        titleLabel.sizeToFit()
    
    0 讨论(0)
提交回复
热议问题