UILabel负线间距

问题描述 投票:5回答:3

我有一个UILabel,我想让行间距小于0(所以文本靠得更近而不是更远)。我究竟做错了什么?

    UIFont* customFont = [UIFont fontWithName:@"BebasNeue" size:70];
    NSString * text = @"Their \nIdeas";

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
    paragrahStyle.lineSpacing = -30;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [text length])];

    UILabel *lbl1 = [[UILabel alloc] init];
    lbl1.frame = CGRectMake(120, 0, viewWidth, 300);
    lbl1.backgroundColor = [UIColor clearColor];
    lbl1.textColor = grayColor;
    lbl1.numberOfLines = 2;
    lbl1.attributedText = attributedString;
    lbl1.userInteractionEnabled = NO;
    lbl1.font = customFont;
    [view addSubview:lbl1];
    [lbl1 setTransform:CGAffineTransformMakeRotation(0.35)];
ios objective-c
3个回答
5
投票

请参阅Apple setLineSpacing - NSMutableParagraphStyle的以下文档,值不得为负数

setLineSpacing:设置段落中行之间添加的点到aFloat的距离。

 - (void)setLineSpacing:(CGFloat)aFloat

讨论

该值必须是非负的。

还有与线条最小和最大高度相关的方法......可能将线条空间设置为0并修改高度可以帮助您达到您想要的效果 -


21
投票
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
CGFloat minMaxLineHeight = (font.pointSize - font.ascender + font.capHeight);
NSNumber *offset = @(font.capHeight - font.ascender);
NSRange range = NSMakeRange(0, labelText.length);
[style setMinimumLineHeight:minMaxLineHeight];
[style setMaximumLineHeight:minMaxLineHeight];
if (isCentered) {
    [style setAlignment:NSTextAlignmentCenter];
}
[attrString addAttribute:NSParagraphStyleAttributeName
                   value:style
                   range:range];
[attrString addAttribute:NSBaselineOffsetAttributeName
                   value:offset
                   range:range];

这应该使您在文本上接近零间距,并且可以安全地使用任何字体类型和大小。


3
投票

Eric Murphey's answer的基础上,我创建了Swift 4语法的扩展。

extension UILabel{    
    public func zeroLineSpace(){
        let s = NSMutableAttributedString(string: self.text!)
        let style = NSMutableParagraphStyle()
        let lineHeight = self.font.pointSize - self.font.ascender + self.font.capHeight
        let offset = self.font.capHeight - self.font.ascender
        let range = NSMakeRange(0, self.text!.count)
        style.maximumLineHeight = lineHeight
        style.minimumLineHeight = lineHeight
        style.alignment = self.textAlignment
        s.addAttribute(.paragraphStyle, value: style, range: range)
        s.addAttribute(.baselineOffset, value: offset, range: range)
        self.attributedText = s
    }
}

简单地称之为

myUiLabel.zeroLineSpace()

-1
投票

我清理了基督斯蒂尔威尔的答案:


extension UILabel {
    public func zeroLineSpace() {
        guard let text = self.text else { return }
        let attributedString = NSMutableAttributedString(string: text)
        let style = NSMutableParagraphStyle()
        let lineHeight = self.font.pointSize - self.font.ascender + self.font.capHeight
        let offset = self.font.capHeight - self.font.ascender
        let range = NSRange(location: 0, length: attributedString.length)
        style.maximumLineHeight = lineHeight
        style.minimumLineHeight = lineHeight
        style.alignment = self.textAlignment
        attributedString.addAttribute(.paragraphStyle, value: style, range: range)
        attributedString.addAttribute(.baselineOffset, value: offset, range: range)
        self.attributedText = attributedString
    }
}