Change the color of a link in an NSMutableAttributedString
Swift
Updated for Swift 3
Use with a textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.green]
And in context:
let attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")
let linkRange = (attributedString.string as NSString).range(of: "@marcelofabri_")
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: linkRange)
let linkAttributes: [String : Any] = [
NSForegroundColorAttributeName: UIColor.green,
NSUnderlineColorAttributeName: UIColor.lightGray,
NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue]
// textView is a UITextView
textView.linkTextAttributes = linkAttributes
textView.attributedText = attributedString
textView.delegate = self
Swift 4:
let linkAttributes: [String : Any] = [
NSAttributedStringKey.foregroundColor.rawValue: UIColor.green,
NSAttributedStringKey.underlineColor.rawValue: UIColor.lightGray,
NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue]
Objective-C
Use with a textView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor greenColor]};
Source: this answer
And from this post:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
value:@"username://marcelofabri_"
range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
NSUnderlineColorAttributeName: [UIColor lightGrayColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
// assume that textView is a UITextView previously created (either by code or Interface Builder)
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
textView.attributedText = attributedString;
textView.delegate = self;
https://stackoverflow.com/questions/28361072/change-the-color-of-a-link-in-an-nsmutableattributedstring
Change the color of a link in an NSMutableAttributedString的更多相关文章
- change the color of a disabled TEdit?
change the color of a disabled TEdit? Author: P. Below Category: VCL {Question:How can I change the ...
- javafx ComboBox Event and change cell color
public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...
- highcharts dynamic change line color
mouseOut: function(){ this.series.graph.attr({"stroke","#ccc"}) }
- mplayer-for-windows change color scheme in win 7
Q: When I play movie on Windows7, always comes this message: The color scheme has been changed The f ...
- How to change the text color in the terminal
You can open the file ~/.bashrc and then choose the force_color_prompt=yes otherwise, you can change ...
- [Redux] Navigating with React Router <Link>
We will learn how to change the address bar using a component from React Router. In Root.js: We need ...
- CSS教程:vlink,alink,link和a:link
超链接文字的状态可以通过伪类选择符+样式规则来控制. 一组专门的预定义的类称为伪类,主要用来处理超链接的状态.超链接文字的状态可以通过伪类选择符+样式规则来控制.伪类选择符包括: 总: a 表示 超链 ...
- iOS - UITableViewCell Custom Selection Style Color
Customize UITextView selection color in UITableView Link : http://derekneely.com/2010/01/uitableview ...
- Chrome DevTools: Color tricks in the Elements Panel
shift + click to change the color format Tip one The Colour Platters are customeised for you .they s ...
随机推荐
- react-native redux 操作
1.项目目录 2.redux (1)app/redux/action/action.js /** * 步骤一 * 行为 action */ // 定义行为名称 export const CHANGE_ ...
- 如何在其他js 引入main.js 中 vue 的实例?
1.原因解析 经测试发现,代码先执行了 index.js >> main.js >> Home.vue scr/api/index.js src/main.js src/co ...
- 安装Nginx须要系统的辅助软件(linux)
安装Nginx须要系统的辅助软件(linux): yum -y install make gcc gcc-c++ ncurses-devel yum -y install zlib zlib-deve ...
- SpringMVC 学习笔记(五) 基于RESTful的CRUD
1.1. 概述 当提交的表单带有_method字段时,通过HiddenHttpMethodFilter 将 POST 请求转换成 DELETE.PUT请求,加上@PathVariable注解从而实现 ...
- [翻译]NUnit---String && Collection && File && Directory Assert (七)
StringAssert (NUnit 2.2.3) StringAssert类提供一系列检查字符串的方法. CollectionAssert (NUnit 2.4 / 2.5) Collection ...
- [翻译]NUnit---Condition Asserts && Comparisons Asserts && Type Asserts (五)
网址:http://www.cnblogs.com/kim01/archive/2013/03/31/2991597.html Condition Asserts 测试指定条件的方法称作条件测试,测试 ...
- luogu 3953 逛公园
noip2017 D1T3 逛公园 某zz选手看到数据范围直接就最短路计数了,结果写错了爆零 题目大意: N个点M条边构成的有向图,且没有自环和重边.其中1号点是起点,N号点是公园的终点,每条边有一个 ...
- 安装完Anaconda python 3.7,想使用python3.6方法
cmd使用命令: conda create -n py36 python=3.6 anaconda 安装好后,会有提示: To activate this environment, use:# > ...
- 深入理解JMM(Java内存模型) --(五)锁
锁的释放-获取建立的happens before 关系 锁是Java并发编程中最重要的同步机制.锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息. 下面是锁释放-获取的示例代 ...
- EasyUI Validatebox 验证框
转自:http://www.jeasyui.net/plugins/167.html 通过 $.fn.validatebox.defaults 重写默认的 defaults. 验证框(validate ...