I think it's a common issue when you have a set of words where you don't want a break line.
Sometimes the character between those words is a space or a hyphen, etc. In my case it's a point :)
This is my text 50.0/80.0
At the end I did it using the size label and measuring how much space I need for that string in particular:
UIFont *fontAwardNumber = [UIFont fontWithName:@"DIN-Bold" size:20];
NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize labelSize = (CGSize){customCell.awardValueLabel.bounds.size.width, FLT_MAX};
CGRect rectNeededForAwardNumber = [awardNumber boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: fontAwardNumber} context:context];
if (rectNeededForAwardNumber.size.height > customCell.awardValueLabel.bounds.size.height) {
//We need to add a breakline
NSRange range = [awardNumber rangeOfString:@"/"];
if (range.location != NSNotFound) {
awardNumber = [awardNumber stringByReplacingCharactersInRange:range withString:@"/\n"];
}
}
I found other solutions like replacing your space or hyphen for unbreakable characters:
Preventing line breaks in part of an NSAttributedString
But my question is more general, does NSAttributedString provide something to define a set of words as non breakable? Or is there any easier way to do it for a general set of words?