简体   繁体   English

iOS Instagram标签文字布局? 它是如何完成的?

[英]iOS Instagram Tags Text Layout? How Is It Done?

I've always been curious how instagram gets it's tags to layout fluidly and wanted to know what goes behind this functionality? 我一直很好奇Instagram如何获得流畅的布局标签,并想知道这个功能背后的原因是什么? To me, it seems like they're using UIButton's and just calculating placement of the buttons line by line? 对我来说,似乎他们正在使用UIButton并且只是逐行计算按钮的位置? Is that right? 那正确吗? Or is there a simpler method of doing it without having to manual calculate the placement of each tag line by line / or side by side given that multiple tag's width's fit together? 或者是否有一种更简单的方法可以在不必手动计算每个标记的位置的情况下逐行/或并排,因为多个标记的宽度适合在一起?

Or, are they using NSAttributedStrings? 或者,他们是否使用NSAttributedStrings?

Any ideas? 有任何想法吗?

Here's an example of the layout I'm referring to. 这是我所指的布局的一个例子。

在此输入图像描述

Well, I'm currently working on NSAttributedString and UITextView to get all this working, and my current code can do it. 好吧,我目前正在使用NSAttributedStringUITextView来完成所有这些工作,而我当前的代码可以做到这一点。 So I'm going to explain a possible version of doing this. 所以我将解释这样做的可能版本。
Here are the tips and big steps, with iOS7: 以下是iOS7的提示和重要步骤:
NSAttributedString , in iOS7 has a new attribute : NSLinkAttributeName . NSAttributedString中的NSAttributedString具有一个新属性: NSLinkAttributeName You can add it to your NSAttributedString , and with a detection method (combining: how to make #key and @key clickable in IOS7 and Get word from tap in UITextView ) you can manage it. 您可以将它添加到NSAttributedString ,并使用检测方法(组合: 如何在IOS7中使#key和@key可点击以及在UITextView中从tap中获取单词 ),您可以管理它。 That's for the general way of doing it. 这是一般的做法。 For previous version, you'll have to do a little more of work, but keeping this general idea. 对于以前的版本,您将需要做更多的工作,但保持这个一般的想法。
But since, it only for hashtags, since you can detect the world behind the tap, you can do this: Detect what word has been tapped (see previous links), detect if this word has an hashtag, and if yes, you can do whatever action you want. 但是,因为它只针对主题标签,因为你可以检测到水龙头背后的世界,你可以这样做:检测已点击的单词(参见前面的链接),检测这个单词是否有标签,如果是,你可以做无论你想做什么动作。 You can play with a NSRegularExpression to know where are the hashtags, and know when to "color" them. 您可以使用NSRegularExpression来了解标签的位置,并知道何时“着色”它们。

Here is a simplified way to do it using UITextView and NSAttributedString: 这是使用UITextView和NSAttributedString执行此操作的简化方法:

  1. Use regular expressions to get character ranges for links. 使用正则表达式获取链接的字符范围。
  2. Add link styling to the matched ranges. 将链接样式添加到匹配的范围。
  3. Add custom attribute to range that references tap action to execute. 将自定义属性添加到引用要执行的tap操作的范围。
  4. On touch, if index of character is inside a matched range, change the link formatting to highlight it and run the associated tap action. 在触摸时,如果字符索引在匹配范围内,请更改链接格式以突出显示它并运行相关的点击操作。

How to get index of character at location in iOS7: 如何在iOS7中获取位置的字符索引:

- (NSUInteger) getCharIndexAt: (CGPoint) location
{
    NSLayoutManager *layoutManager = self.layoutManager;
    NSUInteger characterIndex = NSNotFound;
    characterIndex = [layoutManager characterIndexForPoint:location inTextContainer:self.textContainer fractionOfDistanceBetweenInsertionPoints:NULL];

    return characterIndex;
}

Also check out WWDC 2013 intro to TextKit Demo 另请参阅WWDC 2013介绍TextKit演示

ios7 text utilities ios7文本实用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM