ios - 使 NSString 中的某些单词成为粗体和可点击的标签

标签 ios objective-c nsstring ios8

<分区>


想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post .

关闭 8 年前

我需要让 NSString 中的某些单词可以点击,并且像标签一样使用不同的字体样式。

我有这样的代码:

NSString *str = @"This is my string and it is #cool and #fun.  Please click on the tags.";

所以上面的词 #cool 和 #fun 将成为 uibutton 操作的按钮。在该函数中,我会将 cool 或 fun 传递给新的 UIViewController。

谢谢!

最佳答案

这是一段代码

NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"Google"];
[str addAttribute: NSLinkAttributeName value: @"http://www.google.com" range: NSMakeRange(0,str.length)];
[str addAttribute:kCTFontAttributeName value: boldFontName range: NSMakeRange(0,str.length)];
yourTextField.attributedText = str;

编辑

为这样的字符串实现类似于 UIButton 操作的方法最接近的方法是首先使用 UITextView 中找到所选范围的矩形>firstRectForRange: 方法,然后用连接的操作覆盖实际不可见的 UIButton

查看 this answer.

关于ios - 使 NSString 中的某些单词成为粗体和可点击的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27220986/

上一篇:ios - 在 tableview 上添加图像自定义按钮背景

下一篇:ios - iphone 6 的自定义导航栏图像

相关文章:

swift - "contentsOfFile"返回nil,可能原因

objective-c - stringByReplacingOccurrencesOfString 循环?

iphone - 通过 UIPanGestureRecognizer 传递点击

ios - 为什么 `NSKeyedUnarchiver` 无法解码 `Date`

objective-c - 如何将事件指示器添加到导航栏?

objective-c - 如何在 iOS 6.x 中以静默方式在后台启动应用程序

ios - 在 initWithCoder 中写入时未显示 CALayer 更改

objective-c - 如何从格式字符串(如 @"xxx=%@, yyy=%@")和对象的 NSArray 创建 NSString?

ios - 在 Swift 中将函数保存到 NSUserDefaults

ios - 如果冲量太小,SKPhysicsBody 球不会反弹