系统的镂空文字中间是空的。普通文字又没有镶边。
如何是文字一种颜色,文字又是一种颜色呢?
实现方式是:
在下面放一个普通文字标签,下面放一个系统镂空文字的标签。
这两个标签位置完全相同。这样就能实现带背景的镂空文字了。
最好是字体设置为粗体,字体越大,效果越好。
上下顺序反一下,效果差的多。
[self addSubview:self.underDescribeTitleLabel]; [self addSubview:self.describeTitleLabel]; -(void)setRank:(NSUInteger) rank { _rank = rank; UIColor *strokeColor = BGColorHex(C69116); NSUInteger tempRank = rank/10+1; if(0 == rank) { tempRank = 0; } else if(rank >= 100) { tempRank = 10; } switch (tempRank) { case 0: strokeColor = BGColorHex(A1A1A1); break; case 1: strokeColor = BGColorHex(FE7241); break; case 2: strokeColor = BGColorHex(FF9802); break; case 3: strokeColor = BGColorHex(FFBF01); break; case 4: strokeColor = BGColorHex(CBDC3C); break; case 5: strokeColor = BGColorHex(699E38); break; case 6: strokeColor = BGColorHex(26A99C); break; case 7: strokeColor = BGColorHex(05A8F3); break; case 8: strokeColor = BGColorHex(4F6BEA); break; case 9: strokeColor = BGColorHex(663CB5); break; case 10: strokeColor = BGColorHex(E9407B); break; default: break; } NSString *rankStr = [NSString stringWithFormat:@"%lu", rank]; NSString *str = [NSString stringWithFormat:@"fron%@", rankStr]; NSUInteger fillCharacterCount = 4; NSMutableAttributedString *textAttributedString = [[NSMutableAttributedString alloc] initWithString:str]; [textAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, fillCharacterCount)]; [textAttributedString addAttribute:NSStrokeColorAttributeName value:strokeColor range:NSMakeRange(fillCharacterCount, rankStr.length)]; [textAttributedString addAttribute:NSStrokeWidthAttributeName value:@4.0 range:NSMakeRange(fillCharacterCount, rankStr.length)]; [textAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.f] range:NSMakeRange(0, fillCharacterCount)]; [textAttributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12.f] range:NSMakeRange(fillCharacterCount, rankStr.length)]; self.describeTitleLabel.attributedText = textAttributedString; [self.describeTitleLabel updateLayout]; textAttributedString = [[NSMutableAttributedString alloc] initWithString:str]; [textAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, fillCharacterCount)]; [textAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(fillCharacterCount, rankStr.length)]; [textAttributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.f] range:NSMakeRange(0, fillCharacterCount)]; [textAttributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12.f] range:NSMakeRange(fillCharacterCount, rankStr.length)]; self.underDescribeTitleLabel.attributedText = textAttributedString; [self.underDescribeTitleLabel updateLayout]; NSString *filename = [NSString stringWithFormat:@"lv%lu", tempRank]; self.decribeImageView.image = [UIImage imageNamed:filename]; }