UILabel: wrap and/or break inside word(s) with hyphen(s)

Elaborating on Matt’s answer here: https://stackoverflow.com/a/16502598/196358 it can be done using NSAttributedString and NSParagraphStyle. See below:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.hyphenationFactor = 1.0f;
    
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleString attributes:@{ NSParagraphStyleAttributeName : paragraphStyle }];
    
self.titleLabel.attributedText = attributedString;

This will cause the label to break at logical places mid-word using hyphens. It looks great, and is pretty simple to do. It requires iOS 6.0, but I’ve only tried it under 7.0.

WARNING: Font, Color, Alignment — basically nothing is inherited by attributed-text’s render from it’s hosting and/or owner UILabel (you need to add those attributes manually).

Leave a Comment

tech