NSTextField/NSTextView中显示超链接以及NSMutableAttributedString用法
扩展NSAttributedString
简单的实现方法是为NSAttributedString 添加一个category。
然后为此category添加额外的方法。
具体实现如下:
[代码]c#/cpp/oc代码:
@interface NSAttributedString (Hyperlink)
+(id)hyperlinkFromString:(NSString*)inString withURL:(NSURL*)aURL;
@end
@implementation NSAttributedString (Hyperlink)
+(id)hyperlinkFromString:(NSString*)inString withURL:(NSURL*)aURL
{
NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: inString];
NSRange range = NSMakeRange(0, [attrString length]);
[attrString beginEditing];
[attrString addAttribute:NSLinkAttributeName value:[aURL absoluteString] range:range];
// make the text appear in bl
[attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];
16
17
// next make the text appear with an underline
18
[attrString addAttribute:
NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];
[attrString endEditing];
return [attrString autorelease];
}
@end
NSTextField中添加超链接
[代码]c#/cpp/oc代码:
-(void)setHyperlinkWithTextField:(NSTextField*)inTextField
{
// both are needed, otherwise hyperlink won't accept mousedown
[inTextField setAllowsEditingTextAttributes: YES];
[inTextField setSelectable: YES];
NSURL* url = [NSURL URLWithString:@\"http://www.apple.com\"];
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init];
[string appendAttributedString: [NSAttributedString hyperlinkFromString:@\"Apple Computer\" withURL:url]];
// set the attributed string to the NSTextField
[inTextField setAttributedStringValue: string];
[string release];
}
NSTextView中添加超链接
[代码]c#/cpp/oc代码:
-(void)setHyperlinkWithTextView:(NSTextView*)inTextView
{ // create the attributed string
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
05
06
// create the url and use it for our attributed string
07
NSURL* url = [NSURL URLWithString: @\"http://www.apple.com\"];
[string appendAttributedString:[NSAttributedString hyperlinkFromString:@\"Apple Computer\" withURL:url]];
// apply it to the NSTextView's text storage
[[inTextView textStorage] setAttributedString: string];
[string release];
}
NSTextField/NSTextView中显示超链接以及NSMutableAttributedString用法的更多相关文章
- WPF中动态更新TextBlock文字中的超链接,文本
1.------------------------------------------------------------------------- 修改超链接的文本文字: <TextBloc ...
- (转)Linux中显示空闲内存空间的free命令的基本用法
这篇文章主要介绍了Linux系统中free命令的基本用法,用free命令查看内存空余信息是Linux系统入门学习中的基础知识,需要的朋友可以参考下 free 命令显示系统使用和空闲的内存情况,包括 ...
- html中的alt和title用法区别
html中的alt和title用法区别 首先明确一下概念,alt是html标签的属性,而title既是html标签,又是html属性.title标签这个不用多说,网页的标题就是写在<title& ...
- 我给女朋友讲编程html系列(3) --html中的超链接标签-a标签 和 框架frame与框架集frameset
我们浏览网页的时候,当单击某段文字或图片时,就会打开一个新的网页,这里面就使用了超链接. 就比如下图是一个导航类网页,当你单击某个链接就会打开新的网页. 比如,我拿我的qq空间“金河访谈”举例,新建一 ...
- servlet中doGet()和doPost()的用法
转自:https://blog.csdn.net/qq_38963960/article/details/79468182 1.servlet中doGet()和doPost()的用法 一般来说我们是用 ...
- Delphi中stringlist分割字符串的用法
Delphi中stringlist分割字符串的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 1.CommaT ...
- SQL Server中Rowcount与@@Rowcount的用法 和set nocount on 也会更新@@Rowcount
rowcount的用法: rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, set rowcount 10select * from 表A 这样的查询只会 ...
- Linq中关键字的作用及用法
Linq中关键字的作用及用法 1.All:确定序列中的所有元素是否都满足条件.如果源序列中的每个元素都通过指定谓词中的测试,或者序列为空,则为 true:否则为 false. Demo: 此示例使用 ...
- iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片
Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...
随机推荐
- end to end testing
概念 https://www.softwaretestinghelp.com/what-is-end-to-end-testing/ What is “End to End Testing”? Ter ...
- centos7.2 环境下 mysql-5.1.73 安装配置
安装mysql,安装前准备 如果mysql用户不存在,那么添加mysql用户 groupadd mysql useradd -g mysql mysql mysql编译安装 tar -zxvf mys ...
- Selective Search for Object Recognition(理解)
0 - 背景 在目标检测任务中,我们希望输入一副图像,输出目标所在的位置以及目标的类别.最常用的算法是滑动窗口方法,但滑动窗口其实相当于穷举图像中的所有子图像,其效率低且精度也受限.该论文提出一种新的 ...
- rocketmq 4.2.0集群配置
采用的是2m-2s-async模式 1. 修改每台机器的/etc/hosts 文件,增加如下内容 192.168.1.100 nameserver1192.168.1.102 nameserver ...
- 非root用户安装java版本
有时候,我们所用的用户并不是root用户,升级java版本,其实是一个非常简单的过程,具体过程如下: 将下载好的tar包进行解压,然后进行配置文件,在命令行敲入 vi ~/.bashrc,在这个文件 ...
- 【原创】大数据基础之Spark(1)Spark Submit即Spark任务提交过程
Spark2.1.1 一 Spark Submit本地解析 1.1 现象 提交命令: spark-submit --master local[10] --driver-memory 30g --cla ...
- 【原创】大叔问题定位分享(3)Kafka集群broker进程逐个报错退出
kafka0.8.1 一 问题现象 生产环境kafka服务器134.135.136分别在10月11号.10月13号挂掉: 134日志 [2014-10-13 16:45:41,902] FATAL [ ...
- [C]\x字符转义序列
概述 \x转义的定义是这样的 转义符 字符值 输出结果 \xh[h...] 具有此十六进制码的字符 输出此字符 问题 看似\x后面可以接受1或n个十六进制的字符,但是如果你把一个 ...
- Flask开发微电影网站(七)
1.后台管理之电影管理 1.1 定义电影表单 在app的admin目录的forms.py文件中,定义电影表单 # 电影表单 class MovieForm(FlaskForm): title = St ...
- 使用Pycharm创建一个Django项目
在使用python写脚本一段时间后,想尝试使用Django来编写一个python项目,现做以下记录备忘: 1.创建项目 如果本地没有安装与所选python版本对应Django版本,pycharm会自动 ...