我有一个UILabel,我想在文本开始的地方添加一个UIView。
问题是标签中的文本与中心对齐,所以我不知道如何确定实际文本从何处开始,以及如何定位这个UIView。
知道我怎么算这东西吗?
谢谢!
发布于 2016-08-16 20:59:31
你也可以用
yourLabel.sizeToFit()若要自动调整标签的大小以满足其所需空间,请执行以下操作。然后用以下内容读出标签的框架:
yourLabel.frame.size (width or height)发布于 2016-08-16 11:51:34
来自:How to find the position(x,y) of a letter in UILabel
NSRange range = [@"Good,Morning" rangeOfString:@","];
NSString *prefix = [@"Good,Morning" substringToIndex:range.location];
CGSize size = [prefix sizeWithFont:[UIFont systemFontOfSize:18]];
CGPoint p = CGPointMake(size.width, 0);
NSLog(@"p.x: %f",p.x);
NSLog(@"p.y: %f",p.y);您可以修改它以达到您的目的。
发布于 2016-08-16 11:51:54
您可以使用"nsattributedstring“获取文本大小。
let label = UILabel(frame: CGRectMake(0, 0, 100, 100))
label.text = "dshfk,j"
label.textAlignment = .Center
label.textColor = UIColor.redColor()
let string = label.attributedText
let width = string!.size().width
let height = string!.size().height
let view = UIView(frame: CGRectMake(width, height, width, height))
view.backgroundColor = UIColor.blackColor()然后你可以用这个..。
样本视图

https://stackoverflow.com/questions/38972212
复制相似问题