Find coordinates of a substring in UILabel

2020-03-05 02:06发布

I have a string coming from server which I am displaying on UILabel. It is within that string, I am identifying some particular substring. I want to place a button on that substring(button will be a subview of UILabel). For this I require substring coordinates. I went through this Gist but I am not able to understand it.

Suppose my complete string is abc, 567-324-6554, New York. I want 567-324-6554 to be displayed on button, for which I need its coordinates. How can I use above link to find coordinates of substring?

1条回答
来,给爷笑一个
2楼-- · 2020-03-05 02:38

Might be you can calculate using-

CGSize stringSize = [string sizeWithFont:myFont 
                       constrainedToSize:maximumSize 
                           lineBreakMode:self.myLabel.lineBreakMode];

Now New position-

x = self.myLabel.frame.origin.x + stringSize.width

and for y similarly you need to have code with consideration of x.

查看更多
登录 后发表回答