当前位置: 首页 > news >正文

织梦做淘宝客网站高密市网站建设

织梦做淘宝客网站,高密市网站建设,wordpress发不了邮件,高校校园网站建设与运行前言#xff1a;AttributedString是Apple推出的可以实现单个字符或字符范围带相应属性的字符串。属性提供了一些文本特性#xff0c;可以让文本展示的样式更加丰富。在日常开发过程中#xff0c;我通常用于同一个Label中包含不同的字体大小或字体颜色的样式编写中。 使用举…前言AttributedString是Apple推出的可以实现单个字符或字符范围带相应属性的字符串。属性提供了一些文本特性可以让文本展示的样式更加丰富。在日常开发过程中我通常用于同一个Label中包含不同的字体大小或字体颜色的样式编写中。 使用举例 需求需要设置一个红底白字的Label attributedLabel UILabel()let contentStr NSString(string: AttributedString) let attStr NSMutableAttributedString(string: contentStr as String) attStr.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 20, weight: .medium), range: NSRange(location: 0, length: contentStr.length)) attStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.white, range: NSRange(location: 0, length: contentStr.length)) attStr.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.red, range: NSRange(location: 0, length: contentStr.length)) attributedLabel.attributedText attStr样式展示 文本属性介绍 从上方代码可以看出文本的属性是通过设置文本字体文本颜色文本背景颜色所实现的。所以下面来一一列举一些常用的文本属性及展示效果。 设置文本字体大小和粗细 attStr.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 20, weight: .medium), range: NSRange(location: 0, length: contentStr.length))效果 设置文本颜色 attStr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: NSRange(location: 0, length: contentStr.length))效果 设置背景颜色 attStr.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.red, range: NSRange(location: 0, length: contentStr.length))效果 设置下划线 attStr.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: contentStr.length))效果 设置下划线颜色 默认下划线颜色与文本颜色相同 attStr.addAttribute(NSAttributedString.Key.underlineColor, value: UIColor.green, range: NSRange(location: 0, length: contentStr.length))效果 拼接文本 先设置好相关文本属性然后将其相互连接 let contentStr1 NSString(string: Attributed) let attStr1 NSMutableAttributedString(string: contentStr1 as String) attStr1.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 20, weight: .medium), range: NSRange(location: 0, length: contentStr1.length)) attStr1.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: NSRange(location: 0, length: contentStr1.length))let contentStr2 NSString(string: String) let attStr2 NSMutableAttributedString(string: contentStr2 as String) attStr2.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 12, weight: .medium), range: NSRange(location: 0, length: contentStr2.length)) attStr2.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.green, range: NSRange(location: 0, length: contentStr2.length))attStr1.append(attStr2)attributedLabel.attributedText attStr1效果 Attributes创建及使用 attributes可以一次性创建多个属性attributes是一个字典 需求创建一个红底白字的Label let contentStr NSString(string: AttributedString) let attStr NSMutableAttributedString(string: contentStr as String) attStr.addAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20, weight: .regular),NSAttributedString.Key.foregroundColor : UIColor.white,NSAttributedString.Key.backgroundColor : UIColor.red], range: NSRange(location: 0, length: contentStr.length))attributedLabel.attributedText attStr效果 常用属性方法整合 我将常用的一些文本属性进行整合了一个类 import Foundation import UIKitpublic struct ZUAttributedString {public enum Font:String {case thin PingFangSC-Thincase light PingFangSC-Lightcase medium PingFangSC-Mediumcase regular PingFangSC-Regular}public enum Line{case nonecase midLinecase underLine}public static func attributeString(content:String,font: UIFont,alignment:NSTextAlignment? NSTextAlignment.center,textColor:UIColor?,backgroundColor: UIColor? nil,line:Line .none,lineSpacing:CGFloat 0) - NSMutableAttributedString{let contentStr NSString(string: content)let attStr NSMutableAttributedString(string: contentStr as String)//set colorif let textColor textColor {attStr.addAttribute(NSAttributedString.Key.foregroundColor, value: textColor, range: NSRange(location: 0, length: contentStr.length))}if let backgroundColor backgroundColor {attStr.addAttribute(NSAttributedString.Key.backgroundColor, value: backgroundColor, range: NSRange(location: 0, length: contentStr.length))}let style NSMutableParagraphStyle()if let align alignment {style.alignment align} else {style.alignment NSTextAlignment.center}if lineSpacing 0{style.lineSpacing lineSpacing}attStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSRange(location: 0, length: contentStr.length))attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: contentStr.length))switch line{case .none:breakcase .midLine:attStr.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attStr.length))breakcase .underLine:attStr.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attStr.length))break}return attStr}public static func attributeString(content:String,font:ZUAttributedString.Font,size:CGFloat,alignment:NSTextAlignment? NSTextAlignment.center,textColor:UIColor?,backgroundColor:UIColor? nil,line:Line .none,maximumLineHeight:CGFloat) - NSMutableAttributedString{let attStr NSMutableAttributedString(string: content)//set colorif let textColor textColor {attStr.addAttribute(NSAttributedString.Key.foregroundColor, value: textColor, range: NSRange(location: 0, length: content.count))}if let backgroundColor backgroundColor {attStr.addAttribute(NSAttributedString.Key.backgroundColor, value: backgroundColor, range: NSRange(location: 0, length: content.count))}let style NSMutableParagraphStyle()if let align alignment {style.alignment align} else {style.alignment NSTextAlignment.center}attStr.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSRange(location: 0, length: content.count))//set fontswitch font {case .thin:let font UIFont(name: ZUAttributedString.Font.thin.rawValue, size: size) ?? UIFont.systemFont(ofSize: size)attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: content.count))case .light:let font UIFont(name: ZUAttributedString.Font.light.rawValue, size: size) ?? UIFont.systemFont(ofSize: size)attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: content.count))breakcase .medium:let font UIFont(name: ZUAttributedString.Font.medium.rawValue, size: size) ?? UIFont.systemFont(ofSize: size)attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: content.count))breakcase .regular:let font UIFont(name: ZUAttributedString.Font.regular.rawValue, size: size) ?? UIFont.systemFont(ofSize: size)attStr.addAttribute(NSAttributedString.Key.font, value: font, range: NSRange(location: 0, length: content.count))break}switch line{case .none:breakcase .midLine:attStr.addAttribute(NSAttributedString.Key.strikethroughStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attStr.length))breakcase .underLine:attStr.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSMakeRange(0, attStr.length))break}let paragraphStyle NSMutableParagraphStyle()paragraphStyle.maximumLineHeight maximumLineHeight// Line spacing attributeattStr.addAttribute(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attStr.length))return attStr} }方法使用 let attStr1 ZUAttributedString.attributeString(content: Attributed, font: UIFont.systemFont(ofSize: 20, weight: .regular), textColor: UIColor.green, backgroundColor: nil) let attStr2 ZUAttributedString.attributeString(content: String, font: UIFont.systemFont(ofSize: 20, weight: .regular), textColor: UIColor.blue, backgroundColor: nil)attStr1.append(attStr2)效果 所以通过方法整合的方式可以更加方便便捷的使用AttributedString并且也可以更好的实现我们目标的文本样式 参考文章 iOS swift 带有attributeString的多行文本label Swift生成属性文本AttributedString 如果该文章对你有所帮助可以点赞、收藏并且关注一下 后续会持续更新更多技术内容
http://www.pierceye.com/news/40785/

