主要应用firstLineHeadIndent 属性,留出前边的的空白距离。视觉上类似图文混排。主要函数类似这个。
demo
ps:现在csdn 处处收费,真的恶心啊。不过,不收费又活不下去。哈哈。
// 1. 由文本生成attributedStringstatic func JC_AttributedText(text:String, color:UIColor, font:UIFont,lineSpacing:CGFloat,firstLineHeadIndent:CGFloat) -> NSAttributedString {let attrString = NSMutableAttributedString(string: text)let prgStyle = NSMutableParagraphStyle()let range = NSRange(location: 0, length: text.count/*text.characters.count*/)prgStyle.lineSpacing = lineSpacing//首行缩进 font.pointSize * 2 (字体大小17号字乘以2,34f即首行空出两个字符)prgStyle.firstLineHeadIndent = firstLineHeadIndent;//prgStyle.tailIndent = 0.0f;//行尾缩进//prgStyle.lineSpacing = 2.0f;//行间距attrString.addAttribute(.paragraphStyle, value: prgStyle, range: range)attrString.addAttribute(.foregroundColor, value: color, range: range)attrString.addAttribute(.font, value: font, range: range)return attrString}