扩展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用法的更多相关文章

  1. WPF中动态更新TextBlock文字中的超链接,文本

    1.------------------------------------------------------------------------- 修改超链接的文本文字: <TextBloc ...

  2. (转)Linux中显示空闲内存空间的free命令的基本用法

    这篇文章主要介绍了Linux系统中free命令的基本用法,用free命令查看内存空余信息是Linux系统入门学习中的基础知识,需要的朋友可以参考下   free 命令显示系统使用和空闲的内存情况,包括 ...

  3. html中的alt和title用法区别

    html中的alt和title用法区别 首先明确一下概念,alt是html标签的属性,而title既是html标签,又是html属性.title标签这个不用多说,网页的标题就是写在<title& ...

  4. 我给女朋友讲编程html系列(3) --html中的超链接标签-a标签 和 框架frame与框架集frameset

    我们浏览网页的时候,当单击某段文字或图片时,就会打开一个新的网页,这里面就使用了超链接. 就比如下图是一个导航类网页,当你单击某个链接就会打开新的网页. 比如,我拿我的qq空间“金河访谈”举例,新建一 ...

  5. servlet中doGet()和doPost()的用法

    转自:https://blog.csdn.net/qq_38963960/article/details/79468182 1.servlet中doGet()和doPost()的用法 一般来说我们是用 ...

  6. Delphi中stringlist分割字符串的用法

    Delphi中stringlist分割字符串的用法 TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 1.CommaT ...

  7. SQL Server中Rowcount与@@Rowcount的用法 和set nocount on 也会更新@@Rowcount

    rowcount的用法: rowcount的作用就是用来限定后面的sql在返回指定的行数之后便停止处理,比如下面的示例, set rowcount 10select * from 表A 这样的查询只会 ...

  8. Linq中关键字的作用及用法

    Linq中关键字的作用及用法 1.All:确定序列中的所有元素是否都满足条件.如果源序列中的每个元素都通过指定谓词中的测试,或者序列为空,则为 true:否则为 false. Demo: 此示例使用 ...

  9. iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片

    Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...

随机推荐

  1. 关于中国菜刀,如何"切菜"

    介绍 经典标题党,中国菜刀有大牛已经分析过了->传送门(http://blog.csdn.net/p656456564545/article/details/49671829).博主PHP刚接触 ...

  2. 初步认识Promise

    在解释什么是Promise之前,先看一道练习题,做完练习题也就知道Promise到底是干嘛用的了. 假设现在有个需求:你要封装一个方法,我给你一个要读取文件的路径,你这个方法能帮我读取文件,并把内容返 ...

  3. 20175204 张湲祯 2018-2019-2《Java程序设计》 第一周学习总结

    20175204 张湲祯 2018-2019-2<Java程序设计>第一周学习总结 教材学习内容总结 -第一章Java入门要点: -Java的地位:具有面向对象,与平台无关,安全稳定和多线 ...

  4. PHP 【一】

    输出    [输出在表格中] <!DOCTYPE html> <html> <body> <h1>My first PHP page</h1> ...

  5. # 20175333曹雅坤《Java程序设计》第1周学习总结

    教材学习内容总结 1.学习第一章PPT,安装JRE,JDK并配置path环境参数 2.在windows上使用dos命令运行教材第一章代码Hello.java和People.java 3.下载使用git ...

  6. Fast RCNN 中的 Hard Negative Mining

     Fast RCNN 中将与 groud truth 的 IoU 在 [0.1, 0.5) 之间标记为负例, [0, 0.1) 的 example 用于 hard negative mining. ...

  7. 使用 gcc 编译 libvmaf-1.3.9 时的 注意事项

    vmaf-1.3.9\wrapper\Makefile 首行添加 CXX = g++CC = gcccc = gcc CFLAGS_COMMON 行尾追加 -msse4.1 CFLAGS_COMMON ...

  8. Windows2016的 IIS中配置PHP7运行环境

    Windows2016的 IIS中配置PHP7运行环境 在Windows 的IIS(8.0)中搭建PHP运行环境: 一:安装IIS服务器 .进入控制面板>>程序和功能>>打开或 ...

  9. java----JDOM解析XML

    JDOM: 与DOM类似,基于树形结构 效率比DOM快 下载: http://www.jdom.org/dist/binary/jdom-2.0.6.zip 导包导java中的工程目录 jdom-2. ...

  10. 微信H5支付坑一--手续费未结算

    简单随笔小记: 场景:在微信H5支付的过程中,无论怎么支付完成,在微信商户后台查询手续费依然未扣除,当时手续费账户月为5元. 解决方法:起初无论怎么测试都不知道代码到底问题出在哪里了,想一下手续费账户 ...