139

UILabelこのようなテキストのを作成したい

ここに画像の説明を入力

これどうやってするの?文字が小さい場合は、線も小さくする必要があります。

4

21 に答える 21

250

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です。

于 2012-10-30T05:29:30.413 に答える
50

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
于 2015-09-14T03:09:33.213 に答える
36

この単純なケース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;  
于 2014-01-27T10:24:59.863 に答える
34

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
于 2018-05-07T13:32:25.827 に答える
23

スウィフトコード

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: "Your Text")
    attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))

それから:

yourLabel.attributedText = attributeString

プリンスの回答に感謝します;)

于 2015-01-25T22:34:19.660 に答える
9

NSMutableAttributedString を使用して IOS 6 で実行できます。

NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:@"$198"];
[attString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:NSMakeRange(0,[attString length])];
yourLabel.attributedText = attString;
于 2012-10-30T05:21:39.653 に答える
1

テキスト プロパティを属性付きに変更し、テキストを選択して右クリックし、フォント プロパティを取得します。取り消し線をクリックします。 スクリーンショット

于 2019-07-02T17:33:20.967 に答える
0

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!")
于 2017-12-26T07:44:28.950 に答える