如何修复它,以便在以编程方式添加文本标签时我的文本标签不会在顶部被截断?

How do I fix it so that my text label isn't cut off at the top when programmatically adding a text label?

我以编程方式添加了一个 label,它工作正常,只是像 å, ä, ö 这样的本地字符在顶部被截断了。即使我将 _nothingToShow.lineBreakMode = NSLineBreakByClipping; 更改为 NSLineBreakByCharWrapping.

也是如此

我这样设置标签:

-(void)setupNoOldExcursionsView {
    _myView = [[UIView alloc] initWithFrame:self.view.frame];
    _myView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: _myView];

    [self.view bringSubviewToFront: _myView];

    if (@available(iOS 11.0, *)) {
        _nothingToShow = [[UILabel alloc] init];

        [self generalLabelSetUp];

        _myView.translatesAutoresizingMaskIntoConstraints = NO;
        _nothingToShow.translatesAutoresizingMaskIntoConstraints = NO;

        UILayoutGuide *g = self.view.safeAreaLayoutGuide;

        [NSLayoutConstraint activateConstraints:@[
               [_myView.topAnchor constraintEqualToAnchor:g.topAnchor constant:0.0],
               [_myView.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant:0.0],
               [_myView.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:0.0],
               [_myView.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:0.0],

               [_nothingToShow.centerXAnchor constraintEqualToAnchor: _myView.centerXAnchor],
               [_nothingToShow.centerYAnchor constraintEqualToAnchor: _myView.centerYAnchor],
               [_nothingToShow.leadingAnchor constraintEqualToAnchor: _myView.leadingAnchor constant:0],
               [_nothingToShow.trailingAnchor constraintEqualToAnchor: _myView.trailingAnchor constant:0]
           ]];
    }
}

并设置 label:

-(void)generalLabelSetUp {
      _nothingToShow.text = NSLocalizedString(@"NoExcursions", @"");
      _nothingToShow.textColor = [UIColor blackColor];
      _nothingToShow.textAlignment = NSTextAlignmentCenter;
      [_nothingToShow setNumberOfLines: 0];
//      _nothingToShow.lineBreakMode = NSLineBreakByClipping;
        _nothingToShow.lineBreakMode = NSLineBreakByCharWrapping;

      [_nothingToShow setCenter: CGPointMake(_noOldExcursions.center.x, _noOldExcursions.center.y - 40)];

      _nothingToShow.font = [UIFont apolloNoExperiences];
      _nothingToShow.textColor = [UIColor apolloBlueColor];

      [_noOldExcursions addSubview:_nothingToShow];
  }

如何确保文本没有被截断?我是否以某种方式设置 label 的高度?或者缩小文本以适合标签?

尝试添加顶部和底部约束常量值,例如:

   [_myView.topAnchor constraintEqualToAnchor:g.topAnchor constant:10.0],
   [_myView.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant: -10.0],

如果这对您不起作用,那么您的问题出在您用来添加标签的容器上,只需将高度调大即可。