iOS--UILabel设置行距和字间距,并根据文本计算高度

2019-05-24  本文已影响0人  早起的虫儿子被鸟吃

iOS开发中经常会用到UILabel来展示一些文字性内容,默认的排版方式会有些挤,这里来介绍一下如何给UILabel设置行间距和字间距,欢迎大家多提意见,共勉❤️❤️❤️

实现方法

设置行间距,字间距等都是对字符串的处理,这里需要用到富文本NSAttributedStringNSMutableAttributedString,设置其属性即可。

创建label备用:

NSString*textContent =@"人生若只如初见,何事秋风悲画扇。等闲变却故人心,却道故人心易变。骊山语罢清宵半,泪雨零铃终不怨。何如薄幸锦衣郎,比翼连枝当日愿。";//labelUILabel*testLabel = [[UILabelalloc] initWithFrame:CGRectMake(20,100,self.view.frame.size.width-20*2,300)];//多行显示testLabel.numberOfLines =0;    testLabel.backgroundColor = [UIColorlightGrayColor];    [self.view addSubview:testLabel];//富文本属性NSMutableDictionary*textDict = [NSMutableDictionarydictionary];//基本属性设置//字体颜色textDict[NSForegroundColorAttributeName] = [UIColorblackColor];//字号大小textDict[NSFontAttributeName] = [UIFontsystemFontOfSize:16.0];

行间距:

设置行间距需要用到NSMutableParagraphStyleNSMutableParagraphStyle用于设定文本段落有关的设置,比如行间距,文本缩进,段间距等,用法如下:

//段落样式NSMutableParagraphStyle*paraStyle = [[NSMutableParagraphStylealloc] init];//行间距paraStyle.lineSpacing =10.0;//首行文本缩进paraStyle.firstLineHeadIndent =20.0;//使用//文本段落样式textDict[NSParagraphStyleAttributeName] = paraStyle;

字间距:

直接修改NSAttributedString,或者NSMutableAttributedStringNSKernAttributeName属性即可,用法如下:

//字间距(字符串)textDict[NSKernAttributeName] = @(1);

赋值:

//赋值testLabel.attributedText = [[NSAttributedStringalloc] initWithString:textContent attributes:textDict];

效果如下:

行间距,字间距

计算文本高度(有行间距的情况下)

记住你对label的设置,计算高度时传入字符串,宽度和富文本的属性(字典类型)即可。

-(CGFloat)getSpaceLabelHeight:(NSString*)str withAttrDict:(NSMutableDictionary*)dict withWidth:(CGFloat)width {CGSizesize = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOriginattributes:dict context:nil].size;returnsize.height;}

NSMutableParagraphStyle补充

NSMutableParagraphStyle功能很强大,还有很多属性可以用,比如:

//段落行距//paragraphStyle.lineSpacing = 10; //非首行文本缩进                //paragraphStyle.headIndent = 5;  // 文本缩进(右端)                //paragraphStyle.tailIndent = -20;  // 首行文本缩进          //paragraphStyle.firstLineHeadIndent = 20;  //文本对齐方式        //paragraphStyle.alignment = NSTextAlignmentRight;  //折行方式//paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; //文本写入方式//paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight; //文本行间距 是默认行间距的多少倍//paragraphStyle.lineHeightMultiple = 3.0; //文本最大行距        //paragraphStyle.maximumLineHeight = 50;  //文本最小行距      //paragraphStyle.minimumLineHeight = 50; /*

allowsDefaultTighteningForTruncation(收缩字符间距允许截断)

*///paragraphStyle.allowsDefaultTighteningForTruncation = YES; // 设置每行的最后单词是否截断,在0.0-1.0之间,默认为0.0,越接近1.0单词被截断的可能性越大,//paragraphStyle.hyphenationFactor = 1.0; //段落后面的间距//paragraphStyle.paragraphSpacing = 10; //设置段与段之间的距离          //paragraphStyle.paragraphSpacingBefore = 20;     

