核心文本 – NSAttributedString行高正确?

Core Text的行距完全在黑暗中。 我正在使用NSAttributedString,我指定了以下属性: – kCTFontAttributeName – kCTParagraphStyleAttributeName

从这个CTFrameSetter被创build并绘制到上下文。

在段落样式属性中,我想指定线的高度。

当我使用kCTParagraphStyleSpecifierLineHeightMultiple每行接收填充文本的顶部,而不是在这个高度的中间显示文本。

当我使用kCTParagraphStyleSpecifierLineSpacing一个填充添加到文本的底部。

请帮助我使用文本(字形)在该高度的中间达到指定的行高度,而不是坐在底部或顶部的文本。

如果不沿着明确创buildCTLine等的路线,这是不可能的?

你可以使用这个,如果你正在开发的iOS> = 6.0

NSInteger strLength = [myString length]; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setLineSpacing:24]; [attString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, strLength)]; 

我对以下的陈述仍然没有100%的信心,但似乎是有道理的。 请纠正我错在哪里。

线高(前导)是指连续线的基线之间的距离。 这里的基线可以被解释为文本所在的想象线。

间距是线条之间的空间。 该空格出现在文本行之后。

我结束了对我的问题使用以下解决scheme:

 // NOT SURE WHAT THE THEORY BEHIND THIS FACTOR IS. WAS FOUND VIA TRIAL AND ERROR. CGFloat factor = 14.5/30.5; CGFloat floatValues[4]; floatValues[0] = self.lineHeight * factor/(factor + 1); floatValues[1] = self.lineHeight/(factor + 1); floatValues[2] = self.lineHeight; 

此matrix与NSAttributedString的段落样式参数一起使用:

 CTParagraphStyleSetting paragraphStyle[3]; paragraphStyle[0].spec = kCTParagraphStyleSpecifierLineSpacing; paragraphStyle[0].valueSize = sizeof(CGFloat); paragraphStyle[0].value = &floatValues[0]; paragraphStyle[1].spec = kCTParagraphStyleSpecifierMinimumLineHeight; paragraphStyle[1].valueSize = sizeof(CGFloat); paragraphStyle[1].value = &floatValues[1]; paragraphStyle[2].spec = kCTParagraphStyleSpecifierMaximumLineHeight; paragraphStyle[2].valueSize = sizeof(CGFloat); paragraphStyle[2].value = &floatValues[2]; CTParagraphStyleRef style = CTParagraphStyleCreate((const CTParagraphStyleSetting*) &paragraphStyle, 3); [attributedString addAttribute:(NSString*)kCTParagraphStyleAttributeName value:(id)style range:NSMakeRange(0, [string length])]; CFRelease(style); 

希望这有助于某人。 我会更新这个答案,因为我发现更多的相关信息。

Swift 3

  let textFont = UIFont(name: "Helvetica Bold", size: 20)! let textColor = UIColor(white: 1, alpha: 1) // White let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.paragraphSpacing = 20 // Paragraph Spacing paragraphStyle.lineSpacing = 40 // Line Spacing let textFontAttributes = [ NSFontAttributeName: textFont, NSForegroundColorAttributeName: textColor, NSParagraphStyleAttributeName: paragraphStyle ] as [String : Any] 

您可以设置/更新故事板中的行间距和行高度以及编程方式。

从界面生成器:

在这里输入图像说明

编程方式:

SWIFT 4

 extension UILabel { // Pass value for any one of both parameters and see result func setLineSpacing(lineSpacing: CGFloat = 0.0, lineHeightMultiple: CGFloat = 0.0) { guard let labelText = self.text else { return } let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = lineSpacing paragraphStyle.lineHeightMultiple = lineHeightMultiple let attributedString:NSMutableAttributedString if let labelattributedText = self.attributedText { attributedString = NSMutableAttributedString(attributedString: labelattributedText) } else { attributedString = NSMutableAttributedString(string: labelText) } // Line spacing attribute attributedString.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length)) self.attributedText = attributedString } } 

现在调用扩展function

 let label = UILabel() let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel" // Pass value for any one argument - lineSpacing or lineHeightMultiple label.setLineSpacing(lineSpacing: 2.0) . // try values 1.0 to 5.0 // or try lineHeightMultiple //label.setLineSpacing(lineHeightMultiple = 2.0) // try values 0.5 to 2.0 

或者使用标签实例(只需复制并执行此代码即可查看结果)

 let label = UILabel() let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel" let attrString = NSMutableAttributedString(string: stringValue) var style = NSMutableParagraphStyle() style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48 style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40 // Line spacing attribute attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) // Character spacing attribute attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length)) label.attributedText = attrString 

Swift 3

 let label = UILabel() let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel" let attrString = NSMutableAttributedString(string: stringValue) var style = NSMutableParagraphStyle() style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48 style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40 attrString.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: stringValue.characters.count)) label.attributedText = attrString 

这在Xcode 7.2中适用于我。 iOS 9.2.1。 (Swift 2.1):

  dispatch_async(dispatch_get_main_queue()) { () -> Void in let paragraphStyleWithSpacing = NSMutableParagraphStyle() paragraphStyleWithSpacing.lineSpacing = 2.0 //CGFloat let textWithLineSpacing = NSAttributedString(string: str, attributes: [NSParagraphStyleAttributeName : paragraphStyleWithSpacing]) self.MY_TEXT_VIEW_NAME.attributedText = textWithLineSpacing }