【问题标题】:Auto detect links in tableview UILabel自动检测表视图 UILabel 中的链接
【发布时间】:2011-07-31 14:58:30
【问题描述】:

我发现了很多关于这个主题的东西。我只想将标签中出现的链接自动检测为链接和超链接。我查看了three20 项目 TTtwitter,但这看起来真的很复杂,我不知道从哪里开始集成它。

有谁知道这是否可以通过简单的方式完成,或者有人可以向我解释一下吗?

提前谢谢!

【问题讨论】:

    标签: iphone objective-c ios hyperlink uilabel


    【解决方案1】:

    会建议你去UIWebView

    使用UIWebView 的以下属性来检测链接..

    @property(nonatomic) UIDataDetectorTypes dataDetectorTypes
    

    【讨论】:

    • 但是我当前拥有的文本加载在一个标签中,我可以像标签一样设置所有属性吗?例如行数。
    • 顺便说一句,我正在使用带有海关单元格的 tableView。这些自定义单元格中有标签。我应该将它从 UILabel 更改为 UIWebView 吗?
    • @peter :是的,你需要从 UILabel 更改为 UIWebView
    • 我试图改变它,但它并没有按照我需要的方式运行。链接以相同的 uiwebview 打开它,并且由于这是表格视图的单元格的子视图,因此不能以正确的方式工作。除此之外,我只能在启用用户交互时单击链接。由于我使用它来扩展和缩回单元格,因此存在一些问题。
    【解决方案2】:

    您不能在 UILabel 或 UIButtons 中为文本添加下划线。这是我在类似情况下所做的。

    我想在看起来像链接的视图中添加下划线文本,但我不想要 UIWebView 的功能,我需要标准的目标/操作 UIControl 功能。我所做的只有在您想要显示一行文本并且看起来像一个链接时才有效。像使用标准 UIButton 一样使用它。您应该使用 type = UIButtonTypeCustom 创建。

    我创建了一个 UIButton 的子类,它通过覆盖 drawRect 来支持单行文本的下划线。我在子类中添加了BOOL _titleLabelUnderlined iVar。

    XButton.m

    - (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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多