Swift iOS : attributedText 富文本 *** 作

Swift iOS : attributedText 富文本 *** 作,第1张

概述通常给UILabel设置文本,我们都是设置属性UILabel.text。这意味着显示的文本是单一的,整个文本只能有一种同样的文本效果。而另外一个属性UILabel.attributedText,就可以可以分段设置的不同的字体、阴影效果等,比如前几个字为一个阴影效果,后几个字使用下划线效果。 如下代码我做了些改变,以便在Swift 3.0上可以运行,本来的代码来自 https://makeapppi

通常给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,length:1))        //: Make the second pizza red and outlined in Helvetica Neue        myMutableString.addAttribute(            NSFontAttributename,value: UIFont(                name: "Helvetica Neue",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 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)!,length: 7))        //: Appending the String with !!! and an Attributed String        let myAddedStringAttributes:[String:AnyObject]? = [            NSFontAttributename:UIFont(                name: "AvenirNext-Heavy",NSForegroundcolorAttributename:UIcolor.red,NSShadowAttributename: shadow        ]        let myAddedString = NSAttributedString(            string: "!!!",attributes: myAddedStringAttributes)        myMutableString.append(myAddedString)        //: Apply to the label        myLabel.attributedText = myMutableString    }}

相当的具有表现力。

总结

以上是内存溢出为你收集整理的Swift iOS : attributedText 富文本 *** 作全部内容,希望文章能够帮你解决Swift iOS : attributedText 富文本 *** 作所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1060745.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存