Swift iOS : attributedText 富文本操作
来源:互联网 发布:淘宝店铺被关闭直通车 编辑:程序博客网 时间:2024/06/05 04:53
通常给UILabel设置文本,我们都是设置属性UILabel.text。这意味着显示的文本是单一的,整个文本只能有一种同样的文本效果。而另外一个属性UILabel.attributedText,就可以可以分段设置的不同的字体、阴影效果等,比如前几个字为一个阴影效果,后几个字使用下划线效果。
如下代码我做了些改变,以便在Swift 3.0上可以运行,本来的代码来自 https://makeapppie.com/2016/07/05/using-attributed-strings-in-swift-3-0/ ,
可以运行起来,查看效果:
import UIKit@UIApplicationMainclass 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 }}
相当的具有表现力。
阅读全文
0 0
- Swift iOS : attributedText 富文本操作
- iOS开发之富文本(oc与swift两种都有)
- Swift 3.0 label富文本
- Swift HTML富文本显示
- Swift 富文本属性设置
- Ueditor 富文本操作
- ios 富文本编辑器
- iOS之富文本
- iOS之富文本
- ios之富文本
- iOS之富文本
- iOS之富文本
- iOS 富文本 AttributedString
- IOS-富文本
- iOS之富文本
- ios 富文本
- iOS AttributeString(富文本)
- iOS之富文本
- 数组中的list和Arraylist的区别
- 工作日志
- select框的进化
- COLORREF转成RGB
- 利用Bundle在activity之间传递对象
- Swift iOS : attributedText 富文本操作
- Objects.requireNonNull方法说明
- 制作无白色栏、无白色背景框的加载等待dialog
- Binary Search
- 算法实现Alg17《Infrared Small-Target Detection Using Multiscale Gray Difference Weighted Image Entropy 》
- iOS 11: CORE ML—浅析
- 清除 SQL Server 错误日志文件
- java集合排序
- CSS中的ZOOM属性