<分区>
我需要让 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/
相关文章:
swift - "contentsOfFile"返回nil,可能原因
objective-c - stringByReplacingOccurrencesOfString 循环?
iphone - 通过 UIPanGestureRecognizer 传递点击
ios - 为什么 `NSKeyedUnarchiver` 无法解码 `Date`
objective-c - 如何在 iOS 6.x 中以静默方式在后台启动应用程序
ios - 在 initWithCoder 中写入时未显示 CALayer 更改
objective-c - 如何从格式字符串(如 @"xxx=%@, yyy=%@")和对象的 NSArray 创建 NSString?