2016-01-31 22 views
8

UILabel'ımdaki bir gölgenin yayılmasını artırmak istiyorum ancak bunu yapmak için özellik bulamıyorum. Sorunu göstermek için aşağıdan bir fotoğraf ekledim.iOS - Shadow yayıldı UILabel

An example of the desired label shadow, and the current one.

üst Etiket ben Photoshop oluşturmak mümkün duyuyorum istenen etkidir. En alttaki Etiket, iOS'ta bulabildiğim özellikleri gösterir. İşte alt etiket için kullandığım kod.

let bottomLabel = UILabel(frame: CGRectMake(0, 0, maxWidth, 18)) 
bottomLabel.backgroundColor = UIColor.clearColor() 
bottomLabel.textColor = UIColor.whiteColor() 
bottomLabel.font = UIFont.boldSystemFontOfSize(16) 
bottomLabel.text = title 
bottomLabel.textAlignment = .Center 
bottomLabel.numberOfLines = 1 
bottomLabel.layer.shadowOffset = CGSize(width: 0, height: 0) 
bottomLabel.layer.shadowOpacity = 1 
bottomLabel.layer.shadowRadius = 2 

İkinci bir etiketi gölge olarak kullanmak için bir öneri buldum, ancak bu istenen sonucu vermedi. İşte o etiketin kodu.

let bottomLabelShadow = UILabel(frame: CGRectMake(0, 1, maxWidth, 18)) 
bottomLabelShadow.backgroundColor = UIColor.clearColor() 
bottomLabelShadow.textColor = UIColor.blackColor() 
bottomLabelShadow.font = UIFont.boldSystemFontOfSize(16) 
bottomLabelShadow.text = title 
bottomLabelShadow.textAlignment = .Center 
bottomLabelShadow.numberOfLines = 1 
bottomLabelShadow.layer.shadowOffset = CGSize(width: 0, height: 0) 
bottomLabelShadow.layer.shadowOpacity = 1 
bottomLabelShadow.layer.shadowRadius = 2 
+0

Aslında https://makeapppie.com/2015/05/19/swift-swift-how-to-make-a-drop-shadow-in-user-interfaces/ gölgeler hakkında iyi makale olduğunda –

cevap

22

Benim için bir oyun alanında çalışıyor gibi görünüyor. İstediğiniz gibi görünmesi için gölge yarıçapını artırmanız yeterlidir.

Bu tam oyun alanı kod ben

let view = UIView(frame: CGRectMake(0,0,100,20)) 
view.backgroundColor = UIColor.yellowColor() 

let bottomLabel = UILabel(frame: CGRectMake(0, 0, 100, 20)) 
bottomLabel.backgroundColor = UIColor.clearColor() 
bottomLabel.textColor = UIColor.whiteColor() 
bottomLabel.font = UIFont.boldSystemFontOfSize(18) 
bottomLabel.text = "Testing" 
bottomLabel.textAlignment = .Center 
bottomLabel.numberOfLines = 1 
bottomLabel.layer.shadowOffset = CGSize(width: 0, height: 0) 
bottomLabel.layer.shadowOpacity = 1 
bottomLabel.layer.shadowRadius = 6 

view.addSubview(bottomLabel) 

kullanılan Veya bir bulanıklık görünümü arka üzerine kullanıyorsanız, daha iyi görünümlü bir etki elde etmek için canlılık kullanabilirsiniz olduğunu. Swift 3 olarak

enter image description here

+0

darnit, beni dövüyorsun. LOL. uzun süre kullanmadıktan sonra oyun alanlarını nasıl kullanacağınızı anlamaya çalışıyordu ... – DeveloperACE

+0

Hızlı yanıt için teşekkürler! Ona baktım ama aradığım sonuç değil. Resmi, Playground örneğinize daha iyi uyacak şekilde güncelledim. –

+0

Bunun için teşekkürler! – CaffeineShots

1

Rikola cevabı:

import UIKit 

let view = UIView(frame: CGRect.init(x: 0, y: 0, width: 100, height: 20)) 
view.backgroundColor = UIColor.yellow 

let bottomLabel = UILabel(frame: CGRect.init(x: 0, y: 0, width: 100, height: 20)) 
bottomLabel.backgroundColor = UIColor.clear 
bottomLabel.textColor = UIColor.white 
bottomLabel.font = UIFont.boldSystemFont(ofSize: 18) 
bottomLabel.text = "Testing" 
bottomLabel.textAlignment = .center 
bottomLabel.numberOfLines = 1 
bottomLabel.layer.shadowOffset = CGSize(width: 0, height: 0) 
bottomLabel.layer.shadowOpacity = 1 
bottomLabel.layer.shadowRadius = 6 

view.addSubview(bottomLabel) 

basın biraz "göz" etiketinin görsel anlatım için, sağ barda view.addSubview(bottomLabel) baskı üzerinde elektrik süpürgesi.

4
//Try This! Hope this helps!! 

// Create a string 
let myString = "Shadow" 

// Create a shadow 
let myShadow = NSShadow() 
myShadow.shadowBlurRadius = 3 
myShadow.shadowOffset = CGSize(width: 3, height: 3) 
myShadow.shadowColor = UIColor.gray 

// Create an attribute from the shadow 
let myAttribute = [ NSShadowAttributeName: myShadow ] 

// Add the attribute to the string 
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute) 

// set the attributed text on a label 
myLabel.attributedText = myAttrString