Ios 如何使用删除线文本创建UILabel?
我想创建一个Ios 如何使用删除线文本创建UILabel?,ios,swift,cocoa-touch,uilabel,strikethrough,Ios,Swift,Cocoa Touch,Uilabel,Strikethrough,我想创建一个UILabel,其中的文本如下 myLabel.attributedText = "my string".strikeThrough() 我该怎么做?当文本很小时,行也应该很小。您可以在IOS 6中使用NSMutableAttributedString进行此操作 NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:@"$198"]; [attString add
UILabel
,其中的文本如下
myLabel.attributedText = "my string".strikeThrough()
我该怎么做?当文本很小时,行也应该很小。您可以在IOS 6中使用NSMutableAttributedString进行此操作
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:@"$198"];
[attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])];
yourLabel.attributedText = attString;
您可以在IOS 6中使用NSMutableAttributeString来完成此操作
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:@"$198"];
[attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])];
yourLabel.attributedText = attString;
SWIFT 4更新代码
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
然后:
使字符串的某些部分敲击,然后提供范围
let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)
NSString* strPrice = @"£399.95";
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:strPrice];
[finalString addAttribute: NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range: NSMakeRange(0, [titleString length])];
self.lblOldPrice.attributedText = finalString;
let example = NSAttributedString(string: "440").withStrikeThrough(1)
myLabel.attributedText = example
目标-C 在iOS 6.0中
UILabel
支持nsattributestring
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:@2
range:NSMakeRange(0, [attributeString length])];
Swift
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your String here")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
定义:
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange
参数列表:
名称:指定属性名称的字符串。属性键可以由另一个框架提供,也可以是您定义的自定义键。有关在何处查找系统提供的属性键的信息,请参阅NSAttributedString类参考中的概述部分
值:与名称关联的属性值
aRange:指定属性/值对应用的字符范围
然后
对于低于iOS 6.0版本的
,您需要第三方组件来执行此操作。
其中一个是,另一个是。SWIFT 4更新代码
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
然后:
使字符串的某些部分敲击,然后提供范围
let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)
NSString* strPrice = @"£399.95";
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:strPrice];
[finalString addAttribute: NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range: NSMakeRange(0, [titleString length])];
self.lblOldPrice.attributedText = finalString;
let example = NSAttributedString(string: "440").withStrikeThrough(1)
myLabel.attributedText = example
目标-C
在iOS 6.0中UILabel
支持nsattributestring
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:@2
range:NSMakeRange(0, [attributeString length])];
Swift
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your String here")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
定义:
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange
参数列表:
名称:指定属性名称的字符串。属性键可以由另一个框架提供,也可以是您定义的自定义键。有关在何处查找系统提供的属性键的信息,请参阅NSAttributedString类参考中的概述部分
值:与名称关联的属性值
aRange:指定属性/值对应用的字符范围
然后
对于低于iOS 6.0版本的,您需要第三方组件来执行此操作。
其中一个是,另一个是。对于这个简单的例子,我更喜欢NSAttributedString
而不是NSMutableAttributedString
:
NSAttributedString * title =
[[NSAttributedString alloc] initWithString:@"$198"
attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
[label setAttributedText:title];
用于指定属性字符串的NSUnderlineStyleAttributeName
和nsStrikethrowStyleAttributeName
属性的常量:
typedef enum : NSInteger {
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,
NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,
NSUnderlineByWord = 0x8000
} NSUnderlineStyle;
对于这种简单的情况,我更喜欢NSAttributedString
而不是NSMutableAttributedString
:
NSAttributedString * title =
[[NSAttributedString alloc] initWithString:@"$198"
attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
[label setAttributedText:title];
用于指定属性字符串的NSUnderlineStyleAttributeName
和nsStrikethrowStyleAttributeName
属性的常量:
typedef enum : NSInteger {
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,
NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,
NSUnderlineByWord = 0x8000
} NSUnderlineStyle;
SWIFT代码
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
然后:
感谢;) SWIFT代码
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
然后:
感谢;) 使用下面的代码
let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)
NSString* strPrice = @"£399.95";
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:strPrice];
[finalString addAttribute: NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range: NSMakeRange(0, [titleString length])];
self.lblOldPrice.attributedText = finalString;
let example = NSAttributedString(string: "440").withStrikeThrough(1)
myLabel.attributedText = example
使用下面的代码
let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)
NSString* strPrice = @"£399.95";
NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:strPrice];
[finalString addAttribute: NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range: NSMakeRange(0, [titleString length])];
self.lblOldPrice.attributedText = finalString;
let example = NSAttributedString(string: "440").withStrikeThrough(1)
myLabel.attributedText = example
在Swift中,对单删除线样式使用枚举:
let attrString = NSAttributedString(string: "Label Text", attributes: [NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])
label.attributedText = attrString
其他删除线样式(记住使用.rawValue访问枚举):
- NSUnderlineStyle.StyleNone
- NSUnderlineStyle.StyleSingle
- NSUnderlineStyle.StyleThick
- NSUnderlineStyle.StyleDouble
删除线图案(与样式一起或一起使用):
- NSUnderlineStyle.PatternDot
- NSUnderlineStyle.PatternDash
- NSUnderlineStyle.PatternDashDot
- NSUnderlineStyle.PatternDashDotDot
指定删除线仅应用于单词(而不是空格):
- NSUnderlineStyle.ByWord
在Swift中,将枚举用于单删除线样式:
let attrString = NSAttributedString(string: "Label Text", attributes: [NSStrikethroughStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue])
label.attributedText = attrString
其他删除线样式(记住使用.rawValue访问枚举):
- NSUnderlineStyle.StyleNone
- NSUnderlineStyle.StyleSingle
- NSUnderlineStyle.StyleThick
- NSUnderlineStyle.StyleDouble
删除线图案(与样式一起或一起使用):
- NSUnderlineStyle.PatternDot
- NSUnderlineStyle.PatternDash
- NSUnderlineStyle.PatternDashDot
- NSUnderlineStyle.PatternDashDotDot
指定删除线仅应用于单词(而不是空格):
- NSUnderlineStyle.ByWord
对于任何正在查看如何在tableview单元格(Swift)中执行此操作的人,您必须如下设置.attributeText:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TheCell")!
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: message)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
cell.textLabel?.attributedText = attributeString
return cell
}
如果要删除删除删除线,请执行此操作,否则它将粘住!:
cell.textLabel?.attributedText = nil
对于正在查看如何在tableview单元格(Swift)中执行此操作的任何人,您必须如下设置.attributeText:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TheCell")!
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: message)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
cell.textLabel?.attributedText = attributeString
return cell
}
如果要删除删除删除线,请执行此操作,否则它将粘住!:
cell.textLabel?.attributedText = nil
对于那些面临多行文字罢工问题的人
let attributedString = NSMutableAttributedString(string: item.name!)
//necessary if UILabel text is multilines
attributedString.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue), range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.darkGray, range: NSMakeRange(0, attributedString.length))
cell.lblName.attributedText = attributedString
对于那些面临多行文字罢工问题的人
let attributedString = NSMutableAttributedString(string: item.name!)
//necessary if UILabel text is multilines
attributedString.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue), range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.darkGray, range: NSMakeRange(0, attributedString.length))
cell.lblName.attributedText = attributedString
SWIFT 4
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text Goes Here")
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attributeString.length))
self.lbl_productPrice.attributedText = attributeString
另一种方法是使用字符串扩展名
扩展
extension String{
func strikeThrough()->NSAttributedString{
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attributeString.length))
return attributeString
}
}
调用函数:像这样使用它
testUILabel.attributedText = "Your Text Goes Here!".strikeThrough()
归功于@Yahya-2017年12月更新
归功于@kuzdu-2018年8月更新
SWIFT 4
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text Goes Here")
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attributeString.length))
self.lbl_productPrice.attributedText = attributeString
另一种方法是使用字符串扩展名
扩展
extension String{
func strikeThrough()->NSAttributedString{
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attributeString.length))
return attributeString
}
}
调用函数:像这样使用它
testUILabel.attributedText = "Your Text Goes Here!".strikeThrough()
归功于@Yahya-2017年12月更新
归功于@kuzdu-2018年8月更新
删除Swift iOS中的UILabel文本。请试试这个它对我有用
let attributedString = NSMutableAttributedString(string:"12345")
attributedString.addAttribute(NSAttributedStringKey.baselineOffset, value: 0, range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.styleThick.rawValue), range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSAttributedStringKey.strikethroughColor, value: UIColor.gray, range: NSMakeRange(0, attributedString.length))
yourLabel.attributedText = attributedString
您可以更改“删除线样式”,如styleSingle、styleThick和styleDouble
删除Swift iOS中的UILabel文本。请试试这个它对我有用
let attributedString = NSMutableAttributedString(string:"12345")
attributedString.addAttribute(NSAttributedStringKey.baselineOffset, value: 0, range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.styleThick.rawValue), range: NSMakeRange(0, attributedString.length))
attributedString.addAttribute(NSAttributedStringKey.strikethroughColor, value: UIColor.gray, range: NSMakeRange(0, attributedString.length))
yourLabel.attributedText = attributedString
您可以像styleSingl一样更改“删除线样式”