I have got scenario where i would like to find where text in UILabel is ending and get its coordinates in terms of x and y. Then I need to insert image right after last word of UILabel text. This event will be fired when I click on particular image in app. How can I 开发者_JAVA百科find what are the x and y of ending character of UILabel text?
Regards ayaz alavi
Yang's approach seems fine, but if you don't want to resize the label for some reason (text is centered, text will change, etc.), you might use the UIKit additions in NSString. Of course, this will only work properly for one-line labels.
CGSize textSize = [label.text sizeWithFont:label.font];
CGFloat imgX;
CGFloat imgY = label.frame.origin.y - imageView.frame.size.height;
if(label.textAlignment == UITextAlignmentLeft) {
imgX = label.frame.origin.x + textSize.width;
} else if(label.textAlignment == UITextAlignmentCenter) {
imgX = label.frame.origin.x + textSize.width / 2.0;
} else if(label.textAlignment == UITextAlignmentRight) {
// This part really depends on what you want to do in this case
imgX = label.frame.origin.x
+ label.frame.size.width
- textSize.width
- imageView.frame.size.width;
}
imageView.frame = CGRectMake(imgX,
imgY,
imageView.frame.size.width,
imageView.frame.size.height);
You could resize the label, then get the coordinates and use them for your image view:
[_label sizeToFit];
CGFloat x = _label.frame.origin.x + _label.frame.size.width;
CGFloat y = _label.frame.origin.y + _label.frame.size.height;
_imageView.frame = CGRectMake(x, y, imageWidth, imageHeight);
(Note that with this approach you'll run into problems with multi-line labels where the last line is not the longest.)
I solved this issue by using unicode smileys which are supported by iphone and UI textarea. so i just keep on appending unicode characters to the text in the field and it keeps on showing me images.
精彩评论