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
相关文章
- How can I access the UI element AutoSuggestEdit on wpf in model? I need reload the item source of AutoSuggestEdit when input text changed
- What do 'lazy' and 'greedy' mean in the context of regular expressions?
- What is the scope of variables in JavaScript? 还没看完
- What are the uses of “using” in C#?
- What is the difference between Version and 'Runtime Version' in .Net?
- How to get the parents of a merge commit in git? 父结点
- How to pass values across the pages in ASP.net without using Session
- ErrorHandling in asp.net web api
- The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.
- The property 'RowId' is part of the object's key information and cannot be modified.
- 解决 warning 显示 The linear_assignment function is deprecated in 0.21 and will be removed from 0.23. Use scipy.optimize.linear_sum_assignment instead
- This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in问题
- The Use of Aliases in ElasticSearch
- Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘id‘ in field list is ambiguous
- The development prospect of SAP consultants in China in the next decade
- 解决"No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android"错误
- flink-sql-gateway:Caused by: org.apache.flink.table.api.NoMatchingTableFactoryException: Could not find a suitable table factory for 'org.apache.flink.table.factories.CatalogFactory' in the classpath.
- In PyTorch 1.1.0 and later, you should call them in the opposite order: `optimizer.step()` before `l
- Another unnamed CacheManager already exists in the same VM
- Unity 报错之 The same field name is serialized multiple times in the class or its parent class.
- 程序猿学英语——In September the English learning summary
- The total number of locks exceeds the lock table size,mysql update报错
- suseoj The wheat of the prime minister
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
- Glossary of Terms in the JavaTM platform --reference
- 316. Remove Duplicate Letters (accumulate -> count of the difference elements in a vector)
- * resolve_conffiles: Existing conffile /etc/config/dhcp is different from the conffile in the new package. The new conffile will be placed at /etc/config/dhcp-opkg.