advertising

Swift iOS development books, to help you get started developing www.ituring.com.cn/book/2413

The body of the

We usually set text to UILabel, we always set the property uilabel.text. This means that the text displayed is single, and the entire text can only have the same text effect. Another attribute, uilabel. attributedText, allows you to set different fonts and shadows in sections, such as the first few words as a shadow and the last few words as an underscore.

The following code I made some changes, so that can run on Swift 3.0, original code from makeapppie.com/2016/07/05/… .

Can run, see the effect:

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        self.window = UIWindow(frame: UIScreen.main.bounds)
        letpage = Page() page.view.backgroundColor = .white self.window! .rootViewController = page self.window? .makeKeyAndVisible()return true
    }
}
class Page: UIViewController {



    override func viewDidLoad() {
        super.viewDidLoad()

        let myLabel = UILabel()
        let myString = "P is for Pizza and Pizza is for me"view.backgroundColor = UIColor.white //initialize the label to be the entire view //and to word wrap myLabel.frame = View. The bounds. InsetBy (20), dy dx: 20: / / where frame. The size, width = 350.0 / / uncommentfor playgrounds
        myLabel.lineBreakMode = .byWordWrapping
        myLabel.numberOfLines = 0
//        myLabel.backgroundColor = UIColor.yellow
        myLabel.text = myString
        view.addSubview(myLabel)

        //: Initialize the mutable string
        let myMutableString = NSMutableAttributedString(
            string: myString,
            attributes: [NSFontAttributeName:
                UIFont(name: "Georgia"And the size: 18.0)! ] ) / /for first Pizza
        myMutableString.addAttribute(
            NSFontAttributeName,
            value: UIFont(
                name: "Chalkduster"And the size: 24.0)! , range: NSRange( location: 9, length: 5)) //: Make a big blue P myMutableString.addAttribute( NSFontAttributeName, value: UIFont( name:"AmericanTypewriter-Bold"And the size: 36.0)! , range: NSRange( location:0, length:1)) myMutableString.addAttribute( NSForegroundColorAttributeName, value: UIColor.blue, range: NSRange( location:0, length:1)) //: Make the second pizza red and outlinedin Helvetica Neue
        myMutableString.addAttribute(
            NSFontAttributeName,
            value: UIFont(
                name: "Helvetica Neue"And the size: 36.0)! , range: NSRange( location: 19, length: 5)) myMutableString.addAttribute( NSStrokeColorAttributeName, value: UIColor.red, range: NSRange( location: 19, length: 5)) myMutableString.addAttribute( NSStrokeWidthAttributeName, value: 4, range: NSRange( location: 19, length: 5)) //: Set the background color is attributes text. //:which is not the color of the background text.
        let  stringLength = myString.characters.count
        myMutableString.addAttribute(NSBackgroundColorAttributeName,
                                     value: UIColor.magenta,
                                     range: NSRange(
                                        location: 0,
                                        length: stringLength))



        //: Add a Drop Shadow

        //: Make the Drop Shadow
        let shadow = NSShadow()
        shadow.shadowOffset = CGSize(width: 5, height: 5)
        shadow.shadowBlurRadius = 5
        shadow.shadowColor = UIColor.gray

        //: Add a drop shadow to the text
        myMutableString.addAttribute(
            NSShadowAttributeName,
            value: shadow,
            range: NSRange(
                location: 27,
                length: 7))

        //:Change to 48 point Menlo
        myMutableString.addAttribute(
            NSFontAttributeName,
            value: UIFont(
                name: "Menlo"And the size: 48.0)! , range: NSRange( location: 27, length: 7)) //: Appending the String with !!! and an Attributed Stringlet myAddedStringAttributes:[String:AnyObject]? = [
            NSFontAttributeName:UIFont(
                name: "AvenirNext-Heavy"And the size: 48.0)! , NSForegroundColorAttributeName:UIColor.red, NSShadowAttributeName: shadow ]let myAddedString = NSAttributedString(
            string: "!!!",
            attributes: myAddedStringAttributes)
        myMutableString.append(myAddedString)

        //: Apply to the label
        myLabel.attributedText = myMutableString

    }

}Copy the code

Quite expressive.