Swift iOS : attributedText 富文本操做

廣告

Swift iOS開發小書 ,幫你快速上手開發 www.ituring.com.cn/book/2413swift

正文

一般給UILabel設置文本,咱們都是設置屬性UILabel.text。這意味着顯示的文本是單一的,整個文本只能有一種一樣的文本效果。而另一個屬性UILabel.attributedText,就能夠能夠分段設置的不一樣的字體、陰影效果等,好比前幾個字爲一個陰影效果,後幾個字使用下劃線效果。bash

以下代碼我作了些改變,以便在Swift 3.0上能夠運行,原本的代碼來自 makeapppie.com/2016/07/05/…app

能夠運行起來,查看效果:ide

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)
        let page = 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.bounds.insetBy(dx: 20, dy: 20)
        //mylabel.frame.size.width = 350.0//uncomment for 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", size: 18.0)!])

        // for first Pizza
        myMutableString.addAttribute(
            NSFontAttributeName,
            value: UIFont(
                name: "Chalkduster",
                size: 24.0)!,
            range: NSRange(
                location: 9,
                length: 5))

        //: Make a big blue P
        myMutableString.addAttribute(
            NSFontAttributeName,
            value: UIFont(
                name: "AmericanTypewriter-Bold",
                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 outlined in Helvetica Neue
        myMutableString.addAttribute(
            NSFontAttributeName,
            value: UIFont(
                name: "Helvetica Neue",
                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",
                size: 48.0)!,
            range: NSRange(
                location: 27,
                length: 7))

        //: Appending the String with !!! and an Attributed String
        let myAddedStringAttributes:[String:AnyObject]? = [
            NSFontAttributeName:UIFont(
                name: "AvenirNext-Heavy",
                size: 48.0)!,
            NSForegroundColorAttributeName:UIColor.red,
            NSShadowAttributeName: shadow

        ]
        let myAddedString = NSAttributedString(
            string: "!!!",
            attributes: myAddedStringAttributes)
        myMutableString.append(myAddedString)

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

    }

}複製代碼

至關的具備表現力。字體

相關文章
相關標籤/搜索