简体   繁体   English

UILabel 阴影已被不需要的切断

[英]UILabel Shadow Has Unwanted Cut Off

I have a UILabel that I add a shadow to.我有一个添加阴影的 UILabel。 The UILabel shows up and so does the shadow, but the leftmost shadow is cut off so that it is in line with the edge of the text. UILabel 显示出来,阴影也显示出来,但最左边的阴影被切掉,使其与文本的边缘保持一致。 I moved the position of the label to see if it was being covered by a view, but everything stayed the same.我移动了标签的位置以查看它是否被视图覆盖,但一切都保持不变。 I also took out the sizeToFit, and it stayed the same.我还取出了 sizeToFit,它保持不变。 Here is the initialization of the label:下面是标签的初始化:

    UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    scoreLabel.text = text;
    [scoreLabel setFont:[UIFont fontWithName:fontName size:fontSize]];
    scoreLabel.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];

    scoreLabel.shadowColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f];
    scoreLabel.shadowOffset = CGSizeMake(-10.0, 2.0);
    scoreLabel.clipsToBounds = NO;

    [scoreLabel sizeToFit];

    scoreLabel.center = CGPointMake(x, y);

I had this problem with a custom font and solved it by subclassing UILabel and adding the shadowOffset to intrinsicContentSize with this override: 我有这个问题有一个自定义的字体和通过继承的UILabel并添加解决它shadowOffsetintrinsicContentSize与此覆盖:

override var intrinsicContentSize: CGSize  {
    get {
        let s = super.intrinsicContentSize
        return CGSize(width: s.width + abs(shadowOffset.width), height: s.height + abs(shadowOffset.height))
    }
}

我想你需要补充一下

scoreLabel.clipsToBounds = NO;

This is the solution you want:这是您想要的解决方案:

let style = NSMutableParagraphStyle()
style.firstLineHeadIndent = someInset // can be shadowBlur and/or shadowRadius
style.headIndent = someInset
style.tailIndent = -someInset // must be negative since its at the end (tail)

let attrString = NSMutableAttributedString(string: "Some Text")
attrString.addAttribute(.paragraphStyle, value: style, range: .init(location: 0, attrString.length)
let label = UILabel()
label.numberOfLines = 0 // not necessary
label.attributedText = attrString

我添加了这个,切断消失了:

scoreLabel.textAlignment = NSTextAlignmentCenter;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM