设置文本的:行间距,字间距,行与字间距
Objective-C代码:NSParagraphStyleAttributeName
CGFloat signUpKnowWidth = 0;
CGFloat signUpKnowHeight = 0;
if (noticeContentL.text.length > 0) {
// 单行文本(宽度)
// CGSize size = [knowString sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*13 weight:UIFontWeightMedium]}];
// 多行文本(高度)
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineBreakMode:NSLineBreakByCharWrapping];
style.lineSpacing = 5;
CGSize size = [noticeContentL.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width/375*310, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*13 weight:UIFontWeightMedium], NSParagraphStyleAttributeName:style} context:nil].size;
signUpKnowWidth = ceil(size.width); // ceil 如果参数是小数,则求最小的整数但不小于本身
signUpKnowHeight = ceil(size.height);
// 设置行间距:
NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:noticeContentL.text];
[attriString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [noticeContentL.text length])];
noticeContentL.attributedText = attriString;
}
image
swift代码:
let lblTitle = UILabel(frame: CGRect(x: 10, y: 150, width: KScreenWidth-20, height: 100))
lblTitle.textColor = UIColor.white
lblTitle.backgroundColor = UIColor.darkGray
lblTitle.textAlignment = .center
lblTitle.numberOfLines = 0
lblTitle.font = UIFont.systemFont(ofSize: 12)
lblTitle.text = "SDCycleScrollView之前一直在OC中使用觉得很简单又熟悉了所以这次写的Demo依旧搬了过来.SDCycleScrollView之前一直在OC中使用觉得很简单又熟悉了所以这次写的Demo依旧搬了过来."
self.view.addSubview(lblTitle)
let attrStr = NSMutableAttributedString(string: lblTitle.text!)
//设置行间距
let style:NSMutableParagraphStyle = NSMutableParagraphStyle()
style.lineSpacing = 10 //行间距(垂直上的间距)
style.lineBreakMode = .byCharWrapping //英文字符拆开显示,byWordWrapping表示不拆开显示
style.alignment = .center //居中显示(如果要设置alignment,这个必须设置,因为label的textAlignment会无效)
style.firstLineHeadIndent = 25.0 //设置首行字符缩进距离
style.headIndent = 5 //每行的左右间距(字间距)
attrStr.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSRange(location: 0, length: (lblTitle.text?.characters.count)!))
//设置字间距
attrStr.addAttribute(NSKernAttributeName, value: 1.5, range: NSRange(location: 0, length: (lblTitle.text?.characters.count)!))
lblTitle.attributedText = attrStr
网友评论