文字阴影

如果给UILabel直接设置下面的代码,效果仅仅只是多了层黑色的阴影而没有渐变模糊的效果

  shadow.shadowOffset = CGSizeMake(.5,.5);
  shadow.shadowColor = [UIColor lightGrayColor];

所以推荐下面这段代码,可以实现渐变模糊效果

    NSShadow * shadow = [[NSShadow alloc]init];
    shadow.shadowBlurRadius = 1.f;
    shadow.shadowOffset = CGSizeMake(.5,.5);
    shadow.shadowColor = [UIColor lightGrayColor];
    
    NSString * message = [NSString stringWithFormat:@"%@:%@",_model.userName,_model.message];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:message attributes:@{NSShadowAttributeName:shadow}];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:[message rangeOfString:message]];
    
    self.messageLabel.attributedText = attributedString.copy;

你可能感兴趣的:(文字阴影)