给UILabel添加边框

本文介绍了如何在iOS开发中为UILabel添加边框,通过设置attributedText属性,结合NSStrokeWidthAttributeName属性来实现边框效果。同时需要注意,当NSStrokeWidthAttributeName值为正数时,内部字体可能被描边颜色覆盖,而负数值则能保持字体清晰可见。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


效果:


代码

-(NSMutableAttributedString *)useString:(NSString *)str{
    NSDictionary *attri = @{NSStrokeWidthAttributeName:@3,NSStrokeColorAttributeName:[UIColor colorWithHexString:@"#373737" alpha:0.75]};
    NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:str];
    [aStr setAttributes:attri range:NSMakeRange(0, str.length)];
    
    return aStr;
}

使用:

self.timeLabel.attributedText = [selfuseString:[NSStringstringWithFormat:@"%@/%@",timeString,self.totalTime]];


注意:

代码:

-(NSMutableAttributedString *)useString:(NSString *)str{
    NSDictionary *attri = @{NSStrokeWidthAttributeName:@-3,NSStrokeColorAttributeName:[UIColor colorWithHexString:@"#373737" alpha:0.75],NSForegroundColorAttributeName:[UIColor whiteColor]};
    NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:str];
    [aStr setAttributes:attri range:NSMakeRange(0, str.length)];
    
    return aStr;
}


图片:



NSStrokeWidthAttributeName属性设置为3(正)时候里面字体颜色是看不清的描边颜色为主;

当设置为-3时候里面字体颜色是可以看清的



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值