zoukankan      html  css  js  c++  java
  • NSMutableAttributedString(富文本)的简单使用

    #import "ViewController.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 84, self.view.frame.size.width-40, self.view.frame.size.height-84-20)];
        label.text = @"春种一粒粟,秋成万颗子。\n四海无闲田,农夫犹饿死。\n锄禾日当午,汗滴禾下土。\n谁知盘中餐,粒粒皆辛苦。";
        [self.view addSubview:label];
        NSRange rangeOne = [label.text rangeOfString:@"春种一粒粟"];
        NSRange rangeTwo = [label.text rangeOfString:@"秋成万颗子"];
        NSRange rangeThree = [label.text rangeOfString:@"四海无闲田"];
        NSRange rangeFour = [label.text rangeOfString:@"农夫犹饿死"];
        NSRange rangeFive = [label.text rangeOfString:@"锄禾日当午"];
        NSRange rangeSix = [label.text rangeOfString:@"汗滴禾下土"];
        NSRange rangeSeven = [label.text rangeOfString:@"谁知盘中餐"];
        NSRange rangeEight = [label.text rangeOfString:@"粒粒皆辛苦"];
        NSMutableAttributedString *attributedLabel = [[NSMutableAttributedString alloc] initWithString:label.text];
        //字体
        [attributedLabel addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, label.text.length)];
        //段落
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 20; //行间距
        paragraphStyle.alignment = NSTextAlignmentCenter;
        [attributedLabel addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, label.text.length)];
        //字体颜色
        [attributedLabel addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeOne.location, rangeOne.length)];
        //字体底色
        [attributedLabel addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeTwo.location, rangeTwo.length)];
        //删除线
        [attributedLabel addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:3] range:NSMakeRange(rangeThree.location, rangeThree.length)];
        [attributedLabel addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeThree.location, rangeThree.length)];
        //下划线
        [attributedLabel addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(rangeFour.location, rangeFour.length)];
        [attributedLabel addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeFour.location, rangeFour.length)];
        //文字间距
        [attributedLabel addAttribute:NSKernAttributeName value:[NSNumber numberWithInt:5] range:NSMakeRange(rangeFive.location, rangeFive.length)];
        //字体倾斜(正值右倾,负值左倾)
        [attributedLabel addAttribute:NSObliquenessAttributeName value:[NSNumber numberWithFloat:0.5] range:NSMakeRange(rangeSix.location, rangeSix.length)];
        //笔画宽度(正值中空,负值填充)
        [attributedLabel addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:1] range:NSMakeRange(rangeSeven.location, rangeSeven.length)];
        //填充颜色
        [attributedLabel addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeSeven.location, rangeSeven.length)];
        //阴影效果
        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowColor = [UIColor redColor];
        shadow.shadowOffset = CGSizeMake(2, 2);
        shadow.shadowBlurRadius = 2;
        [attributedLabel addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(rangeEight.location, rangeEight.length)];
        //自适应高
        label.numberOfLines = 0;
        CGRect labelStringRect = [attributedLabel boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*2, 9999) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
        CGRect labelRect = label.frame;
        labelRect.size.height = labelStringRect.size.height;
        label.frame = labelRect;
        
        label.attributedText = attributedLabel;
    }

    效果图如下:

  • 相关阅读:
    嵌入式Linux系统移植(二)——交叉编译工具集
    嵌入式linux系统移植(一)
    C语言常用关键语法精华总结
    ARM汇编常用指令
    嵌入式Linux系统移植——uboot常用命令
    VHDL的参数写在一个vhd文件里
    [PAT] 1077 Kuchiguse (20 分)Java
    [PAT] 1073 Scientific Notation (20 分)Java
    [PAT] 1069 The Black Hole of Numbers (20 分)Java
    [PAT] 1065 A+B and C (64bit) (20 分)Java
  • 原文地址:https://www.cnblogs.com/fanzhiying/p/4901335.html
Copyright © 2011-2022 走看看