有時(shí)候設(shè)計(jì)師出的圖,字體行間距會(huì)有特殊要求,因?yàn)槟J(rèn)間距太擠了挑势,此時(shí)就需要代碼設(shè)置一下:
OC:
- (NSAttributedString *)setLineSpaceWith:(CGFloat)space
font:(UIFont *)font
text:(NSString *)text{
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineSpacing = space - (font.lineHeight - font.pointSize);
NSDictionary *dic = @{NSParagraphStyleAttributeName: paragraphStyle};
NSAttributedString *attr = [[NSAttributedString alloc]initWithString:text attributes:dic];
return attr;
}
Swift:
func setTextLineSpace(text: String,
space: CGFloat,
font: UIFont) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = space - (font.lineHeight - font.pointSize)
let text = NSAttributedString.init(string: text, attributes: [NSAttributedStringKey.paragraphStyle: paragraphStyle])
return text
}