label textView 段落间距

2017-06-22  本文已影响427人  seventhboy

NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByWordWrapping;
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = 11; //设置行间距
paraStyle.hyphenationFactor = 1.0;
paraStyle.firstLineHeadIndent = 0.0;
paraStyle.paragraphSpacingBefore = 0.0;
paraStyle.headIndent = 0;
paraStyle.tailIndent = 0;
NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName: 11
};
NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
label.attributedText = attributeStr;

一、设置textView的行间距
1.如果只是静态显示textView的内容为设置的行间距,执行如下代码:

// textview 改变字体的行间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;// 字体的行间距

NSDictionary *attributes = @{ 
                             NSFontAttributeName:[UIFont systemFontOfSize:15], 
                             NSParagraphStyleAttributeName:paragraphStyle 
                             }; 
textView.attributedText = [[NSAttributedString alloc] initWithString:@"输入你的内容" attributes:attributes];

2.如果是想在输入内容的时候就按照设置的行间距进行动态改变,那就需要将上面代码放到textView的delegate方法里

-(void)textViewDidChange:(UITextView *)textView

{

//    textview 改变字体的行间距

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

paragraphStyle.lineSpacing = 20;// 字体的行间距



NSDictionary *attributes = @{

                             NSFontAttributeName:[UIFont systemFontOfSize:15],

                             NSParagraphStyleAttributeName:paragraphStyle

                             };

textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];

}

上一篇下一篇

猜你喜欢

热点阅读