UILabel
このようなテキストのを作成したい
これどうやってするの?文字が小さい場合は、線も小さくする必要があります。
UILabel
このようなテキストのを作成したい
これどうやってするの?文字が小さい場合は、線も小さくする必要があります。
SWIFT5更新コード
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSRange(location: 0, length: attributeString.length))
それから:
yourLabel.attributedText = attributeString
弦の一部を打つには、範囲を指定します
let somePartStringRange = (yourStringHere as NSString).range(of: "Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: somePartStringRange)
Objective-C
iOS6.0 では>UILabel
サポートNSAttributedString
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
value:@2
range:NSMakeRange(0, [attributeString length])];
迅速
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
Parameters List:
name:属性名を指定する文字列。属性キーは、別のフレームワークから提供することも、定義したカスタムキーにすることもできます。システム提供の属性キーの場所については、NSAttributedStringクラスリファレンスの概要セクションを参照してください。
value:名前に関連付けられた属性値。
aRange:指定された属性/値のペアが適用される文字の範囲。
それで
yourLabel.attributedText = attributeString;
あなたはこれlesser than iOS 6.0 versions
をする必要がある3-rd party component
からです。それらの1つはTTTAttributedLabelであり、もう1つはOHAttributedLabelです。
Swift では、単一の取り消し線スタイルを使用します。
let attributedText = NSAttributedString(
string: "Label Text",
attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue]
)
label.attributedText = attributedText
追加の取り消し線スタイル ( .rawValue を使用することを忘れないでください):
.none
.single
.thick
.double
取り消し線パターン (スタイルと OR 演算する):
.patternDot
.patternDash
.patternDashDot
.patternDashDotDot
取り消し線が (スペースではなく) 単語にのみ適用されるように指定します。
.byWord
この単純なケースNSAttributedString
よりも好きです:NSMutableAttributedString
NSAttributedString * title =
[[NSAttributedString alloc] initWithString:@"$198"
attributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)}];
[label setAttributedText:title];
NSUnderlineStyleAttributeName
属性付き文字列のとNSStrikethroughStyleAttributeName
属性の両方を指定するための定数:
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 5.0 の取り消し線
let attributeString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle,
value: NSUnderlineStyle.single.rawValue,
range: NSMakeRange(0, attributeString.length))
self.yourLabel.attributedText = attributeString
それは私にとって魅力のように機能しました。
延長で使う
extension String {
func strikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(
NSAttributedString.Key.strikethroughStyle,
value: NSUnderlineStyle.single.rawValue,
range:NSMakeRange(0,attributeString.length))
return attributeString
}
}
このように呼び出します
myLabel.attributedText = "my string".strikeThrough()
取り消し線の有効化/無効化の UILabel 拡張。
extension UILabel {
func strikeThrough(_ isStrikeThrough:Bool) {
if isStrikeThrough {
if let lblText = self.text {
let attributeString = NSMutableAttributedString(string: lblText)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0,attributeString.length))
self.attributedText = attributeString
}
} else {
if let attributedStringText = self.attributedText {
let txt = attributedStringText.string
self.attributedText = nil
self.text = txt
return
}
}
}
}
次のように使用します。
yourLabel.strikeThrough(btn.isSelected) // true OR false
スウィフトコード
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "Your Text")
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
それから:
yourLabel.attributedText = attributeString
プリンスの回答に感謝します;)
NSMutableAttributedString を使用して IOS 6 で実行できます。
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:@"$198"];
[attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])];
yourLabel.attributedText = attString;
テキスト プロパティを属性付きに変更し、テキストを選択して右クリックし、フォント プロパティを取得します。取り消し線をクリックします。
String 拡張機能を作成し、以下のメソッドを追加します
static func makeSlashText(_ text:String) -> NSAttributedString {
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: text)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
return attributeString
}
次に、このようにラベルに使用します
yourLabel.attributedText = String.makeSlashText("Hello World!")