WechatIMG15.jpeg
比如要实现一个这种效果,当然可能有现成的第三方库,但是几行代码能搞定的就为了这一个功能导入一个库代价还是有点大,那么我们就来自己实现
代码很简单
/**
空心字体
@param str 文本
@param textColor 文本颜色
@param textBorderColor 文本边框颜色
@param strokeWidth 文件边框宽度
@return 文本
*/
+(NSMutableAttributedString *)textHollow:(NSString *)str textColor:(UIColor *)textColor textBorderColor:(UIColor *)textBorderColor strokeWidth:(CGFloat)strokeWidth
{
NSDictionary *dict = @{
NSStrokeColorAttributeName:textBorderColor,
NSStrokeWidthAttributeName : [NSNumber numberWithFloat:strokeWidth],
NSForegroundColorAttributeName:textColor
};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:str attributes:dict];
return attribtStr;
}
使用
self.progressLabel.attributedText = [NSString textHollow:[NSString stringWithFormat:@"%.0f",progress] textColor:_progressColor textBorderColor:[UIColor lightGrayColor] strokeWidth:-3];
需要注意的是:strokeWidth 要设置为负数,设置正数没有效果。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有