富文本属性补充

/*

    删除线和下划线

    枚举常量 NSUnderlineStyle中的值

    NSUnderlineStyleNone        //不设置删除线

    NSUnderlineStyleSingle      // 设置删除线为细单实线

    NSUnderlineStyleThick      //  设置删除线为粗单实线

    NSUnderlineStyleDouble    // 设置删除线为细双实线

    NSUnderlinePatternSolid

    NSUnderlinePatternDot    //点

    NSUnderlinePatternDash  //虚线

    NSUnderlinePatternDashDot  //虚线和点

    NSUnderlinePatternDashDotDot  //虚线和点点

    NSUnderlineByWord

    */// NSNumber,加删除线,默认不加删除线,其它的话是加不同风格的删除线dict[NSStrikethroughStyleAttributeName] = @(NSUnderlinePatternSolid|NSUnderlineStyleSingle);//UIColor,删除线颜色,默认等于文本前景颜色,前提是需要加删除线,和NSStrikethroughStyleAttributeName有关dict[NSStrikethroughColorAttributeName] = [UIColoryellowColor];// NSNumber,加下划线,默认NSUnderlineStyleNone不加下划线,其它的话是加不同的下划线dict[NSUnderlineStyleAttributeName] = @(NSUnderlineStyleDouble);// UIColor,下划线颜色,默认等于文本前景颜色,前提是需要加下划线,和NSUnderlineStyleAttributeName有关dict[NSUnderlineColorAttributeName] = [UIColoryellowColor];// UIColor,默认等于文本前景颜色,需要和NSStrokeWidthAttributeName一起使用dict[NSStrokeColorAttributeName] = [UIColoryellowColor];// NSNumber,使文本有一种中空的效果(有立体效果)数字越大,文本填充的越满,数字越小,文本颜色越淡,不需要和NSStrokeColorAttributeName一起使用dict[NSStrokeWidthAttributeName] = @5;/*

    文本阴影

    NSShadow *shadow = [[NSShadow alloc] init];

    shadow.shadowOffset = CGSizeMake(2, 3);      阴影偏移量

    shadow.shadowColor = [UIColor yellowColor];  阴影颜色

    shadow.shadowBlurRadius = 1.0;              阴影圆角

    */// NSShadow,默认没有阴影,dict[NSShadowAttributeName] = shadow;// NSNumber,文本字间距(字与字之间的距离)dict[NSKernAttributeName] = @10;// NSURL或者是链接字符串,文本链接样式,自带下划线,文本颜色是蓝色dict[NSLinkAttributeName] = [NSURLURLWithString:@"http:baidu.com"];// NSURL或者是链接字符串,文本链接样式,自带下划线,文本颜色是蓝色dict[NSLinkAttributeName] =@"http:baidu.com";// NSNumber,本文的扩张倍数,负数的话相当于缩小dict[NSExpansionAttributeName] = @(-0.5);// NSNumber,本文的斜体程度以及斜体方向,默认0不歪斜,负数相当于右斜,正数相当于左斜,歪斜的程度由数字的大小决定dict[NSObliquenessAttributeName] = @(0.7);// NSNumber,行文本基线的偏移量dict[NSBaselineOffsetAttributeName] = @(15.0);// 貌似是文本写入方向dict[NSWritingDirectionAttributeName] = @[@(NSWritingDirectionOverride),@(NSWritingDirectionRightToLeft)];// 文本的垂直与水平,目前在iOS,它总是水平,任何其他值的行为是未定义的dict[NSVerticalGlyphFormAttributeName] = @(1);// 在文本中插入表情NSTextAttachment*textAtt = [[NSTextAttachmentalloc] init];    textAtt.image = [UIImageimageNamed:@"cloud.png"];    dict[NSAttachmentAttributeName] = textAtt;

本文为转载的出处再下方👇

作者:林夕不昔

链接:https://www.jianshu.com/p/badee2350860

来源:简书

简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

上一篇下一篇

猜你喜欢

热点阅读