知识点总结14:label多行显示文字&图文混排

UITextField的attributedPlaceholder和UILabel的attributedText类似都可以用带属性的文字来设置.

  • 1.不可变富文本NSAttributedString
/*将label加到控制器的view上*/
- (void)viewDidLoad {
    [super viewDidLoad];
    /**** 1.不可变富文本NSAttributedString ****/
    // 1.0文本
    NSString *text = @"哈哈哈我是富文本";
    
    // 1.1属性字典
    NSMutableDictionary *attributedDict = [NSMutableDictionary dictionary];
    attributedDict[NSBackgroundColorAttributeName] = [UIColor yellowColor];
    attributedDict[NSFontAttributeName] = [UIFont systemFontOfSize:8];
    //    NSMutableDictionary *attributedDict2 = [NSMutableDictionary dictionary];
    //    attributedDict2[NSFontAttributeName] = [UIFont boldSystemFontOfSize:13];
    
    // 1.2属性文字
    NSAttributedString *attriStr= [[NSAttributedString alloc] initWithString:text attributes:attributedDict];
    
    // 1.3设置控件属性
    UILabel *label = [[UILabel alloc] init];
    // 文本居中
    label.textAlignment = NSTextAlignmentCenter;
    // 文本标题
    label.attributedText = attriStr;
    
    // 1.4设置控件的frame
    label.frame = CGRectMake(10, 70, 100, 50);
    label.backgroundColor = [UIColor redColor];
    [self.view addSubview:label];
}
  • 2.富文本多行不同格式显示(相对于NSAttributedString,即mutableAttriStr)
/*将label加到控制器的view上*/
- (void)viewDidLoad {
    [super viewDidLoad];
    
/**** 2.富文本多行不同格式显示 ****/
    // 2.0文本
    NSString *text2 = @"嘻嘻嘻\n富文本多行显示\n我是第三行";
    
    // 2.1多组属性字典
    NSMutableDictionary *attributedDict1 = [NSMutableDictionary dictionary];
    attributedDict1[NSBackgroundColorAttributeName] = [UIColor blueColor];
    attributedDict1[NSForegroundColorAttributeName] = [UIColor redColor];
    attributedDict1[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    
    NSMutableDictionary *attributedDict2 = [NSMutableDictionary dictionary];
    attributedDict2[NSBackgroundColorAttributeName] = [UIColor greenColor];
    attributedDict2[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    attributedDict2[NSFontAttributeName] = [UIFont boldSystemFontOfSize:10];

    // 2.2富文本(属性文字)
    NSMutableAttributedString *mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:text2];
    // 要注意setAttributes和addAttributes的区别
    [mutableAttriStr setAttributes:attributedDict1 range:NSMakeRange(0, text.length)];
    [mutableAttriStr addAttributes:attributedDict2 range:NSMakeRange(4, 5)];
    [mutableAttriStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(12, 5)];
    [mutableAttriStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:11] range:NSMakeRange(12, 5)];
    
    // 2.3设置控件属性
    UILabel *label2 = [[UILabel alloc] init];
    label2.attributedText = mutableAttriStr;
    label2.textAlignment = NSTextAlignmentRight;
    label2.backgroundColor = [UIColor yellowColor];
    // 必须设置行数为0,才能换行
    label2.numberOfLines = 0;

    
    // 2.4设置控件的frame
    label2.frame = CGRectMake(120, 75, 150, 60);
    [self.view addSubview:label2];
}

特别注意:
(1) 必须设置行数为0,才能换行
label2.numberOfLines = 0;

(2)换行就用"\n"
NSString *text2 = @"嘻嘻嘻\n富文本多行显示\n我是第三行";

  • 3.图文混排(带图片)
/*将label加到控制器的view上*/
- (void)viewDidLoad {
    [super viewDidLoad];
   
/**** 3.图文混排 ****/
    // 1.第一部分:二哈
    NSString *strOne = @"二哈";
    NSMutableDictionary *attriDictOne = [NSMutableDictionary dictionary];
    attriDictOne[NSForegroundColorAttributeName] = [UIColor redColor];
    attriDictOne[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    NSAttributedString *attriStrOne = [[NSAttributedString alloc] initWithString:strOne attributes:attriDictOne];
    
    // 2.第二部分:图片
    NSTextAttachment *attactchment = [[NSTextAttachment alloc] init];
    attactchment.image = [UIImage imageNamed:@"header_cry_icon"];
    // 让图片的宽和高与文字一样
    CGFloat lineH = [UIFont systemFontOfSize:12].lineHeight;
    // 调整图片的大小和位置
    attactchment.bounds = CGRectMake(0, -3, lineH, lineH);
    NSAttributedString *attriStrTwo = [NSAttributedString attributedStringWithAttachment:attactchment];
    
    // 3.第三部分:图文混排
    NSString *strThree = @"图文混排";
    NSMutableDictionary *attriDictThree = [NSMutableDictionary dictionary];
    attriDictThree[NSForegroundColorAttributeName] = [UIColor blueColor];
    attriDictThree[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    NSAttributedString *attriStrThree = [[NSAttributedString alloc] initWithString:strThree attributes:attriDictThree];
    
    // 4.第四部分:换行\n
    NSString *strFour = @"\n";
    NSAttributedString *attriStrFour = [[NSAttributedString alloc] initWithString:strFour];
    
    // 5.结束语:拜拜
    NSString *strFive = @"拜拜";
    NSMutableDictionary *attriDictFive = [NSMutableDictionary dictionary];
    attriDictFive[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    attriDictFive[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    NSAttributedString *attriStrFive = [[NSAttributedString alloc] initWithString:strFive attributes:attriDictFive];
    
    // 6.组合成整体
    NSMutableAttributedString *totalAttriStr = [[NSMutableAttributedString alloc] init];
    [totalAttriStr appendAttributedString:attriStrOne];
    [totalAttriStr appendAttributedString:attriStrTwo];
    [totalAttriStr appendAttributedString:attriStrThree];
    [totalAttriStr appendAttributedString:attriStrFour];
    [totalAttriStr appendAttributedString:attriStrFive];
    
    UILabel *attriLabel = [[UILabel alloc] init];
    attriLabel.attributedText = totalAttriStr;
    attriLabel.numberOfLines = 0;
    attriLabel.backgroundColor = [UIColor colorWithRed:40 green:40 blue:40 alpha:0.7];
    attriLabel.frame = CGRectMake(100, 150, 200, 40);
    attriLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:attriLabel];

}

你可能感兴趣的:(知识点总结14:label多行显示文字&图文混排)