如何使用可点击的第一个单词创建UILabel [英] How to create UILabel with clickable first word

查看:161
本文介绍了如何使用可点击的第一个单词创建UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iOS中创建标签,任何人都可以帮助我使标签文本的第一个单词变为粗体和可点击。
标签显示用户名及其注释,第一个单词始终是用户名。
提前致谢!

解决方案

我想更优雅的解决方案是使用



请注意应该在Xcode / AppCode中打开 xcworkspace ,因为我正在使用 CocoaPods 这里。



希望有所帮助。



BR。

Eugene


I want to create label in iOS, can anyone help me to make the first word of the label's text bold and clickable. The label displays username and its comment and the first word is always the username. Thanks in advance!

解决方案

I suppose more elegant solution will be using TTTAttributedString or similar.

Example:

Output:

2013-03-10 07:16:54.429 ClickableUILabel-SO[4770:c07] UserName clicked
Address:    {
    comment = "Your comment.";
    userName = user2126537;
}
2013-03-10 07:16:55.460 ClickableUILabel-SO[4770:c07] UserName clicked
Address:    {
    comment = "Another comment.";
    userName = nsgulliver;
}

Key point:

...

NSRange userNameRange = [text rangeOfString: userName];

...

label.delegate = self;
[label addLinkToAddress: @{
           @"userName" : userName,
            @"comment" : comment
    }
                  withRange: userNameRange];

...

- (void) attributedLabel: (TTTAttributedLabel *)label
didSelectLinkWithAddress: (NSDictionary *)addressComponents
{
    NSLog(@"UserName clicked\nAddress:\t%@", addressComponents);
}

Complete source code

Note that you should open xcworkspace in Xcode/AppCode because I'm using CocoaPods here.

Hope it helps.

BR.
Eugene

这篇关于如何使用可点击的第一个单词创建UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