2017-08-23 7 views
1

私はUICollectionViewを持っています。ここでは、セルにUILabelsが含まれていて、UIImageであるbackgroundViewがあります。ラベルの背景ははっきりしています。私の問題は、テキストの色とテキストが現れるイメージの領域内の色との間のコントラストがほとんどない場合、テキストのセグメントが読みにくくなることです。私は、これを助けるために使用できるテクニックがあると考えています(白いテキストに黒い境界線が付いていますか?誰かが私に助言してくれますか?イメージにラベルを貼り付けて、テキストを常に読み取り可能にする方法

まず編集:

私は@EssamMohamed/@AbdelahadDarwish答えをしようとしたが、それはうまくいきませんでした。私は何か間違っているかもしれない。

私は.xibファイルとカスタムクラスを使用しています。ここではカスタムクラスは次のとおりです。

class PointOfInterestCell: UICollectionViewCell { 

    @IBOutlet weak var nameLabel: UILabel! { 
     didSet { 
      //nameLabel.textColor = UIColor.tohTerracotaColor() 
     } 
    } 

    @IBOutlet weak var distanceLabel: UILabel! { 
     didSet { 
      let strokeTextAttributes = [ 
       NSStrokeColorAttributeName : UIColor.black, 
       NSForegroundColorAttributeName : UIColor.white, 
       NSStrokeWidthAttributeName : -1.0, 
      ] as [String : Any] 

      let text = distanceLabel.text ?? "????" 

      distanceLabel.attributedText = NSAttributedString(string: text, attributes: strokeTextAttributes) 
     } 
    } 
} 
+0

この回答を確認して色を取得するには、https://stackoverflow.com/a/29266983を参照してください。 – adev

+0

またはこの回答は平均色https://stackoverflow.com/a/13695592を与え、ラベルテキストの逆色を見つける。 – adev

+0

こんにちは、色1と色2が対照的な色である[色2]境界で[色1]のテキストを使用することをお勧めします。これらの色はカラーホイールから選択できます。 –

答えて

0

ラベル

import UIKit 

@IBDesignable 
    class StrockLabel: UILabel { 


     override init(frame: CGRect) { 
      super.init(frame: CGRect.zero) 
     } 

     required init?(coder aDecoder: NSCoder) { 
      super.init(coder: aDecoder) 
     } 

     override var text: String?{ 

      didSet{ 

       let strokeTextAttributes = [ 
        NSStrokeColorAttributeName : UIColor.black, 
        NSForegroundColorAttributeName : UIColor.white, 
        NSStrokeWidthAttributeName : -1.0, 
        ] as [String : Any] 

       let textStr = text ?? "????" 
       self.attributedText = NSAttributedString(string: textStr, attributes: strokeTextAttributes) 
      } 


     } 
     /* 
     // Only override draw() if you perform custom drawing. 
     // An empty implementation adversely affects performance during animation. 
     override func draw(_ rect: CGRect) { 
     // Drawing code 
     } 
     */ 

    } 

/////////

//および使用

@IBOutlet weak var distanceLabel: StrockLabel! 

distanceLabel.text = "DDDD" 
+0

私は自分自身と仕事をテストしました、私は確信しています –

+0

ありがとうございます。私は間違ったことをする必要があります。私の質問に追加した編集を見てください。 – Verticon

+0

@Verticonもう一度私の答えを確認してください –

2
のattributedTextを使用することができます

私は[色1]のテキストを[色2]のストロークで使うと思います。ここで、色1と色2は対照的な色です(相補的な配色) 2つの色が互いに向き合うカラーホイールからこれらの色を選択することができる。

enter image description here

あなたはストロークを作成する方法について検索した場合、ヘルプはこのlinkを確認してください。

関連する問題