UINavigationBar multi-line title

Set the titleView property of the UINavigationItem. For example, in the view controller’s viewDidLoad method you could do something like: UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 44)]; label.backgroundColor = [UIColor clearColor]; label.numberOfLines = 2; label.font = [UIFont boldSystemFontOfSize: 14.0f]; label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; label.textAlignment = UITextAlignmentCenter; label.textColor = [UIColor whiteColor]; label.text = … Read more

Center NSTextAttachment image next to single line UILabel

You can use the capHeight of the font. Objective-C NSTextAttachment *icon = [[NSTextAttachment alloc] init]; UIImage *iconImage = [UIImage imageNamed:@”icon.png”]; [icon setBounds:CGRectMake(0, roundf(titleFont.capHeight – iconImage.size.height)/2.f, iconImage.size.width, iconImage.size.height)]; [icon setImage:iconImage]; NSAttributedString *iconString = [NSAttributedString attributedStringWithAttachment:icon]; [titleText appendAttributedString:iconString]; Swift let iconImage = UIImage(named: “icon.png”)! var icon = NSTextAttachment() icon.bounds = CGRect(x: 0, y: (titleFont.capHeight – iconImage.size.height).rounded() / … Read more

Animate text change in UILabel

I wonder if it works, and it works perfectly! Objective-C [UIView transitionWithView:self.label duration:0.25f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ self.label.text = rand() % 2 ? @”Nice nice!” : @”Well done!”; } completion:nil]; Swift 3, 4, 5 UIView.transition(with: label, duration: 0.25, options: .transitionCrossDissolve, animations: { [weak self] in self?.label.text = (arc4random()() % 2 == 0) ? “One” : “Two” }, … Read more

How can I get text to wrap in a UILabel (via UIViewRepresentable) without having a fixed width?

Possible solution is to declare the width as a variable on MyTextView: struct MyTextView: UIViewRepresentable { var width: CGFloat func makeUIView(context: Context) -> UILabel { let label = UILabel() label.lineBreakMode = .byWordWrapping label.numberOfLines = 0 label.preferredMaxLayoutWidth = width label.text = “Here’s a lot of text for you to display. It won’t fit on the screen.” … Read more