在 UILabel 中绘制一条简单的线
Drawing a simple line in a UILabel
有没有办法通过 UILabel 添加简单的线条。我有一个 40px 高的 UILabel,只是想在中间水平绘制一条黑线(20px)。有没有一种方法可以做到这一点而不必创建图像并将其设置为背景?
您应该能够继承 UILabel
并覆盖 drawRect
方法。
你可以使用 UILabel 来完成
height=1 width=as you need
并将其背景颜色设置为黑色并将其放在您的 40px UILabel 上。希望这会对你有所帮助。
是的,有几种方法。比如可以给label添加1点高度的subview:
Swift 3.0 更新:
let lineView = UIView(
frame: CGRect(x: 0,
y: label.bounds.size.height / 2,
width: label.bounds.size.width,
height: 1
)
)
lineView.backgroundColor = UIColor.black
label.addSubview(lineView)
如果您的标签包含文本,那么您可以像这样在标签中使用删除线。
Objective-C
NSString *newStringStrike = @"your text";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:newStringStrike];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:@1
range:NSMakeRange(0, [attributeString length])];
labelName.attributedText = attributeString;
使用 Swift 3.1:
let newStringStrike = "your text"
let attributeString = NSMutableAttributedString(string: newStringStrike)
attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, attributedString.length))
labelName.attributedText = attributedString
在 swift4 中:
let newStringStrike = "your text"
let attributeString = NSMutableAttributedString(string: newStringStrike)
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 1, range: NSMakeRange(0, attributeString.length))
yourLabel = attributeString
只需创建一个 NSAttributedString 并分配给您的标签
let newStringStrike = "your text"
let attributeString = NSMutableAttributedString(string: newStringStrike)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: NSMakeRange(0, attributeString.length))
yourLabel.attributedText = attributeString
根据之前的答案,您可以为 swift 5
定义这样的内容
extension UILabel{
func setTextWithStrike(text: String)
{
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: NSMakeRange(0, attributedString.length))
self.attributedText = attributedString
}
}
然后像这样使用它:
labelWithStike.setTextWithStrike(text: "text with strike")
有没有办法通过 UILabel 添加简单的线条。我有一个 40px 高的 UILabel,只是想在中间水平绘制一条黑线(20px)。有没有一种方法可以做到这一点而不必创建图像并将其设置为背景?
您应该能够继承 UILabel
并覆盖 drawRect
方法。
你可以使用 UILabel 来完成
height=1 width=as you need
并将其背景颜色设置为黑色并将其放在您的 40px UILabel 上。希望这会对你有所帮助。
是的,有几种方法。比如可以给label添加1点高度的subview:
Swift 3.0 更新:
let lineView = UIView(
frame: CGRect(x: 0,
y: label.bounds.size.height / 2,
width: label.bounds.size.width,
height: 1
)
)
lineView.backgroundColor = UIColor.black
label.addSubview(lineView)
如果您的标签包含文本,那么您可以像这样在标签中使用删除线。
Objective-C
NSString *newStringStrike = @"your text";
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:newStringStrike];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:@1
range:NSMakeRange(0, [attributeString length])];
labelName.attributedText = attributeString;
let newStringStrike = "your text"
let attributeString = NSMutableAttributedString(string: newStringStrike)
attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, attributedString.length))
labelName.attributedText = attributedString
在 swift4 中:
let newStringStrike = "your text"
let attributeString = NSMutableAttributedString(string: newStringStrike)
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 1, range: NSMakeRange(0, attributeString.length))
yourLabel = attributeString
只需创建一个 NSAttributedString 并分配给您的标签
let newStringStrike = "your text"
let attributeString = NSMutableAttributedString(string: newStringStrike)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: NSMakeRange(0, attributeString.length))
yourLabel.attributedText = attributeString
根据之前的答案,您可以为 swift 5
定义这样的内容extension UILabel{
func setTextWithStrike(text: String)
{
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: NSMakeRange(0, attributedString.length))
self.attributedText = attributedString
}
}
然后像这样使用它:
labelWithStike.setTextWithStrike(text: "text with strike")