UILabel
下線付きのテキストを作成する完全な例を次に示します。
スウィフト5:
let homeLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
let text = NSMutableAttributedString(string: "hello, world!")
let attrs = [NSAttributedString.Key.underlineStyle: NSUnderlineStyle.patternDash.rawValue | NSUnderlineStyle.single.rawValue]
text.addAttributes(attrs, range: NSRange(location: 0, length: text.length))
homeLabel.attributedText = text
スウィフト 4:
let homeLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
let text = NSMutableAttributedString(string: "hello, world!")
let attrs = [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.patternDash.rawValue | NSUnderlineStyle.styleSingle.rawValue]
text.addAttributes(attrs, range: NSRange(location: 0, length: text.length))
homeLabel.attributedText = text
スウィフト 2:
Swift では、 を受け取るInt
メソッドに を渡すNSNumber
ことができるため、 への変換を削除することで、これを少しきれいにすることができますNSNumber
。
text.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleDouble.rawValue, range: NSMakeRange(0, text.length))
注: この回答は以前は元の質問で使用されていましたが、Xcode 6.1 の時点でプロパティに置き換えられたため、toRaw()
現在は正しくありません。toRaw()
rawValue