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的更多相关文章

  1. 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  ...

  2. javafx ComboBox Event and change cell color

    public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...

  3. highcharts dynamic change line color

    mouseOut: function(){ this.series.graph.attr({"stroke","#ccc"}) }

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. CSS教程:vlink,alink,link和a:link

    超链接文字的状态可以通过伪类选择符+样式规则来控制. 一组专门的预定义的类称为伪类,主要用来处理超链接的状态.超链接文字的状态可以通过伪类选择符+样式规则来控制.伪类选择符包括: 总: a 表示 超链 ...

  8. iOS - UITableViewCell Custom Selection Style Color

    Customize UITextView selection color in UITableView Link : http://derekneely.com/2010/01/uitableview ...

  9. 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 ...

随机推荐

  1. QlikView格式化某一个单元格

    QlikView中能够创建透视表和垂直表,或者一般的Table.假如有的时候须要某一个单元格的样式和其它单元格不一样.颜色或者边框宽度等.能够通过下面方式实现: 工具栏里面有个button叫:Desi ...

  2. java的输入输出流(一)

    java中i/o流是java中核心的一部分,曾经学过.可是理解不够深入,渐渐的也就忘了,如今在从新学习下java的io处理,写下我学习的笔记.便于记忆,和总结归纳: 本文原创,转载请注明:http:/ ...

  3. 实现日、周、月排行统计 sql

    在如今很多系统中,都需要进行日.周.月排行统计,但是在网上寻找 了一番,发现很多都是相对的周.月排行,即周排行则用当前时间减去7天.这样我个人认为并不恰当.如月排行中,假设今天是4月22日,则从3月2 ...

  4. VELT-0.1.6开发:载入根文件系统

    快乐虾 http://blog.csdn.net/lights_joy/(QQ群:Visual EmbedLinux Tools 375515651) 欢迎转载,但请保留作者信息 VELT的全称是Vi ...

  5. [办公自动化]excel工作簿内的表无法删除,单击右键无删除键

    今天同事问,我自己的工作簿,没有设置保护,但是就是无法删除其中的工作表. 后来发现,她的excel工作簿打开的文件名后面显示[共享]. 原因找到了. 取消共享就可以了.

  6. 【bzoj4538】[Hnoi2016]网络

    我们考虑树剖,线段树上维护一个堆,保存不经过该段区间的路径的权值. 对于一条路径我们将对于线段树中的区间提取出来,在对于线段树中进行修改.也就是在堆中插入或删除. 对于一次询问,只要找到包含该点的线段 ...

  7. Linux/Android——Input系统之InputReader (七)【转】

    本文转载自:http://blog.csdn.net/jscese/article/details/42739197 在前文Linux/Android——Input系统之frameworks层Inpu ...

  8. jquery uploadify在谷歌浏和火狐下无法上传的解决方案(.Net版)

    在项目紧张的进行过程中,jquery uploadify上传不兼容的问题一直没有试着去解决,只幻想着用ie的人越来越多,怎么奈何firefox4刚推出,就有4000万的下载.......仰天长叹,记生 ...

  9. android EditText如何使光标随着输入内容移动

    EditText 使光标随着输入内容移动的setSelection()

  10. Opencv+Zbar二维码识别(一维码校正)

    一维码由一组规则排列的黑色线条.白色线条以及对应的字符组成.对倾斜的(没有严重形变)一维码的角度校正,可以根据其黑白相间.排列规则的特点,计算傅里叶频谱,通过傅里叶频谱中直线的倾斜角度计算空间域图像一 ...