UITextView文本在文本中间的每个单词之间留出小的间隙

时间:2017-01-11 08:04:28

标签: ios objective-c fonts uitextview

我有UITextView添加了文本内容,使用字体系列avinar roma,我注意到每个单词之间存在小的差距,这是不一致的。如何避免每个单词的textview文本中的小差距。

enter image description here

    descriptionTextView = [[UITextView alloc] initWithFrame:CGRectMake(20, titlelabel.frame.origin.y + titlelabel.frame.size.height + 5, view.frame.size.width - 40, view.frame.size.height - 150)];
    descriptionTextView.backgroundColor = [UIColor clearColor];
    descriptionTextView.textColor = MH_PANTONE_444;
    descriptionTextView.textAlignment = NSTextAlignmentCenter;
    descriptionTextView.font = [UIFont fontWithName:MH_FONT_AVENIRROMAN size:MH_SCREEN_HEIGHT/48];
    descriptionTextView.text = [descriptionArray objectAtIndex:index];
    descriptionTextView.editable = NO;
    descriptionTextView.scrollEnabled = NO;
    descriptionTextView.selectable = YES;
    descriptionTextView.dataDetectorTypes = UIDataDetectorTypeAll;

请在上面附上图片 大学医学中心与剩余文本的差距不大。

3 个答案:

答案 0 :(得分:1)

设置hyphenationFactor应该可以解决您的问题。但是你必须使用NSAttributedString而不是纯文本:

UIFont *font = [UIFont fontWithName: MH_FONT_AVENIRROMAN size: MH_SCREEN_HEIGHT/48];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.hyphenationFactor = 1.0;

NSDictionary *attributes = @{ NSFontAttributeName:font,
                              NSForegroundColorAttributeName: MH_PANTONE_444;
                              NSParagraphStyleAttributeName: paragraphStyle };

NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString: [descriptionArray objectAtIndex:index]
                                                                                   attributes: attributes];

descriptionTextView.attributedText = attributedText;

https://developer.apple.com/reference/uikit/nsmutableparagraphstyle/1535553-hyphenationfactor

您可能需要调整hyphenationFactor,直到获得所需的结果。 (0.0-1.0)

答案 1 :(得分:0)

我通过将leftView的{​​{1}}属性设置为带有填充大小的空视图解决了同样的问题:

UITextField

答案 2 :(得分:0)

在iOS应用程序中,我还遇到了与自定义字体文本对齐的问题,这与垂直对齐有关。我想这是因为您在应用程序中使用的自定义字体。但幸运的是有一个解决方案。

每个字体文件都有一些关于显示字母的配置。对于左侧间距,其中一个属性为minLeftSideBearing,右侧间距为minRightSideBearing。我想通过更改它们可以解决这个间距问题。

请按照以下链接检查如何更改此属性。 http://www.andyyardley.com/2012/04/24/custom-ios-fonts-and-how-to-fix-the-vertical-position-problem/

如上所述,您需要安装字体工具。您可以从以下链接下载。

https://developer.apple.com/download/more/?=font

希望这对你有所帮助。

谢谢,杰伊。