IOS - 像报纸一样证明 UILabel [英] IOS - Justify UILabel like in the newspaper

查看:28
本文介绍了IOS - 像报纸一样证明 UILabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对齐我的 UILabel/UITextView,如下图所示.

I'm trying to align my UILabel/UITextView like in the picture below.

任何人都知道如何以这种方式证明文本的合理性.

Anyone got a lead how to justify the text that way.

推荐答案

我也有一些 NSTextAlignementJustified 的问题.

I have some trouble with NSTextAlignementJustified too.

您可以使用 NSAttributedString 来实现这一点,方法是设置对齐方式并将 firstLineHeadIndent 设置为接近于零的值(为什么?嗯……嗯,我不知道):

You can achieve this with the use of NSAttributedString, by setting the alignment and by setting firstLineHeadIndent to something close to zero (why? hmmmm... well I don't know):

NSMutableParagraphStyle *paragraphStyles = [[NSMutableParagraphStyle alloc] init];
paragraphStyles.alignment                = NSTextAlignmentJustified;
paragraphStyles.firstLineHeadIndent      = 0.001f;
NSString *stringTojustify                = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
NSDictionary *attributes                 = @{NSParagraphStyleAttributeName: paragraphStyles};
NSAttributedString *attributedString     = [[NSAttributedString alloc] initWithString:stringTojustify attributes:attributes];

CGFloat labelWidth = self.view.frame.size.width * 0.7f;
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 0.5f - labelWidth * 0.5f,
                                                             100,
                                                             labelWidth,
                                                             800)];
myLabel.backgroundColor = [UIColor orangeColor];
myLabel.textColor = [UIColor whiteColor];
myLabel.numberOfLines = 0;
myLabel.attributedText = attributedString;
[myLabel sizeToFit];

[self.view addSubview:myLabel];

这篇关于IOS - 像报纸一样证明 UILabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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