【发布时间】:2011-07-31 14:58:30
【问题描述】:
我发现了很多关于这个主题的东西。我只想将标签中出现的链接自动检测为链接和超链接。我查看了three20 项目 TTtwitter,但这看起来真的很复杂,我不知道从哪里开始集成它。
有谁知道这是否可以通过简单的方式完成,或者有人可以向我解释一下吗?
提前谢谢!
【问题讨论】:
标签: iphone objective-c ios hyperlink uilabel
我发现了很多关于这个主题的东西。我只想将标签中出现的链接自动检测为链接和超链接。我查看了three20 项目 TTtwitter,但这看起来真的很复杂,我不知道从哪里开始集成它。
有谁知道这是否可以通过简单的方式完成,或者有人可以向我解释一下吗?
提前谢谢!
【问题讨论】:
标签: iphone objective-c ios hyperlink uilabel
会建议你去UIWebView
使用UIWebView 的以下属性来检测链接..
@property(nonatomic) UIDataDetectorTypes dataDetectorTypes
【讨论】:
您不能在 UILabel 或 UIButtons 中为文本添加下划线。这是我在类似情况下所做的。
我想在看起来像链接的视图中添加下划线文本,但我不想要 UIWebView 的功能,我需要标准的目标/操作 UIControl 功能。我所做的只有在您想要显示一行文本并且看起来像一个链接时才有效。像使用标准 UIButton 一样使用它。您应该使用 type = UIButtonTypeCustom 创建。
我创建了一个 UIButton 的子类,它通过覆盖 drawRect 来支持单行文本的下划线。我在子类中添加了BOOL _titleLabelUnderlined iVar。
- (void)drawRect:(CGRect)rect
{
// just in case...
[super drawRect];
if (_titleLabelUnderlined) {
CGContextRef context = UIGraphicsGetCurrentContext();
// determine the size of the titleLabel text based on the font
CGSize textSize =[self.titleLabel.text sizeWithFont:self.titleLabel.font
forWidth:self.bounds.size.width
lineBreakMode:UILineBreakModeTailTruncation];
// set the underline color
CGContextSetStrokeColorWithColor(context, self.currentTitleColor.CGColor);
// line width
CGContextSetLineWidth(context, 1.0f);
// calc the start and end points for the line
CGFloat minX = self.titleLabel.center.x - textSize.width/2.0f;
CGFloat maxX = self.titleLabel.center.x + textSize.width/2.0f;
// start the line
CGContextMoveToPoint(context, minX, CGRectGetMinY(self.titleLabel.frame) + CGRectGetHeight(self.titleLabel.frame) + 1.0f);
// draw the line to the end point
CGContextAddLineToPoint(context, maxX, CGRectGetMinY(self.titleLabel.frame) + CGRectGetHeight(self.titleLabel.frame) + 1.0f);
// commit the drawing
CGContextStrokePath(context);
}
}
【讨论】: