0

I have added a label programmatically, and it works fine, except that local characters like å, ä, ö gets cut off att the top. This is true even if I change _nothingToShow.lineBreakMode = NSLineBreakByClipping; to NSLineBreakByCharWrapping.

I set up the label like this:

-(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]
           ]];
    }
}

and setting up the 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];
  }

How do I make sure the text is not cut off? Do I set the height of the label in some way? Or shrink the text to fit the label?

  • You need to adjust the label's height based on it's contents. You can implement the same solution given [here](https://stackoverflow.com/questions/30450434/figure-out-size-of-uilabel-based-on-string-in-swift). – Ayazmon Feb 19 '20 at 12:55

1 Answers1

1

Try adding a top and bottom constraint constant value like:

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

if this didn't work for you then your issue is with the continer that you use to add the lable, just make the hight bigger.

Omar Masri
  • 349
  • 1
  • 8