UILabel 内的 iPhone 应用程序自定义图像

发布于 2024-09-03 20:48:06 字数 153 浏览 7 评论 0原文

我有一个场景,我想找到 UILabel 中文本的结尾位置并获取其 x 和 y 坐标。然后我需要在 UILabel 文本的最后一个单词之后插入图像。当我单击应用程序中的特定图像时,将触发此事件。如何找到 UILabel 文本的结束字符的 x 和 y 是什么?

问候 阿亚兹阿拉维

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 find what are the x and y of ending character of UILabel text?

Regards
ayaz alavi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-09-10 20:48:06

Yang 的方法看起来不错,但是如果您出于某种原因不想调整标签大小(文本居中、文本会更改等),您可以在 NSString 中使用 UIKit 添加。当然,这仅适用于单行标签。

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

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);
南薇 2024-09-10 20:48:06

您可以调整标签大小,然后获取坐标并将其用于图像视图:(

[_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);

请注意,使用这种方法,您会遇到多行标签的问题,其中最后一行不是最长的。)

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.)

樱花坊 2024-09-10 20:48:06

我通过使用 iphone 和 UI textarea 支持的 unicode smileys 解决了这个问题。所以我只是继续将 unicode 字符附加到字段中的文本中,并且它继续向我显示图像。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文