2017-10-21 47 views
0

ラベルの下線の色を変更するにはどうすればよいですか?私は、テキスト全体ではなく、色を変更するだけの下線を必要とします。Swiftの下線のみの色

私は下線を取得するには、このコードを使用しています

let underlineAttribute = [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue] 
let underlineAttributedString = NSAttributedString(string: "\(nearSavings[indexPath.row]) ,-", attributes: underlineAttribute) 
cell.detailTextLabel?.attributedText = underlineAttributedString 

しかし、私は、下線の色を設定するには、コードを見つける傾けます。助けることができる人は誰ですか?

答えて

0

ラベルの下に下線として機能する1行の境界線を追加する方法もあります。この回避策を使用すると、ラベル全体を強調する:

は、ラベルへの参照

@IBOutlet weak var myLabel: UILabel! 

は、ラベルの下

let labelSize = myLabel.frame.size 
    let border = CALayer() 
    let w = CGFloat(2.0) 

    border.borderColor = UIColor.yellow.cgColor // <--- Here the underline color 
    border.frame = CGRect(x: 0, y: labelSize.height - w, width: labelSize.width, height: labelSize.height) 
    border.borderWidth = w 
    myLabel.layer.addSublayer(border) 
    myLabel.layer.masksToBounds = true 

注意を境界線を追加取得します。あなたが部分的にテキストundlerlineこのソリューションはNSAttributedStringKey.underlineColor属性が何をしたいん

0

appropiateされていないする必要がある場合:テキストの色が黒のままになり、一方、

let underlineAttributes = [ 
    NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue, 
    NSAttributedStringKey.underlineColor: UIColor.orange 
] as [NSAttributedStringKey : Any] 
let underlineAttributedString = NSAttributedString(string: "Test", attributes: underlineAttributes) 

これは、オレンジ色の下線の色を設定します。

0

スウィフト4ソリューション

あなたは[NSAttributedStringKey:任意]などの属性の配列とNSAttributedStringを使用する必要があります。

サンプルコード:

インポートのUIKit

class ViewController: UIViewController { 

    @IBOutlet weak var myLabel: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Colored Underline Label 
     let labelString = "Underline Label" 
     let textColor: UIColor = .blue 
     let underLineColor: UIColor = .red 
     let underLineStyle = NSUnderlineStyle.styleSingle.rawValue 

     let labelAtributes:[NSAttributedStringKey : Any] = [ 
      NSAttributedStringKey.foregroundColor: textColor, 
      NSAttributedStringKey.underlineStyle: underLineStyle, 
      NSAttributedStringKey.underlineColor: underLineColor 
     ] 

     let underlineAttributedString = NSAttributedString(string: labelString, 
                  attributes: labelAtributes) 

     myLabel.attributedText = underlineAttributedString 
    } 

} 
関連する問題