uilabel 的相关处理 类富文本(自动换行,设置行高,同一个label多种颜色,给文字加下划线 首行缩进 等 等)

6 篇文章 0 订阅
1 篇文章 0 订阅

1.自动换行

UIFont *font=[UIFont systemFontOfSize:lableFont];
self.numberOfLines=0;
self.lineBreakMode=NSLineBreakByWordWrapping;
self.text=content;
self.font=font;
self.textColor=color;
CGSize sizeW=[content sizeWithFont:font constrainedToSize:lableSize lineBreakMode:NSLineBreakByWordWrapping];
self.frame=CGRectMake(self.frame.origin.x, self.frame.origin.y, sizeW.width, sizeW.height);// 此处的 siezeW.with  可以根据需要 设置为 你想要的宽度 如在cell 中让他为300 或者200 等等 此处设置为siezeW.with 是为了 让label 适应宽度   同样的在cell 中 为了让cell 自适应高度 只要将 cell的高度设置为 sizeW.height  就可以了

有时候根据工程需求 需要根据比如“冒号” “ 分号”等换行 就可以使用

//  根据 冒号 分号等的分行 显示   \r\n即为分行 把想换行的特殊符号 换为\r\n 就可以了然后在加上上面的就可以根据你字符串的内容自动换行了
NSString *str = [str1 stringByReplacingOccurrencesOfString:@";" withString:@"\r\n"];


2.调整uilabel 行高间距

UILabel *contenLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 5, 300, 310)];
contenLable.textColor=[Utils getDarkColor];
contenLable.font=[UIFont systemFontOfSize:13];

contenLable.lineBreakMode=UILineBreakModeWordWrap;
contenLable.numberOfLines=0;
// 设置字体间每行的间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 15.0f;
paragraphStyle.maximumLineHeight = 15.0f;
paragraphStyle.minimumLineHeight = 15.0f;
paragraphStyle.lineSpacing = 4.0f;// 行间距
NSString *string = @"此处的 siezeW.with  可以根据需要 设置为 你想要的宽度 如在cell 中让他为300 或者200 等等 此处设置为siezeW.with 是为了 让label 适应宽度   同样的在cell 中 为了让cell 自适应高度 只要将 cell的高度设置为 sizeW.height  就可以了";
NSDictionary *ats = @{
NSParagraphStyleAttributeName : paragraphStyle,
};
contenLable.attributedText = [[NSAttributedString alloc] initWithString:string attributes:ats];

[view addsubview: contenLabel];

3.对label 添加下划线同一个label 多种颜色 

 添加下划

NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:str];
NSRange contentRange = { 0,[content length]};
[content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];

 添加 颜色  只有在ios 7上可以

// NSRange contentRangenew = {0,2};
// [content addAttribute:NSForegroundColorAttributeName value:(id)[[UIColor blueColor]CGColor] range:contentRangenew];
// NSRange conw = {4,19};
// [content addAttribute:NSForegroundColorAttributeName value:(id)[[UIColor blackColor]CGColor] range:conw];

Label.attributedText = content;


4.总结以上

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentJustified;//设置对齐方式
paragraph.lineBreakMode = NSLineBreakByWordWrapping;

NSAttributedString的初始化方法有

   -initWithString:用String初始化,并没有Attributed信息。
   -initWithAttributedString:用AttributedString去初始化。
  -initWithString:Attributed:用string及attribute的dictionary来初始化。

具体AttributtedString属性的键值对如下:

   NSString *const NSFontAttributeName;//值为UIFont,设置字体,默认值为12-point Helvetica(Neue) 。
NSString *const NSParagraphStyleAttributeName;//值为NSParagraphStyle,设置段落属性,默认值为[NSParagraphStyle defaultParagraphStyle]返回的值。

NSMutableParagraphStyle与NSParagraphStyle包括一下属性

alignment //对齐方式
firstLineHeadIndent //首行缩进
headIndent //缩进
tailIndent //尾部缩进
lineBreakMode //断行方式
maximumLineHeight //最大行高
minimumLineHeight //最低行高
lineSpacing //行距
paragraphSpacing //段距
paragraphSpacingBefore //段首空间
baseWritingDirection //句子方向
lineHeightMultiple //可变行高,乘因数。
hyphenationFactor //连字符属性
NSString *const NSForegroundColorAttributeName;//值为UIColor,字体颜色 默认黑色


NSString *const NSBackgroundColorAttributeName;//值为UIColor,字体背景色,默认没有。

NSString *const NSLigatureAttributeName;//值为整型NSNumber,连字属性,一般中文用不到,在英文中可能出现相邻字母连笔的情况。0为不连笔;1为默认连笔,也是默认值;2在ios 上不支持。

NSString *const NSKernAttributeName;//值为浮点数NSNumber,字距属性,默认值为0。


NSString *const NSStrikethroughStyleAttributeName;//值为整型NSNumber,可取值为
enum {
NSUnderlineStyleNone = 0×00,
NSUnderlineStyleSingle = 0×01,
};设置删除线。

NSString *const NSUnderlineStyleAttributeName;//同上。设置下划线。

NSString *const NSStrokeColorAttributeName;//值为UIColor,默认值为nil,设置的属性同ForegroundColor。

NSString *const NSStrokeWidthAttributeName;//值为浮点数NSNumber。设置比画的粗细。

NSString *const NSShadowAttributeName;//值为NSShadow,设置比画的阴影,默认值为nil。

NSString *const NSVerticalGlyphFormAttributeName;//值为整型NSNumber,0为水平排版的字,1为垂直排版的字。


首行缩进举例子

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];// 下面就可以设置他的各种属性了
paragraph.firstLineHeadIndent = 20;// 这里  只是设置了首行缩进  我这里设置首行缩进 20 缩进两个字  根据需求可以调节缩进的大小
NSDictionary *ats = @{
NSParagraphStyleAttributeName : paragraph,
};
Label.attributedText = [[NSMutableAttributedString alloc]initWithString:str attributes:ats];



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值