English 中文(简体)
在NSRange帮助下建立UIButton
原标题:Create UIButton on substring with help of NSRange

I have some text coming from server. It may be single line or multiline text. I have to display the text on UILabel, which is no problem for me. The problem is, I have to display UIButton on finding a particular substring of the same text. For example the text is Nitish 435-234-6543 India which is being displayed as follows :

Nitish  
435-234-6543  
India  

因此,当我发现435-234-6543时。 我必须在<>435-234-6543上显示UIButton。

注:

  1. The text is dynamic - coming from server. Above is only an example.
  2. UIButton will be a subview of UILabel.

I tried different ways like OHAttributedLabel, rectForLetterAtIndex and this too. But not getting success. What my idea is, to create the button when substring is found and to set the frame of button based on NSRange of substring. Is this a possibility? How can it be done? Or is there some other way to do this?

I guess it is the approach I am worried about.

最佳回答

——和;我努力缩小立场,但努力取得成功。 抽签决定立场。 我所想的一个直接解决办法是,为什么你不拿一个或两个标签和一个顿。

Suppose。 您从网络服务处获得的活力是:“Nitesh-56789- 试验”。

  1. Find range of string suppose i.e. "56789". www.un.org/Depts/DGACM/index_spanish.htm
  2. String before starting location of that range assign to one label i.e. assign "Nitesh" to lable one.www.un.org/Depts/DGACM/index_spanish.htm
  3. Now add one custom button with our searched string as a text(56789).www.un.org/Depts/DGACM/index_spanish.htm
  4. Now make sure in main string there something after our substring or not. Here I mean after our search string "56789" still "Test" remain so assign it to third lable.

    www.un.org/Depts/DGACM/index_spanish.htm

这里,你必须利用体积计算所有标签和纽扣带。 旧法。

问题回答

<>strong>1

Make your UILabel a UITextView and use the property dataDetectorTypes to have phone numbers as links automatically.


2

有一种方便的方法来确定任何案文的规模:

CGSize size = [label.text sizeWithFont:label.font 
                     constrainedToSize:CGSizeMake(label.frame.size.width, CGFLOAT_MAX) 
                         lineBreakMode:UILineBreakModeWordWrap];

现在,你可以确定哪一个领域是电话号码,将插座分为以下几条:

NSArray *comp = [label.text componentsSeparatedByString:@"
"];

然后核对数字。 现在,你必须从您的<代码>h 8,中计算确切框架。 可变:

CGFloat positionOfNumber; // the index of your line in comp cast to CGFloat
CGFloat buffer = 10; // fiddle with this
CGFloat buttonHeight = (size.height- 2*buffer)/[comp length];
CGFloat buttonY = buffer + positionOfNumber * buttonHeight;
CGRect buttonFrame = CGRectMake(0, buttonY, label.frame.size.width, buttonHeight); 

对于那些希望在这里找到完美解决办法的人,请登陆https://stackoverflow.com/a/20633388/2177402”>solution。 • 如何获得地下室。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...