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
















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值