在UILabel中的断字处添加连字符 [英] add hyphens on word break in a UILabel

查看:186
本文介绍了在UILabel中的断字处添加连字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置UILabel lineBreakMode来打断单词并将连字符添加到打扰的单词上?

How do I set a UILabel lineBreakMode to break words and add hyphens to broken words?

wo损坏的标签-

a label with a broken wo-

rd应该看起来像这样

rd should look like this

推荐答案

在此处详细介绍Matt的答案: https://stackoverflow.com /a/16502598/196358 可以使用NSAttributedString和NSParagraphStyle完成.见下文:

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;

这将导致标签在使用连字符的单词中间的逻辑位置处中断.它看起来很棒,而且很简单.它需要iOS 6.0,但我只在7.0以下尝试过.

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.

这篇关于在UILabel中的断字处添加连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