本文主要是介绍Lable的富文本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/***************************NSMutableParagraphStyle***************************
* 属性:
* lineSpacing 行距
paragraphSpacing 断距
alignment 校准(居中,靠左,靠右)
firstLineHeadIndent 首行缩进
headIndent 头缩进
tailIndent 尾缩进
lineBreakMode 断行模式
minimumLineHeight
maximumLineHeight
baseWritingDirection 书写的方向
lineHeightMultiple 多个行高
paragraphSpacingBefore 之前的断句
hyphenationFactor 用连接字符号连接
defaultTabInterval
allowsDefaultTighteningForTruncation 是否收紧截断
*/
// 2. 常见的属性及说明
//
// NSFontAttributeName
// 字体
// NSParagraphStyleAttributeName
// 段落格式
// NSForegroundColorAttributeName
// 字体颜色
// NSBackgroundColorAttributeName
// 背景颜色
// NSStrikethroughStyleAttributeName
// 删除线格式
// NSUnderlineStyleAttributeName
// 下划线格式
// NSStrokeColorAttributeName
// 删除线颜色
// NSStrokeWidthAttributeName
// 删除线宽度
// NSShadowAttributeName
// 阴影
/// 注意: 老忘: NSMakeRange ( 从哪里开始(0), 你选择的多长也包含空格)
// 设置为多行显示
self.Labble.numberOfLines = 0;
//model 设置
self.Labble.lineBreakMode=NSLineBreakByWordWrapping;
NSString *str=@"设置Label的行间距设置Label的行间距设置Label的行间距设置Label的行间距设置!!!\nLabel的行间距设置Label的行间距设置Label的行间距设置Label的行间距设置Label的行间距设置Label的行间距设置Label的行间距设置Label的行间距设置Label的!!!\n行间距设置Label的行间距";
self.Labble.text=str;
[self.Labble sizeToFit];
// 选择对应的汉字改变颜色
// self.Labble.textAlignment=NSTextAlignmentCenter;
// NSMutableAttributedString * attributedString=[[NSMutableAttributedString alloc]initWithString:str];
// [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange([str rangeOfString:@"行间"].location , [str rangeOfString:@"行间"].length)];
// self.Labble.attributedText=attributedString;
//设置 字间距
// NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc]initWithString:str attributes:@{NSKernAttributeName:@(10) }];
// self.Labble.attributedText = attributedString;
// // 设置 行距;
// NSMutableParagraphStyle * paragraphStyle=[[NSMutableParagraphStyle alloc]init];
// paragraphStyle.lineSpacing= 30;
// [setStringAttribute addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];
// self.Labble.attributedText=setStringAttribute;
// /// 改变 颜色 和 字体的大小、体型
// [setStringAttribute addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 6)];
// [setStringAttribute addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(3, 9)];
// self.Labble.attributedText=setStringAttribute;
}
/// 行尾距离和使文字靠右显示
- (void)setAttributeStringForPriceLabel:(UILabel *)label text:(NSString *)text
{
NSMutableAttributedString *attrString = [[NSMutableAttributedString
alloc] initWithString:text];
NSUInteger length = [text length];
NSMutableParagraphStyle *
style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.tailIndent = -50; //设置与尾部的距离
style.firstLineHeadIndent = 10; /// 设置首行缩进
style.alignment = NSTextAlignmentRight;//靠右显示
[attrString addAttribute:NSParagraphStyleAttributeName value:style
range:NSMakeRange(0, length)];
label.attributedText = attrString;
}
关于:sizeToFit 的使用
对于自己创建的Lable先赋值, 在调用 sizeToFit, Lable的宽的会根据你的text的字数的多少改变Lable的大小;
但是在xib中,要想实现自适应Lable
这篇关于Lable的富文本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!