Swift iOS : attributedText 富文本操作
2017-07-20 09:21
971 查看
通常给UILabel设置文本,我们都是设置属性UILabel.text。这意味着显示的文本是单一的,整个文本只能有一种同样的文本效果。而另外一个属性UILabel.attributedText,就可以可以分段设置的不同的字体、阴影效果等,比如前几个字为一个阴影效果,后几个字使用下划线效果。
如下代码我做了些改变,以便在Swift 3.0上可以运行,本来的代码来自 https://makeapppie.com/2016/07/05/using-attributed-strings-in-swift-3-0/ ,
可以运行起来,查看效果:
相当的具有表现力。
如下代码我做了些改变,以便在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 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 } }
相当的具有表现力。
相关文章推荐
- 关于iOS中的文本操作-管理text fields 和 text views
- iOS(Swift) TextField限制输入文本的长度(不是字数)
- iOS(Swift) TextField限制输入文本的长度(不是字数)
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- Text Line Dereplication BigData Tool 文本行去重化 大数据工具 130Gb 20亿行数据 60分钟即可完成去重操作 最快的单机版软件
- 在iOS中UILabel作为一个文本标签,在实现程序中几乎每个页面都会用到UILabel,UILabel的常用属性:textColor(文本颜色),text(文本值),font(文本字体),textA
- iOS开发之富文本(oc与swift两种都有)
- iOS文本布局探讨之二——关于TextKit框架中的字体描述
- iOS NSMutableAttributedString/NSAttributedString 富文本设置
- iOS使用NSMutableAttributedString 实现富文本(一行文本里面不同字体大小)
- iOS之UILabel和UITextView富文本操作
- NSMutableAttributedString IOS中的富文本
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- ios NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- iOS使用NSMutableAttributedString实现富文本
- 【Swift 2.1】共享文件操作小结(iOS 8 +)
- Swift控制手电筒操作(iOS)
- ABAP 对长文本的操作 SAVE_TEXT & READ_TEXT