2010-05-17 19 views
41

असल में मुझे UILabel पसंद है। वे मीठे हैं। अब मुझे UITextView पर जाना पड़ा क्योंकि UILabel शीर्ष पर लंबवत पाठ को संरेखित नहीं कर रहा है। अरे नहीं। एक चीज जिसकी मुझे ज़रूरत है वह एक पाठ छाया है। UILabel में यह है। UITextView ऐसा नहीं लगता है। लेकिन मुझे लगता है कि लड़का सिर्फ उसी अंतर्निहित UIKitNSString जोड़ों का उपयोग करता है ?? हो सकता है कि किसी के पास पहले से ही उस समस्या का समाधान हो? मैं क्या लिखूंगा?UITextView पर टेक्स्ट छाया कैसे लागू करें?

+2

के लिए अपडेट किया गया मुझे लगता है कि आप adjwilli जवाब को स्वीकार करना चाहिए, क्योंकि यह आसान चींटी है मुझे लगता है कि सबसे सहमत होंगे कि यह यह करने के लिए सही तरीका है। – Lukas

उत्तर

152
text.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
text.layer.shadowOffset = CGSizeMake(1.0f, 1.0f); 
text.layer.shadowOpacity = 1.0f; 
text.layer.shadowRadius = 1.0f; 

और भूल जाते हैं शीर्ष को जोड़ने के लिए नहीं है:

#import <QuartzCore/QuartzCore.h> 
+1

सही उत्तर चिह्नित उत्तर मुझे लगता है कि यह गन्दा और आम तौर पर खराब रूप है। – averydev

+2

मेरी इच्छा है कि मैं इसे आगे चिह्नित कर सकूं - यह UITextView में छाया जोड़ने का उचित तरीका है। दूसरी विधि बहुत भारी है और यह बिल्कुल नहीं है कि UITextView के बहुत छोटे उपयोगों के अलावा किसी अन्य चीज़ के लिए किया जाना चाहिए। –

+0

अजीब व्यवहार: यह दृष्टिकोण टेक्स्ट को छाया में जोड़ता है, न कि uitextview बॉक्स में। मैं बॉक्स में छाया को छाया में नहीं जोड़ना चाहता हूं। –

14

साथ जवाब text.layer.shadowColor

पूरे TextView के लिए छाया कहते हैं, शायद यह काम करता है, लेकिन यह न केवल पाठ के लिए छाया जोड़ता है।

सही जवाब है:

CALayer *textLayer = ((CALayer *)[textView.layer.sublayers objectAtIndex:0]); 
textLayer.shadowColor = [UIColor whiteColor].CGColor; 
textLayer.shadowOffset = CGSizeMake(0.0f, 1.0f); 
textLayer.shadowOpacity = 1.0f; 
textLayer.shadowRadius = 1.0f; 
+0

काम करने के लिए यह सही तरीका है संपादन योग्य टेक्स्टव्यू – zszen

0

यह प्रयोग कर रहे हैं आईओएस के संस्करण पर निर्भर करता है। आईओएस 6 के साथ शुरुआत एक ही छाया समर्थित है, आप इसे एनएसएट्रिब्रिटेड स्ट्रिंग पर एक विशेषता के रूप में सेट करते हैं जिसे आपने लेबल पर सेट किया था।

8

आईओएस में 6 + उपयोग के लिए जिम्मेदार ठहराया पाठ

NSShadow * shadow = [[NSShadow alloc] init]; 
shadow.shadowColor = [UIColor blackColor]; 
shadow.shadowOffset = CGSizeMake(2, 2); 

NSDictionary * textAttributes = 
@{ NSForegroundColorAttributeName : [UIColor blueColor], 
    NSShadowAttributeName   : shadow, 
    NSFontAttributeName   : [UIFont boldSystemFontOfSize:20] }; 

textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello" 
                  attributes:textAttributes]; 

Hello example

+0

दुख की बात यह है कि इसे संपादित नहीं किया जा सकता – zszen

9

स्विफ्ट 3


let textView = UITextView(frame: view.frame) 
textView.font = UIFont(name: "Helvetica", size: 64.0) 
textView.textColor = .red 
textView.text = "Hello World" 

textView.layer.shadowColor = UIColor.black.cgColor 
textView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0) 
textView.layer.shadowOpacity = 1.0 
textView.layer.shadowRadius = 2.0 
textView.layer.backgroundColor = UIColor.clear.cgColor 

स्विफ्ट 2,3


let textView = UITextView(frame: view.frame) 
textView.font = UIFont(name: "Helvetica", size: 64.0) 
textView.textColor = UIColor.redColor() 
textView.text = "Hello World"  

textView.layer.shadowColor = UIColor.blackColor().CGColor 
textView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0) 
textView.layer.shadowOpacity = 1.0 
textView.layer.shadowRadius = 2.0 
textView.layer.backgroundColor = UIColor.clearColor().CGColor 
9

enter image description here

यह स्विफ्ट उदाहरण पाठ में छाया जोड़ने के लिए जिम्मेदार ठहराया स्ट्रिंग विधि का उपयोग करता। स्विफ्ट में जिम्मेदार तारों के बारे में अधिक जानकारी के लिए this answer देखें। यह विधि (layer method का उपयोग करने के विरोध में) यदि आप चाहें तो छाया की एक श्रृंखला पर छाया सेट करने के लिए लचीलापन देता है।

// 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 = [ NSAttributedStringKey.shadow: myShadow ] 

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

// set the attributed text on a label 
myLabel.attributedText = myAttrString // can also use with UITextView 

स्विफ्ट 4

+0

यह मेरे लिए बहुत अच्छा काम करता है। धन्यवाद। – adev

संबंधित मुद्दे