Swift NSAttributedString的使用

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa; background-color: #ffffff }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff; min-height: 16.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; background-color: #ffffff }
span.s1 { color: #ba2da2 }
span.s2 { color: #000000 }
span.s3 { color: #272ad8 }
span.s4 { color: #703daa }
span.s5 { color: #d12f1b }
span.s6 { color: #4f8187 }
span.s7 { color: #3e1e81 }

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa; background-color: #ffffff }

NSMutableAttributedString

  

     let testAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.backgroundColor: UIColor.yellow,NSAttributedStringKey.strikethroughStyle:1] as [NSAttributedStringKey : Any]

let testAttributeString = NSAttributedString(string: "富文本测试", attributes:testAttributes)

self.testLabel.attributedText = testAttributeString

let prefix = "¥" + "100"

let suffix = "¥" + "200"

let string = prefix + suffix

let myAttribute = [NSAttributedStringKey.foregroundColor: UIColor.lightGray,

NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue,

NSAttributedStringKey.font: UIFont.systemFont(ofSize: 13)] as [NSAttributedStringKey : Any]

let redAttribute = [NSAttributedStringKey.foregroundColor: UIColor.red,

NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)] as [NSAttributedStringKey : Any]

let attString = NSMutableAttributedString(string: string)

attString.addAttributes(redAttribute, range:NSRange.init(location: 0, length: prefix.count))

attString.addAttributes(myAttribute, range: NSRange.init(location: prefix.count, length: suffix.count))

self.titleLabel.attributedText = attString

原文地址:https://www.cnblogs.com/jukaiit/p/9110793.html

时间: 2024-08-12 09:03:37

Swift NSAttributedString的使用的相关文章

NSAttributedString in Swift

转载自: https://www.invasivecode.com/weblog/attributed-text-swift/ I have been talking quite a lot in the past about how to customize text in your app to improve the UI of your applications. Before iOS 6, Core Text was the only available option for deve

iOS开发——实战篇Swift篇&UItableView结合网络请求,多线程,数据解析,MVC实战

UItableView结合网络请求,多线程,数据解析,MVC实战 学了这么久的swift都没有做过什么东西,今天就以自己的一个小小的联系,讲一下,怎么使用swift在实战中应用MVC,并且结合后面的高级知识:网络请求,JSON数据解析一起应用到一个项目中来. 好了,废话不多说,我们直接开始吧. 首先看看最终的效果: 是不是很简单,就是个UItableView显示一些简单的数据,如果你真的觉得太简单了,那么请绕道,寻找更深入东西,但或者没有你想的那么简单,这不仅仅是一个tableView,为什么呢

【iOS开发每日小笔记(十一)】iOS8更新留下的“坑” NSAttributedString设置下划线 NSUnderlineStyleAttributeName 属性必须为NSNumber

这篇文章是我的[iOS开发每日小笔记]系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧.它们可能会给用户体验.代码效率得到一些提升,或是之前自己没有接触过的技术,很开心的学到了,放在这里得瑟一下.90%的作用是帮助自己回顾.记忆.复习. 测试组的小伙伴们大显神威,iOS8刚发布,他们就把测试设备急速升级了,然后就是扑面而来的各种bug和他们各种幸灾乐祸的笑.没办法,老老实实修复bug! 来看看今天我遇到的一个问题: 项目中,我将一个简化的H

转:【iOS开发每日小笔记(十一)】iOS8更新留下的“坑” NSAttributedString设置下划线 NSUnderlineStyleAttributeName 属性必须为NSNumber

http://www.bubuko.com/infodetail-382485.html 这篇文章是我的[iOS开发每日小笔记]系列中的一片,记录的是今天在开发工作中遇到的,可以用很短的文章或很小的demo演示解释出来的小心得小技巧.它们可能会给用户体验.代码效率得到一些提升,或是之前自己没有接触过的技术,很开心的学到了,放在这里得瑟一下.90%的作用是帮助自己回顾.记忆.复习. 测试组的小伙伴们大显神威,iOS8刚发布,他们就把测试设备急速升级了,然后就是扑面而来的各种bug和他们各种幸灾乐祸

textEditorDemo:基于swift的一个富文本编辑器

TextEditorDemo swift:textEditorDemo一个简单的富文本编辑器 一个简单的富文本编辑器 (IPhone 5s Xcode 6.3 swift 1.2) 实现并解决了一些基本功能: 更改字体大小,粗体,下划线,斜体字.并进行了数据的存储 更多请查看网友StringX的文章:http://www.jianshu.com/p/ab5326850e74/comments/327660#comment-327660 在TextView中添加照片,以及照片存储 实现键盘隐藏和弹

[iOS] 使用UIRefreshControl 实现 UITableView下拉刷新(Swift版本)

首先,在viewDidLoad中初始化相关数据: override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. //添加刷新 refreshControl.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged) refreshCon

使用OC和swift创建系统自带的刷新界面

使用OC和swift创建系统自带的刷新界面 一:swift刷新界面代码: import UIKit class ViewController: UITableViewController { // 用于显示的数据源    var _dataSource:[String] = []        // 加载更多 状态 风火轮    var _aiv:UIActivityIndicatorView!        override func viewDidLoad() {        super.

iOS开发——OC和swift创建系统自带的刷新界面

使用OC和swift创建系统自带的刷新界面 一:swift刷新界面代码: import UIKit class ViewController: UITableViewController { // 用于显示的数据源    var _dataSource:[String] = []        // 加载更多 状态 风火轮    var _aiv:UIActivityIndicatorView!        override func viewDidLoad() {        super.

Swift简单新闻APP实例

1.利用swift开发一个简单的新闻APP 主要利用IOS的UITableViewController,和UIwebView,再加上HTTP请求返回json数据并解析 2.APP演示 主界面 点击新闻进入详情 下拉列表刷新 3.APPDelegate.swif // // AppDelegate.swift // UITableViewControllerDemo // // Created by 赵超 on 14-6-24. // Copyright (c) 2014年 赵超. All rig