我有一个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)];
答案 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];
这样可以使文本接近零间距,并且可以安全地使用任何字体类型和大小。
答案 1 :(得分:5)
请参阅Apple setLineSpacing - NSMutableParagraphStyle的以下文档,值不得为负数
setLineSpacing:
将段落中行之间添加的点的距离设置为aFloat
。- (void)setLineSpacing:(CGFloat)aFloat
<强>讨论强>
此值必须为非负值。
还有与线条最小和最大高度相关的方法......可能将线条空间设置为0并修改高度可以帮助您达到想要的效果 -
答案 2 :(得分: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()
答案 3 :(得分:2)
只需稍作调整即可使事情更具可配置性。
extension UILabel {
public func setLineHeight(lineHeight: CGFloat, textAlignment: NSTextAlignment ) {
guard let text = self.text else { return }
let attributeString = NSMutableAttributedString(string: text)
let styleLineHeight = self.font.pointSize - self.font.ascender + self.font.capHeight + lineHeight
let style = NSMutableParagraphStyle()
style.alignment = textAlignment
style.maximumLineHeight = styleLineHeight
style.minimumLineHeight = styleLineHeight
let offset = self.font.capHeight - self.font.ascender + lineHeight * 0.5
let range = NSMakeRange(0, text.count)
attributeString.addAttribute(.paragraphStyle, value: style, range: range)
attributeString.addAttribute(.baselineOffset, value: offset, range: range)
self.attributedText = attributeString
}
}
答案 4 :(得分:-1)
我稍微整理了Christ Stillwell的答案:
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
}
}