• UILable常见的一些富文本设置


    //lable字体大小自适应宽度

    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 100)];
        lable.numberOfLines = 0;
        lable.backgroundColor = [UIColor blueColor];
        lable.text = @"abchdh";
        lable.font = [UIFont systemFontOfSize:200];
        lable.adjustsFontSizeToFitWidth = YES;
        lable.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
        [self.view addSubview:lable];

    //富文本的设置

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"已抢购 %@ 个",model.getNum]];

        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, getNumString.length -1)];

        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4, getNumString.length - 6)];

        [_getNumLabel setAttributedText:attributedString];

    //指定table,textView的行间距

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

        paragraphStyle.lineSpacing = 5;// 字体的行间距

        

        NSDictionary *attributes = @{

                                     NSFontAttributeName:[UIFont systemFontOfSize:13],

                                     NSParagraphStyleAttributeName:paragraphStyle

                                     };

        

        _contentTextView.attributedText = [[NSAttributedString alloc] initWithString:str attributes:attributes];

    //动态lable高度

    - (CGSize)contentSize:(UILabel *)lable {

        NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];

        paragraphStyle.lineBreakMode = lable.lineBreakMode;

        paragraphStyle.alignment = lable.textAlignment;

        

        NSDictionary * attributes = @{NSFontAttributeName : lable.font,

                                      NSParagraphStyleAttributeName : paragraphStyle};

        

        CGSize contentSize = [lable.text boundingRectWithSize:CGSizeMake(lable.frame.size.width, MAXFLOAT)

                                                     options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)

                                                  attributes:attributes

                                                     context:nil].size;

        return contentSize;

    }

    //指定区域内文字加横线

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 200, 50)];
        [self.view addSubview:label];
        label.text = @"12.89";
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor grayColor]; // 横线的颜色跟随label字体颜色改变
        NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",label.text]];
        [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
        label.attributedText = newPrice;
  • 相关阅读:
    寒宣资料汇编
    Windows邮件客户端
    Dear Menuhin
    2017-11-11 Sa Oct Spider
    2017-11-11 Sa Oct How to open a browser in Python
    skynet游戏服务器框架分享
    钉钉 机器人接入 自定义webhook
    golang语法笔记
    [学习笔记]尝试go-micro开发微服务<第一波>
    [学习笔记]Golang--基础数据类型
  • 原文地址:https://www.cnblogs.com/huayuan320/p/6278321.html
Copyright © 2020-2023  润新知