ios实现文字添加阴影

WeChat123abbaa08a0960c97c219f4511f7750.png

如上图所示,实现文字阴影效果
以下属性可根据自身需求进行相应调整

shadowColor // 阴影颜色
shadowOffset  // 阴影偏移量
shadowBlurRadius  // 阴影半径内的模糊效果

完整代码如下

-(UILabel *)textShadowLabel{
    if (!_textShadowLabel) {
        _textShadowLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 300, [UIScreen mainScreen].bounds.size.width, 50)];
        _textShadowLabel.text = @"hello,你好";
        _textShadowLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _textShadowLabel;
}
[self.view addSubview:self.textShadowLabel];
[self setTextShadowColor:self.textShadowLabel];

- (void)setTextShadowColor:(UILabel *)curLabel{
    curLabel.textColor = [UIColor colorWithRed:51/255.0 green:145/255.0 blue:255/255.0 alpha:1.0];
    NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc]initWithString:curLabel.text];
    NSShadow *shadow = [[NSShadow alloc]init];
    //shadowBlurRadius阴影半径内的模糊效果
    shadow.shadowBlurRadius = 1.0;
// shadowOffset:阴影偏移,(3,3)向右下偏移,(-3,-3)向左上偏移,(-3,3)向左下偏移,(3,-3)向右上偏移
    shadow.shadowOffset = CGSizeMake(3, 3);
    shadow.shadowColor = [UIColor colorWithRed:51/255.0 green:145/255.0 blue:255/255.0 alpha:0.2];
    [attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0,attributedString.length)];
    curLabel.attributedText = attributedString;
}

你可能感兴趣的:(ios实现文字添加阴影)