2016-01-31 13 views
8

पर फैली हुई है, मैं अपने UILabel में एक छाया का प्रसार बढ़ाना चाहता हूं लेकिन मुझे ऐसा करने के लिए संपत्ति नहीं मिल रही है। मैंने इस मुद्दे को स्पष्ट करने के लिए नीचे एक तस्वीर जोड़ा है।आईओएस - छाया UILabel

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

शीर्ष लेबल वांछित प्रभाव मैं में फ़ोटोशॉप बनाने में सक्षम हूँ। नीचे लेबल उन गुणों को दिखाता है जिन्हें मैं आईओएस में ढूंढने में सक्षम था। यहां कोड है जिसे मैंने नीचे लेबल के लिए उपयोग किया था।

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 

मुझे छाया के रूप में दूसरे लेबल का उपयोग करने के लिए एक सुझाव मिला लेकिन यह वांछित परिणाम नहीं दिया। उस लेबल के लिए कोड यहां दिया गया है।

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

असल में वहाँ छाया के बारे में एक अच्छा लेख है https://makeapppie.com/2015/05/19/swift-swift-how-to-make-a-drop-shadow-in-user-interfaces/ –

उत्तर

22

ऐसा लगता है कि यह मेरे लिए एक खेल के मैदान में काम करता है। जैसा कि आप चाहते हैं उसे दिखाने के लिए आपको छाया त्रिज्या को बढ़ाने की जरूरत है।

यह सही खेल का मैदान कोड मैं

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) 

इस्तेमाल किया या यदि आप इसे एक कलंक दृश्य पृष्ठभूमि के ऊपर का उपयोग कर रहे हैं, तो आप जीवंतता इस्तेमाल कर सकते हैं एक बेहतर की तलाश में प्रभाव को प्राप्त करने के लिए है। स्विफ्ट 3 के रूप में

enter image description here

+0

डर्निट, आपने मुझे इसे हराया। जबरदस्त हंसी। इतने लंबे समय तक इसका उपयोग न करने के बाद खेल के मैदानों का उपयोग करने का तरीका जानने का प्रयास कर रहा था ... – DeveloperACE

+0

तेज प्रतिक्रिया के लिए धन्यवाद! मैंने इसमें देखा लेकिन यह नतीजा नहीं है जिसे मैं ढूंढ रहा हूं। मैंने आपके Playground उदाहरण से बेहतर मिलान करने के लिए छवि को अपडेट किया है। –

+0

इसके लिए धन्यवाद! – CaffeineShots

1

Rikola का जवाब:

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) 

प्रेस थोड़ा "नेत्र" जब सही पट्टी में view.addSubview(bottomLabel) प्रिंट से अधिक hoovering, लेबल का एक दृश्य प्रतिनिधित्व के लिए।

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