2017-10-31 5 views
0

私はイニシャルインディーズの灰色の円を作成しようとしています。 tabBarItemイメージとして使用します。iOS swift:TabBarItemのラベルからの画像

これは私が円を作成するために使用する関数であり、それはほとんど動作しますが、あなたは下の画像で見ることができるよう、それが正方形でないサークルです:

static func createAvatarWithInitials(name: String, surname: String, size: CGFloat) -> UIImage { 
     let initialsLabel = UILabel() 
     initialsLabel.frame.size = CGSize(width: size, height: size) 
     initialsLabel.textColor = UIColor.white 
     initialsLabel.text = "\(name.characters.first!)\(surname.characters.first!)" 
     initialsLabel.textAlignment = .center 
     initialsLabel.backgroundColor = UIColor(red: 74.0/255, green: 77.0/255, blue: 78.0/255, alpha: 1.0) 
     initialsLabel.layer.cornerRadius = size/2 

     let scale = UIScreen.main.scale 
     UIGraphicsBeginImageContextWithOptions(initialsLabel.frame.size, false, scale) 

     initialsLabel.layer.render(in: UIGraphicsGetCurrentContext()!) 
     let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 
     image.withRenderingMode(.alwaysOriginal) 
     return image 
} 

とどのように私これは

if index == positionForProfileItem { 
        tabBarItem.image = UIImage.createAvatarWithInitials(name: "John", surname: "Doe", size: CGFloat(20)) 
} 

が、私は..タブバーで空の二乗長方形を持って、私は、任意のヘルプを理由にそれを把握することはできません。tabBarControllerののviewDidLoad内で呼び出しますか?

enter image description hereenter image description here

+0

'createAvatarWithInitials'は正しい画像を返しますか? – trungduc

+0

私はuiimageviewでそれを使用すると、最初の質問で見られるようにイメージを返します。私はabバーでそれを使用する場合、私は2番目の画像の結果が –

答えて

1

UIImage withRenderingMode方法は、新しいイメージを返します。送信者は変更されません。

変更ただ一つの最後の2行:

return image.withRenderingMode(.alwaysOriginal) 

しかし、それはあなたがしたいものではありません。あなたが途中でテキストの円のテンプレート画像をしたいので、あなたが次のことを実行する必要があります。

static func createAvatarWithInitials(name: String, surname: String, size: CGFloat) -> UIImage { 
    UIGraphicsBeginImageContextWithOptions(CGSize(width: size, height: size), false, 0) 
    let ctx = UIGraphicsGetCurrentContext()! 

    // Draw an opague circle 
    UIColor.white.setFill() 
    let circle = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: size, height: size)) 
    circle.fill() 

    // Setup text 
    let font = UIFont.systemFont(ofSize: 17) 
    let text = "\(name.characters.first!)\(surname.characters.first!)" 
    let textSize = (text as NSString).size(withAttributes: [ .font: font ]) 

    // Erase text 
    ctx.setBlendMode(.clear) 
    let textRect = CGRect(x: (size - textSize.width)/2, y: (size - textSize.height)/2, width: textSize.width, height: textSize.height) 
    (text as NSString).draw(in: textRect, withAttributes: [ .font: font ]) 

    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 
    UIGraphicsEndImageContext() 

    return image 
} 
+0

ありがとう、今、それが表示されますが、私はasTemplateを持っていると私は何かを見ることができません。提案?それは円形ではない、提案ですか? :D –

+2

タブバー項目のテンプレート画像を作成する場合、色は関係ありません。アルファ値のみが重要です。全体のイメージを不透明にするので、それは灰色の四角形として出てきます。 – rmaddy

+0

okですが、背景色を削除すると、文字は表示されますが、円は表示されません。私はサークルから手紙を切るべきだと思う。なにか提案を? –

1

あなたはcornerRadiusは、画面の撮影にEFECTを持っているため、あなたのラベルにYES = clipsToBoundsを追加する必要があり、また、画像のサイズを20から30に少し大きめにしてみてください。

+0

ありがとう、どのようにテンプレートとしてそれを使用する上で任意の提案? –