zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.2出现0x80072f8a未指定的错误
    [迷宫中的算法实践]迷宫生成算法——Prim算法
    [MVC学习笔记]7.使用极验验证来制作更高逼格的验证码
    Android UI--提高Android UI体验
    Genymotion的使用 -- A Faster Android Emulator
    【Xamarin】Visual Studio 2013 Xamarin for Android开发环境搭建与配置&Genymotion
    【ASP.NET Web API2】利用HttpClient调用Web API(TODO)
    VMware 11 安装 Mac OS X10.10
    ASP.NET MVC 缓存Outputcache (局部动态)
    【ASP.NET Web API2】初识Web API
  • 原文地址:https://www.cnblogs.com/huayuan320/p/6278321.html
Copyright © 2011-2022 走看看