在现有NSMutableAttributed字符串中添加属性

时间:2017-07-03 07:24:59

标签: ios objective-c nsattributedstring strikethrough

我正在研究一些属性字符串格式。我需要在现有的NSMutableAttributed字符串中添加删除线。是否有可能在下面给出的例子中。

First Task For Bhasin
30 Jun 2017 12:05 PM till 30 Jun 2017 01:05 PM{
NSBackgroundColor = "<CGColor 0x6100000934c0> [<CGColorSpace 0x6080000303c0> (kCGColorSpaceICCBased; kCGColorSpaceModelMonochrome; Generic Gray Gamma 2.2 Profile; extended range)] ( 0 0 )";
NSColor = "<CGColor 0x6180002a2ac0> [<CGColorSpace 0x610000230920> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1; extended range)] ( 1 0 0 1 )";
NSFont = "<UICTFont: 0x7fac5476a0b0> font-family: \"Helvetica\"; font-weight: normal; font-style: normal; font-size: 15.00pt";
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n    28L,\n    56L,\n    84L,\n    112L,\n    140L,\n    168L,\n    196L,\n    224L,\n    252L,\n    280L,\n    308L,\n    336L\n), DefaultTabInterval 0, Blocks (\n), Lists (\n), BaseWritingDirection -1, HyphenationFactor 0, TighteningForTruncation NO, HeaderLevel 0";
}

1 个答案:

答案 0 :(得分:6)

attributedString是现有的NSMutableAttributedString

目标C

NSAttributedString转换为NSMutableAttributedString

 NSMutableAttributedString *attributedString = [NSMutableAttributedString alloc]initWithAttributedString:<#Your String Here#>];

添加Strikethrough属性

 [attributedString addAttributes:@{ NSStrikethroughStyleAttributeName : [NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(x, y)];

x =起始索引,y =字符串的长度。

斯威夫特:

 attributedString.addAttributes([NSStrikethroughStyleAttributeName: NSNumber(integer: NSUnderlineStyle.StyleSingle.rawValue)], range: NSMakeRange(x, y))

NSAttributedString转换为NSMutableAttributedString

 let attributedString:NSMutableAttributedString = NSMutableAttributedString(attributedString: <#NSAttributedString#>)