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

原文地址:https://www.cnblogs.com/feng9exe/p/9402372.html

时间: 2024-10-23 08:01:47

Change the color of a link in an NSMutableAttributedString的相关文章

change the color of a disabled TEdit?

Author: P. Below Category: VCL {Question:How can I change the color of a disabled (Edit1.Enabled := false;) control?I do not want the normal grey color. Answer:Two options: 1) place the control on a panel and disable the panel instead of the control.

Change column color for columnChart in extjs

var store = Ext.create('Ext.data.JsonStore', { fields: ['name', 'data'], data: [ { 'name': 'metric one',   'data':10 }, { 'name': 'metric two',   'data': 7 }, { 'name': 'metric three', 'data': 5 }, { 'name': 'metric four',  'data': 2 }, { 'name': 'me

javafx ComboBox Event and change cell color

public class EffectTest extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { stage.setTitle("ComboBoxSample"); Scene scene = new Scene(new Group(), 450, 250); ComboBox emailCom

[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 to add a param to change the Route: import React from 'react'; import {Provider} from 'react-redux'; import {Router, Route, browserHistory } from 're

第4章:缓冲区、着色器、GLSL

原文链接: http://www.rastertek.com/gl40tut04.html Tutorial 4: Buffers, Shaders, and GLSL This tutorial will be the introduction to writing vertex and pixel shaders in OpenGL 4.0. It will also be the introduction to using vertex and index buffers in OpenG

Go语言(golang)开源项目大全

转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析器控制台用户界面加密数据处理数据结构数据库和存储开发工具分布式/网格计算文档编辑器Encodings and Character SetsGamesGISGo ImplementationsGraphics and AudioGUIs and Widget ToolkitsHardwareLangu

Chrome浏览器二维码生成插件

  猛击就可以使用啦->>>猛击使用   源码如下: 源码打包   源码: jquery-2.1.3.min.js jquery.qrcode.min.js https://github.com/jeromeetienne/jquery-qrcode spectrum.css spectrum.js https://github.com/bgrins/spectrum manifest.json { "manifest_version": 2, "name&

.NET框架中SortedSet源码(红黑树)

// #define USING_HASH_SET // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** Class: SortedSet ** ** Purpose: A generic sorted set. ** ** Date: Au

jQuery mobile的swipeleft的使用

今天要用swipeleft这个事件,然后就找了jQuery mobile的官方文档看了一下.然后自己在里面又做了一个向右滑动的扩展,可以正常使用.但是有一个问题,向右滑动后,再往左滑动就没有反应了.但是代码里明明是给向左滑动添加了事件的. 下面是源代码: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="