相关文章:

  • 高权重网站做js代码跳转设计网站页面要怎么切图
  • 四平网站制作制作钓鱼网站的费用
  • 网站建设全做任务反佣金的网站
  • 网站集群建设相关的招标品牌网站建设网站
  • 新乐市建设银行网站建设网站需要购买哪些
  • 淄博 网站设计中铁建设集团官网登录
  • 住房和城乡建设部网站倪虹修改wordpress自带小工具
  • 展厅设计公司网站淘宝联盟推广做网站违法
  • 用路由器做简单的网站哪有网站建设明细报价表
  • 佛山网站设计制作公司做服装网站需要什么
  • 广告门网站seo排名优化培训价格
  • 网站搭建北京做网站要注意什么
  • 六年级上册如何做网站湖北外贸网站建设费用
  • 网站建设推广方式怎么优化网站关键词排名
  • 如何做明星的个人网站闵行区最新消息
  • 勐海县城乡建设局门户网站百度关键词搜索趋势
  • 织梦做的网站织梦修改网页做暧暧网站免费
  • 三沙网站建设西安seo交流
  • 微网站 html深圳出行最新消息
  • 宁波市国家高新区建设局网站网站百度一直没有收录
  • 河南龙王建设集团网站电脑培训班价目表
  • 开发网站网络公司有哪些平面设计学徒工资一般多少
  • 路南网站建设成都网站关键字优化
  • 学ui的网站广州网站定制多少钱
  • 网站后台 二级域名wordpress文章 404
  • 西安网站开发公司哪家强天眼企业信息查询系统官网
  • 为什么要建微信网站物业管理系统app
  • 毕业设计做网站题目重庆做网站公司哪家比较好
  • 柳州学校网站建设网站在vps能访问 在本地访问不了
  • 公司自建网站备案免费制作贺卡的app