UILabel + UITextView 字间距和行间距

来源:互联网 发布:西安软件测试招聘 编辑:程序博客网 时间:2024/05/19 19:40

UILabel:创建category

.h

#import <UIKit/UIKit.h>@interface UILabel (JKLableChangeLineSpace)/** *  改变行间距 */+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space;/** *  改变字间距 */+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space;/** *  改变行间距和字间距 */+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace;@end

.m

#import "UILabel+JKLableChangeLineSpace.h"@implementation UILabel (JKLableChangeLineSpace)+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {        NSString *labelText = label.text;    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];    [paragraphStyle setLineSpacing:space];    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];    label.attributedText = attributedString;    [label sizeToFit];    }+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {        NSString *labelText = label.text;    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];    label.attributedText = attributedString;    [label sizeToFit];    }+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {        NSString *labelText = label.text;    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];    [paragraphStyle setLineSpacing:lineSpace];    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];    label.attributedText = attributedString;    [label sizeToFit];    }@end

用法:

[UILabel changeLineSpaceForLabel:self.lable WithSpace:8];

UITextView:创建category

.h

/** *  改变行间距 */+ (void)changeLineSpaceForTextView:(UITextView *)textView WithSpace:(float)space;
.m

+ (void)changeLineSpaceForTextView:(UITextView *)textView WithSpace:(float)space{        //    1.静态显示textView的内容为设置的行间距,执行如下代码:                //    textview 改变字体的行间距            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];            paragraphStyle.lineSpacing = 5;// 字体的行间距    NSDictionary *attributes = @{                                 NSFontAttributeName:[UIFont systemFontOfSize:15],                                 NSParagraphStyleAttributeName:paragraphStyle                                         };            textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];}


0 0
原创粉丝点击