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. 关于Windows 8使用WMP播放音乐时WUDFHost跑CPU和硬盘的问题解决

    Windows 8使用Windows Media Player播放音乐的时候.事实上有一个这种情况,WMP和某个什么名字看起来非常屌的进程跑CPU非常高,这个跑非常高视你插入的SD卡内的文件数或者移动 ...

  2. 合并小图片利器TexturePacker GUI

    合并小图片,能够非常大的节省网络开销.尤其如今的站点非常喜欢使用大量的小图标来做一些友好提示.当然使用图片文字也是一种选择. 只是这里推荐的是TexturePacker GUI ,这个确实是一款利器. ...

  3. HDOJ 5381 The sum of gcd 莫队算法

    大神题解: http://blog.csdn.net/u014800748/article/details/47680899 The sum of gcd Time Limit: 2000/1000 ...

  4. [Canvas画图] 藏图阁(16) 人体穴位

    本节目标: 趁着今天是愚人节.阿伟决定来重温一下学医的那段日子. 有那么一段时间, 阿伟对武侠小说和医学同一时候产生了浓厚的兴趣,当时最想学的就是葵花点穴手, 一阳指之类的点穴功夫.轻轻一点.就能把别 ...

  5. hdu1078 FatMouse and Cheese(记忆化搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目大意: 题目中的k表示横向或者竖直最多可曾经进的距离,不可以拐弯.老鼠的出发点是(1,1) ...

  6. SQLServer IP不能登录问题解决

    最近项目项目需要,要在windows2008R2服务器上部署项目,可是客户那边服务器上SqlServer也没有安装,一切都要自己解决,所以我就找了个2008的版本,进行安装,安装过程没有遇到什么问题, ...

  7. Credit Risk Scorecards Credit Risk Modeling 评分卡 KS AR

    https://cn.mathworks.com/help/finance/creditscorecard.validatemodel.html?requestedDomain=www.mathwor ...

  8. Android Studio常见问题

    1.导入他们项目时出现R文件出错 首先我们须要了解的是Android studio 是基于gradle的编译模式,内部没有gen文件夹更没有R文件,可是既然它报了这个错.肯定是有原因的.即Gradle ...

  9. P3199 [HNOI2009]最小圈 01分数规划

    裸题,第二个权值是自己点的个数.二分之后用spfa判负环就行了. 题目描述 考虑带权的有向图G=(V,E)G=(V,E)G=(V,E)以及w:E→Rw:E\rightarrow Rw:E→R,每条边e ...

  10. Could not find modernizr-2.6.2 in any of the sources GitLab: API is not accessible

    Could not find modernizr-2.6.2 in any of the sources GitLab: API is not accessible bundle exec rake ...