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 ...
随机推荐
- Java 实现一个链表
public class MyList { static class Node {// 节点类 Object data; Node next; public Node(Object data) {// ...
- 2016/1/21 练习 arraylist 1,添加 add() 2,遍历集合
package shuzu; public class Customer { //从源码中 使用字段生成构造函数 public Customer(String good, int price) { s ...
- 学习MAP 地图好地址
http://www.cnblogs.com/beniao/archive/2010/01/13/1646446.html Bēniaǒ成长笔记在IT江湖里我不是一名老手,我只是一名普通的程序员,愿意 ...
- Apache Ignite——集合分布式缓存、计算、存储的分布式框架
Apache Ignite内存数据组织平台是一个高性能.集成化.混合式的企业级分布式架构解决方案,核心价值在于可以帮助我们实现分布式架构透明化,开发人员根本不知道分布式技术的存在,可以使分布式缓存.计 ...
- ANT-普通替换和正则替换
ant提供了两个指令用于编译时修改文件,好处就不说了 ,就说说如何使用吧. replaceregexp 和 replace的区别就和java中String replace和replaceAll一样 , ...
- flask装饰器route实现路由功能理解
利用装饰器的方式实现了路由函数,这是一个十分简单清晰的结构,而这个功能的实现,有着很大的学习意义 @appweb.route('index',methods=['GET','POST'] def st ...
- 洛谷 P2678 [ NOIP 2015 ] 跳石头 —— 二分答案
题目:https://www.luogu.org/problemnew/show/P2678 二分答案. 代码如下: #include<iostream> #include<cstd ...
- Win10出现键盘未失灵,按下的键都是快捷键的问题
某一天,WIN10开机.然后键盘莫名其妙的都无法正常使用,没有卡Window键,键盘也没有失灵,按下的键都成为了快捷键:终于在 https://zhidao.baidu.com/question/ ...
- bzoj 1029: [JSOI2007]建筑抢修【贪心+堆】
第一想法是按照结束时间贪心,但是这样有反例 所以先按照t贪心,能选则选,把选的楼的持续时间放进大根堆里,当当前的楼不能选的时候如果当前的持续时间比大根堆里最大的要小,就用这个替换最大,这样总数不变但是 ...
- bzoj 1087: [SCOI2005]互不侵犯King【状压dp】
显然是状压,设f[i][j][k]为1到i行选j个king,并且第i行状态为k的方案数,判断是否可行然后枚举转移即可 先把可行状态预处理出来会变快 #include<iostream> # ...