如果我们有超过1行的文字,如何找到UILabel中最后一个字符的坐标? 我想在文本的最后添加一个图像。
答案 0 :(得分:1)
我认为您正在寻找NSTextAttachment
// create an NSMutableAttributedString
let fullString = NSMutableAttributedString(string: "Your text")
// create our NSTextAttachment
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "icon")
// wrap the attachment in its own attributed string so we can append it
let imageString = NSAttributedString(attachment: imageAttachment)
// add the NSTextAttachment wrapper to our full string, then add some more text.
fullString.append(imageString)
// draw the result in a label
yourLabel.attributedText = fullString
答案 1 :(得分:0)
不完全是,但您可以做的是找出使用-[NSString sizeWithFont:constrainedToSize:lineBreakMode:]
来容纳文字的标签有多高,一旦掌握了高度,就可以从那里开始工作,知道右边缘标签和高度,如何将图像定位为容器视图的子视图。
也就是说,您可能希望将其添加到标签底部标签的右侧,在这种情况下,将其添加为子视图,其x轴是标签的右边缘(标签的x轴+ width),并且imageview的y轴设置为标签的y轴+标签的高度,减去字体的大小应该将其放在正确的位置,但是,您可能想要而不是使用标签的字体在最后一次计算中使用高度属性,使用imageview的高度,使其与标签的底部和图像视图的底部齐平...很难说没有看到样机。
无论如何,这应该足以让你继续下去。
答案 2 :(得分:0)
您可以使用此代码根据宽度和内容获取文本的高度。
尝试使用此代码并在给定高度处插入图像。
-(float)getHeightByWidth:(NSString*)myString:(UIFont*)mySize:(int)myWidth
{
CGSize boundingSize = CGSizeMake(myWidth, CGFLOAT_MAX);
CGSize requiredSize = [myString sizeWithFont:mySize constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
return requiredSize.height;
}
快乐的编码......
答案 3 :(得分:0)
sizeWithFont
不会处理UILabel
边缘。