Ios 在UILabel中查找子字符串的绝对位置,以便在顶部放置另一个UILabel

Ios 在UILabel中查找子字符串的绝对位置,以便在顶部放置另一个UILabel,ios,objective-c,iphone,uilabel,coordinate,Ios,Objective C,Iphone,Uilabel,Coordinate,我在故事板中有以下UI 结构如下 prefix is |\ prefix width: 18.780000 prefix height: 0.000000 Old point {8, 0} Absolute point {49.333333333333314, 101.33333333333331} newLabel frame: {{68.113333333333316, 101.33333333333331}, {16, 43}} 父级(UIView) 薄型(UIView)(绿色)

我在故事板中有以下UI

结构如下

prefix is |\
prefix width: 18.780000
prefix height: 0.000000
Old point {8, 0}
Absolute point {49.333333333333314, 101.33333333333331}
newLabel frame: {{68.113333333333316, 101.33333333333331}, {16, 43}}
  • 父级(
    UIView
    • 薄型(
      UIView
      )(绿色)
      • MusicStaffView(
        UIView
        )(音乐符号,实际上是通过自定义类的设置加载的
        UILabel
我的目标是放置另一个
UILabel
,其中包含一个单独的音符,或者在播放音乐时在特定位置的顶部放置一个长度为1的字符串

最终结果应该是这样的

如上所示的设置基于自动布局。我的理解是,如果我需要手动将某个元素单独放置在顶部,那么我不需要为该元素启用自动布局

因此,我似乎需要在
UILabel
中找到单个字符的绝对位置,动态创建
UILabel
,然后将其添加为
UIViewController
视图的子级。但我不能做到这一点,不管发生什么,它总是放在左上方

下面是我用来获取目标字符串、其绝对位置、创建
UILabel
并将其动态放置在该位置的代码

// calculate prefix size
NSRange range = NSMakeRange(2, 1);
NSString *prefix = [self.leftPattern.scoreLabel.text substringToIndex:range.location];
NSDictionary *attributes = @{NSFontAttributeName: self.leftPattern.scoreLabel.font};
CGSize size = [prefix sizeWithAttributes:attributes];
CGPoint p = CGPointMake(size.width, 0);
NSLog(@"prefix width: %f",p.x);
NSLog(@"prefix height: %f",p.y);

// calculate size of target string
NSString *targetString = [self.leftPattern.scoreLabel.text substringWithRange:range];
CGSize targetSize = [targetString sizeWithAttributes:attributes];

NSLog(@"Old point %@", NSStringFromCGPoint(self.leftPattern.frame.origin));

// find absolute point to place our UILabel later
CGPoint localPoint = self.leftPattern.scoreLabel.frame.origin;
CGPoint absolutePoint = [self.leftPattern.scoreLabel convertPoint:localPoint toView:[self.view window]];

NSLog(@"Absolute point %@", NSStringFromCGPoint(absolutePoint));

// create UILabel to place at calculated absolute point
UILabel *newLabel = [[UILabel alloc]initWithFrame:CGRectMake(absolutePoint.x + p.x, absolutePoint.y - 10, targetSize.width, targetSize.height)];
NSLog(@"newLabel frame: %@", NSStringFromCGRect(newLabel.frame));
newLabel.text = targetString;
newLabel.font = self.leftPattern.scoreLabel.font;
newLabel.numberOfLines = 1;
newLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
newLabel.adjustsFontSizeToFitWidth = NO;
newLabel.clipsToBounds = YES;
newLabel.backgroundColor = [UIColor clearColor];
newLabel.textColor = [UIColor redColor];
newLabel.textAlignment = NSTextAlignmentCenter;
[newLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:newLabel];
[self.view layoutIfNeeded];
我从
NSLog()
得到的信息如下

prefix is |\
prefix width: 18.780000
prefix height: 0.000000
Old point {8, 0}
Absolute point {49.333333333333314, 101.33333333333331}
newLabel frame: {{68.113333333333316, 101.33333333333331}, {16, 43}}
此外,当我将设备旋转到横向时,情况变得更糟、更困难,
UILabel
与中心对齐,从而在前面引入空间,我也不知道如何计算其空间宽度


解决此问题的方案或建议将不胜感激

您检查过了吗?我强烈建议您使用这种方法来确定标签的内容大小:-只需复制一份标签,并将前缀文本设置为它,然后您将获得ecact偏移量作为标签的大小。然后设置要覆盖的字符串,大小将定义边界。但我很确定应该有更聪明的way@markov谢谢你的来电。我查看了您给我的链接,以及计算目标子字符串的矩形位置的类似解决方案。事实证明,尽管它可以计算x位置(似乎正确),但它对我不起作用。我需要解决的困难部分是作为故事板一部分的
UILabel
的对齐和自动布局,以及它的设置方式。因此,我改变了我的方法,通过代码以编程方式创建
UILabel
,并设置自动布局(约束)。现在可以了。当它稳定时,我可能会返回并添加我的解决方案作为答案。