我在我的项目中使用TTTAttributedLabel.我已经设法通过修改链接属性来更改我创建的任何链接的默认颜色和下划线.
NSArray *pKeys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName,
(id)kCTUnderlineStyleAttributeName
, nil];
NSArray *pObjects = [[NSArray alloc] initWithObjects:pAlertColor,[NSNumber numberWithInt:
kCTUnderlineStyleNone], nil];
NSDictionary *pLinkAttributes = [[NSDictionary alloc] initWithObjects:pObjects
forKeys:pKeys];
self.alertMessage.linkAttributes = pLinkAttributes;
self.alertMessage.activeLinkAttributes = pLinkAttributes;
Run Code Online (Sandbox Code Playgroud)
但是,我注意到当我点击链接时,它会随着任何其他链接在点击时的瞬间变为红色.我需要改变这种颜色.有关如何做到这一点的任何线索?
有容器视图和UITapGestureRecognizer
上面.它还有一个子视图TTTAttributedLabel
.
当我从容器视图中删除手势识别器时,委托方法 TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url can be called.
Run Code Online (Sandbox Code Playgroud)
当我在容器视图上添加手势识别器时.只调用其action方法.委托方法TTTAttributedLabelDelegate
不会被调用.
现在我需要在我点击链接时调用委托方法TTTAttributedLabel
,并在我点击容器视图的其他区域时调用action方法.
谢谢.
我需要a的文本UILabel
是黑色的,但在文本下面有一个浅灰色的下划线.是否可以使用NSAttributedString
或TTTAttributedLabel
?或者是否需要使用Core Graphics的自定义绘图?
澄清:我需要一个不同颜色下划线的特定颜色文本.示例:红色下划线上的蓝色文本.
我正在构建一个聊天应用程序,出于性能原因,我需要使用UILabel而不是UITextView来显示聊天消息.我之前使用的是TextView,但滚动时的数据检测非常缓慢且不连贯.
问题是目前没有链接/电话/地址等... UILabels的检测.
如何知道字符串中存在链接或电话号码的位置,然后突出显示并使其在UILabel中可单击?
我已经阅读了很多关于如何为链接添加属性的文章,但是它们都是你知道范围或子串的链接.
我想获取任何字符串并找出是否包含链接以及这些链接的位置,然后将tapGestureRecognizer添加到标签并根据点击发生的位置执行操作.
我试图合并一个外部库(TTTAttributedLabel),但我正在使用swift,发现swift的文档有限.我确实设法导入库但是没有自动检测到链接.
uitextview uilabel datadetectortypes tttattributedlabel swift
我想使用TTTAttributedLabel来检测UITableViewCell标签中文本的链接,但它不起作用.我正在使用iOS8的swift.下面是UITableViewCell代码:
class StoryTableViewCell: UITableViewCell, TTTAttributedLabelDelegate {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
// Link properties
let textLabel = self.descriptionLabel
let linkColor = UIColor(red: 0.203, green: 0.329, blue: 0.835, alpha: 1)
let linkActiveColor = UIColor.blackColor()
if (textLabel is TTTAttributedLabel) {
var label = textLabel as TTTAttributedLabel
label.linkAttributes = [NSForegroundColorAttributeName : linkColor]
label.activeLinkAttributes = [NSForegroundColorAttributeName : linkActiveColor]
label.enabledTextCheckingTypes = NSTextCheckingType.Link.toRaw()
label.delegate = self
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想UILabel
用一些带有可点击链接的文本制作一个.不是链接到网页,而是链接到像我这样做的行为UIButton
.所以我用的TTTAttributedLabel
是与之完美搭配的Objective C
.现在我想做同样的事情Swift
,所以我写了下面的代码:
self.someLabel.text = NSLocalizedString("Lost? Learn more.", comment: "")
let range = self.someLabel.text!.rangeOfString(NSLocalizedString("Learn more", comment:""))
self.someLabel.addLinkToURL (NSURL(string:"action://Learn more"), withRange:NSRange (range))
Run Code Online (Sandbox Code Playgroud)
但是,我无法使链接工作Swift
.我收到错误:“Missing argument for parameter 'host' in call”
最后一行.
我一直在研究获得可点击链接的解决方案.我可以在使用UITextView + NSAttributedString时使用它,但当它是UITableViewCell时它不会正确地自动布局.
现在我已经将TTTAttributedLabel添加到我的项目中,它完美地调整了视图的样式.链接也变为蓝色并加下划线.
但是点击它们什么都不做.我确实在我的控制器上实现了TTTAttributedLabelDelegate,在故事板中实现了标签实现MyLabel(它只是扩展了TTTAttributedLabel并具有委托选项,因为我希望它们在同一个函数中触发).现在我已经将控制器设置为我认为可能无法指向自身的委托.
但这些功能都没有被解雇,我得到了断点并登录了它.
我实现了didSelectLinkWithUrl和didLongPressLinkWithUrl.
func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
Debug.log("link clicked")
}
func attributedLabel(label: TTTAttributedLabel!, didLongPressLinkWithURL url: NSURL!, atPoint point: CGPoint) {
Debug.log("link long clicked")
}
Run Code Online (Sandbox Code Playgroud)
出口
@IBOutlet weak var content: MyLabel!
Run Code Online (Sandbox Code Playgroud)
MyLabel
导入UIKit导入TTTAttributedLabel
class MyLabel : TTTAttributedLabel, TTTAttributedLabelDelegate {
override func didMoveToSuperview() {
if (self.delegate == nil) {
self.delegate = self
}
self.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue
self.userInteractionEnabled = true
}
func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
Debug.log("link clicked")
}
func attributedLabel(label: TTTAttributedLabel!, didLongPressLinkWithURL url: …
Run Code Online (Sandbox Code Playgroud) 我在TTTAttributedLabel中添加了一个链接检测器,用于标识@mentions和#hashtags,并在我的TTTAttributedLabel实例中的该位置创建一个链接:
- (void)highlightMentionsInString:(NSString *)text withColor:(UIColor *)color isBold:(BOOL)bold isUnderlined:(BOOL)underlined
{
NSRegularExpression *mentionExpression = [NSRegularExpression regularExpressionWithPattern:@"(?:^|\\s)(@\\w+)" options:NO error:nil];
NSArray *matches = [mentionExpression matchesInString:text
options:0
range:NSMakeRange(0, [text length])];
for (NSTextCheckingResult *match in matches) {
NSRange matchRange = [match rangeAtIndex:1];
NSString *mentionString = [text substringWithRange:matchRange];
NSRange linkRange = [text rangeOfString:mentionString];
NSString* user = [mentionString substringFromIndex:1];
NSString* linkURLString = [NSString stringWithFormat:@"user:%@", user];
[self.attributedLabel addLinkToURL:[NSURL URLWithString:linkURLString] withRange:linkRange];
}
}
Run Code Online (Sandbox Code Playgroud)
我还发现我可以轻松更改链接颜色和属性:
NSArray *keys = [[NSArray alloc] initWithObjects:(id)kCTForegroundColorAttributeName,(id)kCTUnderlineStyleAttributeName
, nil];
NSArray *objects = [[NSArray alloc] initWithObjects:color,[NSNumber numberWithInt:kCTUnderlineStyleNone], nil]; …
Run Code Online (Sandbox Code Playgroud) 我有一个ios应用程序,它从服务器获取一段文本并将其显示在TTTAttributedLabel中.显示的文本将从HTML中删除.
例如
原始HTML
<p>
Hello <a href="http://www.google.com">World!</a>
</p>
Run Code Online (Sandbox Code Playgroud)
文本显示在TTTAttributedLabel中
Hello World!
Run Code Online (Sandbox Code Playgroud)
但是,我想像HTML一样可以点击"世界"这个词.我知道TTTAttributedLabel可以像
TTTAttributedLabel *tttLabel = <# create the label here #>;
NSString *labelText = @"Hello World!";
tttLabel.text = labelText;
NSRange r = [labelText rangeOfString:@"World"];
[tttLabel addLinkToURL:[NSURL URLWithString:@"http://www.google.com"] withRange:r];
Run Code Online (Sandbox Code Playgroud)
但如果"世界"这个词在文本中出现不止一次,则上述代码将是错误的.
任何人都可以提出一个更好的方法来处理这种情况吗?谢谢
我有一个奇怪的问题:我有一个带有链接的TTTAttributedLabel:
"bryan"这个名字就是链接.当a UIAlertView
通过此标签显示时,链接的字体会发生变化:
如您所见,链接丢失了字体大小和重量.谁看过这个吗?"背景中的链接"或其他东西是否有单独的字体属性?谢谢.
ios ×9
swift ×4
objective-c ×3
uilabel ×3
html ×1
hyperlink ×1
ios7 ×1
ios8 ×1
iphone ×1
uialertview ×1
uitableview ×1
uitextview ×1