通常给UILabel设置文本,我们都是设置属性UILabel.text。这意味着显示的文本是单一的,整个文本只能有一种同样的文本效果。而另外一个属性UILabel.attributedText,就可以可以分段设置的不同的字体、阴影效果等,比如前几个字为一个阴影效果,后几个字使用下划线效果。
如下代码我做了些改变,以便在Swift 3.0上可以运行,本来的代码来自 https://makeapppie.com/2016/07/05/using-attributed-strings-in-swift-3-0/ ,
可以运行起来,查看效果:
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@H_403_10@ page = Page()
page.view.backgroundColor = .white
self.window!.rootViewController = page
self.window?.makeKeyAndVisible()
return@H_403_10@ true@H_403_10@
}
}
class Page: UIViewController {
override func viewDidLoad@H_403_10@@H_403_10@() {
super.viewDidLoad()
let@H_403_10@ myLabel = UILabel()
let@H_403_10@ myString = "P is for Pizza and Pizza is for me"@H_403_10@
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@H_403_10@ playgrounds
myLabel.lineBreakMode = .byWordWrapping
myLabel.numberOfLines = 0
// myLabel.backgroundColor = UIColor.yellow
myLabel.text = myString
view.addSubview(myLabel)
//: Initialize the mutable string
let@H_403_10@ myMutableString = NSMutableAttributedString(
string: myString,attributes: [NSFontAttributeName:
UIFont(name: "Georgia"@H_403_10@,size: 18.0)!])
// for@H_403_10@ first Pizza
myMutableString.addAttribute(
NSFontAttributeName,value: UIFont(
name: "Chalkduster"@H_403_10@,size: 24.0)!,range: NSRange(
location: 9,length: 5))
//: Make a big blue P
myMutableString.addAttribute(
NSFontAttributeName,value: UIFont(
name: "AmericanTypewriter-Bold"@H_403_10@,size: 36.0)!,range: NSRange(
location:0,length:1))
myMutableString.addAttribute(
NSForegroundColorAttributeName,value: UIColor.blue,length:1))
//: Make the second pizza red and outlined in@H_403_10@ Helvetica Neue
myMutableString.addAttribute(
NSFontAttributeName,value: UIFont(
name: "Helvetica Neue"@H_403_10@,range: NSRange(
location: 19,length: 5))
myMutableString.addAttribute(
NSStrokeColorAttributeName,value: UIColor.red,range: NSRange(
location: 19,length: 5))
myMutableString.addAttribute(
NSStrokeWidthAttributeName,value: 4,length: 5))
//: Set the background color is attributes text.
//: which@H_403_10@ is not the color of the background text.
let@H_403_10@ 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@H_403_10@ 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"@H_403_10@,size: 48.0)!,length: 7))
//: Appending the String with !!! and an Attributed String
let@H_403_10@ myAddedStringAttributes:[String:AnyObject]? = [
NSFontAttributeName:UIFont(
name: "AvenirNext-Heavy"@H_403_10@,NSForegroundColorAttributeName:UIColor.red,NSShadowAttributeName: shadow
]
let@H_403_10@ myAddedString = NSAttributedString(
string: "!!!"@H_403_10@,attributes: myAddedStringAttributes)
myMutableString.append(myAddedString)
//: Apply to the label
myLabel.attributedText = myMutableString
}
}
相当的具有表现力。