इस सवाल का जवाब पहले ही दिया जा चुका है ... लेकिन मैं यह दिखाना चाहता था कि छाया को कैसे जोड़ा जाए और साथ ही NSAttributedString के साथ फ़ॉन्ट को बदल दिया जाए, ताकि जब लोग इस विषय की खोज करें तो उन्हें ढूंढते रहना न पड़े।
#define FONT_SIZE 20
#define FONT_HELVETICA @"Helvetica-Light"
#define BLACK_SHADOW [UIColor colorWithRed:40.0f/255.0f green:40.0f/255.0f blue:40.0f/255.0f alpha:0.4f]
NSString*myNSString = @"This is my string.\nIt goes to a second line.";
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;
paragraphStyle.lineSpacing = FONT_SIZE/2;
UIFont * labelFont = [UIFont fontWithName:FONT_HELVETICA size:FONT_SIZE];
UIColor * labelColor = [UIColor colorWithWhite:1 alpha:1];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor : BLACK_SHADOW];
[shadow setShadowOffset : CGSizeMake (1.0, 1.0)];
[shadow setShadowBlurRadius : 1];
NSAttributedString *labelText = [[NSAttributedString alloc] initWithString : myNSString
attributes : @{
NSParagraphStyleAttributeName : paragraphStyle,
NSKernAttributeName : @2.0,
NSFontAttributeName : labelFont,
NSForegroundColorAttributeName : labelColor,
NSShadowAttributeName : shadow }];
यहाँ एक स्विफ्ट संस्करण है ...
चेतावनी! यह 4 के लिए काम करता है।
5s के लिए आपको फ्लोट के सभी मानों को डबल वैल्यू में बदलना होगा (क्योंकि कंपाइलर अभी तक सही तरीके से काम नहीं कर रहा है)
फ़ॉन्ट विकल्प के लिए स्विफ्ट एनम:
enum FontValue: Int {
case FVBold = 1 , FVCondensedBlack, FVMedium, FVHelveticaNeue, FVLight, FVCondensedBold, FVLightItalic, FVUltraLightItalic, FVUltraLight, FVBoldItalic, FVItalic
}
एनम पहुंच के लिए स्विफ्ट सरणी (क्योंकि एनम का उपयोग नहीं कर सकते हैं '-'):
func helveticaFont (index:Int) -> (String) {
let fontArray = [
"HelveticaNeue-Bold",
"HelveticaNeue-CondensedBlack",
"HelveticaNeue-Medium",
"HelveticaNeue",
"HelveticaNeue-Light",
"HelveticaNeue-CondensedBold",
"HelveticaNeue-LightItalic",
"HelveticaNeue-UltraLightItalic",
"HelveticaNeue-UltraLight",
"HelveticaNeue-BoldItalic",
"HelveticaNeue-Italic",
]
return fontArray[index]
}
स्विफ्ट जिम्मेदार पाठ फ़ंक्शन:
func myAttributedText (myString:String, mySize: Float, myFont:FontValue) -> (NSMutableAttributedString) {
let shadow = NSShadow()
shadow.shadowColor = UIColor.textShadowColor()
shadow.shadowOffset = CGSizeMake (1.0, 1.0)
shadow.shadowBlurRadius = 1
let paragraphStyle = NSMutableParagraphStyle.alloc()
paragraphStyle.lineHeightMultiple = 1
paragraphStyle.lineBreakMode = NSLineBreakMode.ByWordWrapping
paragraphStyle.alignment = NSTextAlignment.Center
let labelFont = UIFont(name: helveticaFont(myFont.toRaw()), size: mySize)
let labelColor = UIColor.whiteColor()
let myAttributes :Dictionary = [NSParagraphStyleAttributeName : paragraphStyle,
NSKernAttributeName : 3, // (-1,5)
NSFontAttributeName : labelFont,
NSForegroundColorAttributeName : labelColor,
NSShadowAttributeName : shadow]
let myAttributedString = NSMutableAttributedString (string: myString, attributes:myAttributes)
// add new color
let secondColor = UIColor.blackColor()
let stringArray = myString.componentsSeparatedByString(" ")
let firstString: String? = stringArray.first
let letterCount = countElements(firstString!)
if firstString {
myAttributedString.addAttributes([NSForegroundColorAttributeName:secondColor], range:NSMakeRange(0,letterCount))
}
return myAttributedString
}
स्ट्रिंग एरे में रेंज खोजने के लिए उपयोग किया गया पहला और अंतिम एक्सटेंशन:
extension Array {
var last: T? {
if self.isEmpty {
NSLog("array crash error - please fix")
return self [0]
} else {
return self[self.endIndex - 1]
}
}
}
extension Array {
var first: T? {
if self.isEmpty {
NSLog("array crash error - please fix")
return self [0]
} else {
return self [0]
}
}
}
नए रंग:
extension UIColor {
class func shadowColor() -> UIColor {
return UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.3)
}
class func textShadowColor() -> UIColor {
return UIColor(red: 50.0/255.0, green: 50.0/255.0, blue: 50.0/255.0, alpha: 0.5)
}
class func pastelBlueColor() -> UIColor {
return UIColor(red: 176.0/255.0, green: 186.0/255.0, blue: 255.0/255.0, alpha: 1)
}
class func pastelYellowColor() -> UIColor {
return UIColor(red: 255.0/255.0, green: 238.0/255.0, blue: 140.0/255.0, alpha: 1)
}
}
मेरा मैक्रो प्रतिस्थापन:
enum MyConstants: Float {
case CornerRadius = 5.0
}
मेरा बटन बनाने वाला w / जिम्मेदार पाठ:
func myButtonMaker (myView:UIView) -> UIButton {
let myButton = UIButton.buttonWithType(.System) as UIButton
myButton.backgroundColor = UIColor.pastelBlueColor()
myButton.showsTouchWhenHighlighted = true;
let myCGSize:CGSize = CGSizeMake(100.0, 50.0)
let myFrame = CGRectMake(myView.frame.midX - myCGSize.height,myView.frame.midY - 2 * myCGSize.height,myCGSize.width,myCGSize.height)
myButton.frame = myFrame
let myTitle = myAttributedText("Button",20.0,FontValue.FVLight)
myButton.setAttributedTitle(myTitle, forState:.Normal)
myButton.layer.cornerRadius = myButton.bounds.size.width / MyConstants.CornerRadius.toRaw()
myButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
myButton.tag = 100
myButton.bringSubviewToFront(myView)
myButton.layerGradient()
myView.addSubview(myButton)
return myButton
}
मेरा यूआईवीवाई / यूआईबेल मेकर डब्ल्यू / एट्रिब्यूटेड टेक्स्ट, शैडो और राउंड कॉर्नर:
func myLabelMaker (myView:UIView) -> UIView {
let myFrame = CGRectMake(myView.frame.midX / 2 , myView.frame.midY / 2, myView.frame.width/2, myView.frame.height/2)
let mylabelFrame = CGRectMake(0, 0, myView.frame.width/2, myView.frame.height/2)
let myBaseView = UIView()
myBaseView.frame = myFrame
myBaseView.backgroundColor = UIColor.clearColor()
let myLabel = UILabel()
myLabel.backgroundColor=UIColor.pastelYellowColor()
myLabel.frame = mylabelFrame
myLabel.attributedText = myAttributedText("This is my String",20.0,FontValue.FVLight)
myLabel.numberOfLines = 5
myLabel.tag = 100
myLabel.layer.cornerRadius = myLabel.bounds.size.width / MyConstants.CornerRadius.toRaw()
myLabel.clipsToBounds = true
myLabel.layerborders()
myBaseView.addSubview(myLabel)
myBaseView.layerShadow()
myBaseView.layerGradient()
myView.addSubview(myBaseView)
return myLabel
}
सामान्य छाया जोड़ें:
func viewshadow<T where T: UIView> (shadowObject: T)
{
let layer = shadowObject.layer
let radius = shadowObject.frame.size.width / MyConstants.CornerRadius.toRaw();
layer.borderColor = UIColor.whiteColor().CGColor
layer.borderWidth = 0.8
layer.cornerRadius = radius
layer.shadowOpacity = 1
layer.shadowRadius = 3
layer.shadowOffset = CGSizeMake(2.0,2.0)
layer.shadowColor = UIColor.shadowColor().CGColor
}
दृश्य शैली के लिए एक्सटेंशन देखें:
extension UIView {
func layerborders() {
let layer = self.layer
let frame = self.frame
let myColor = self.backgroundColor
layer.borderColor = myColor.CGColor
layer.borderWidth = 10.8
layer.cornerRadius = layer.borderWidth / MyConstants.CornerRadius.toRaw()
}
func layerShadow() {
let layer = self.layer
let frame = self.frame
layer.cornerRadius = layer.borderWidth / MyConstants.CornerRadius.toRaw()
layer.shadowOpacity = 1
layer.shadowRadius = 3
layer.shadowOffset = CGSizeMake(2.0,2.0)
layer.shadowColor = UIColor.shadowColor().CGColor
}
func layerGradient() {
let layer = CAGradientLayer()
let size = self.frame.size
layer.frame.size = size
layer.frame.origin = CGPointMake(0.0,0.0)
layer.cornerRadius = layer.bounds.size.width / MyConstants.CornerRadius.toRaw();
var color0 = CGColorCreateGenericRGB(250.0/255, 250.0/255, 250.0/255, 0.5)
var color1 = CGColorCreateGenericRGB(200.0/255, 200.0/255, 200.0/255, 0.1)
var color2 = CGColorCreateGenericRGB(150.0/255, 150.0/255, 150.0/255, 0.1)
var color3 = CGColorCreateGenericRGB(100.0/255, 100.0/255, 100.0/255, 0.1)
var color4 = CGColorCreateGenericRGB(50.0/255, 50.0/255, 50.0/255, 0.1)
var color5 = CGColorCreateGenericRGB(0.0/255, 0.0/255, 0.0/255, 0.1)
var color6 = CGColorCreateGenericRGB(150.0/255, 150.0/255, 150.0/255, 0.1)
layer.colors = [color0,color1,color2,color3,color4,color5,color6]
self.layer.insertSublayer(layer, atIndex: 2)
}
}
वास्तविक दृश्य ने लोड कार्य किया:
func buttonPress (sender:UIButton!) {
NSLog("%@", "ButtonPressed")
}
override func viewDidLoad() {
super.viewDidLoad()
let myLabel = myLabelMaker(myView)
let myButton = myButtonMaker(myView)
myButton.addTarget(self, action: "buttonPress:", forControlEvents:UIControlEvents.TouchUpInside)
viewshadow(myButton)
viewshadow(myLabel)
}