【问题标题】:UILabel Shadow Has Unwanted Cut OffUILabel 阴影已被不需要的切断
【发布时间】:2015-01-26 12:50:40
【问题描述】:

我有一个 UILabel,我可以在其中添加阴影。 UILabel 出现了,阴影也出现了,但是最左边的阴影被切断了,因此它与文本的边缘对齐。我移动了标签的位置以查看它是否被视图覆盖,但一切都保持不变。我还取出了 sizeToFit,它保持不变。下面是标签的初始化:

    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);

【问题讨论】:

  • 只是出于兴趣,那是什么字体?

标签: ios objective-c uilabel shadow


【解决方案1】:

我认为你需要添加

scoreLabel.clipsToBounds = NO;

【讨论】:

  • 仍然有同样的行为
  • 可以发截图吗?
  • 这看起来确实像其他东西在上面切割它。可能想运行视图检查器并查看究竟是什么
【解决方案2】:

我添加了这个,然后中断消失了:

scoreLabel.textAlignment = NSTextAlignmentCenter;

【讨论】:

  • 这不是一个真正“正确”的解决方案 :) 但如果它对你有用 - 很好
  • 我做了一个 View Inspector 并且标签在顶部 @sha,但我注意到文本在标签的左边缘,所以我添加了这个,现在它居中并且没有剪切关闭。
  • 你应该能够实现你尝试使用左对齐来实现的目标。
  • 我的标签的阴影也被切断了,但改变对齐方式对我不起作用......我想我必须让边界“感知阴影”但无法弄清楚然而。将 clipsToBounds 设置为 false 也无济于事。
【解决方案3】:

我在使用自定义字体时遇到了这个问题,并通过继承 UILabel 并将 shadowOffset 添加到 intrinsicContentSize 来解决这个问题:

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

【讨论】:

  • 这不起作用。
【解决方案4】:

这是您想要的解决方案:

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

【讨论】:

    猜你喜欢
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 2015-01-13
    相关资源
    最近更新 更多