[英]UILabel Shadow Has Unwanted Cut Off
我有一個添加陰影的 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);
我有這個問題有一個自定義的字體和通過繼承的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))
}
}
我想你需要補充一下
scoreLabel.clipsToBounds = NO;
這是您想要的解決方案:
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.