UILabel使用,隐藏电话号码

下面的代码就可以实现将电话号码中间四位替换啦!

- (void)addLineLabel {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, 300, 20)];
    label.text = @"将电话号码12345678901中间四位表示成*号";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor redColor];
    //将号码中间四位替换成*号
    NSMutableString *mStr = [[NSMutableString alloc] initWithString:label.text];
    [mStr replaceCharactersInRange:NSMakeRange(8,4) withString:@"****"];
    label.text = mStr;
    
    //将电话号码加粗,字体放大
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:label.text];
    [str addAttribute:NSForegroundColorAttributeName value:ILColor(0, 0, 0) range:NSMakeRange(5, 11)];
    [str addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(5, 11)];
    label.attributedText = str;
    [self.view addSubview:label];
}```

你可能感兴趣的:(UILabel使用,隐藏电话号码)