【问题标题】:UITextView text give small gap between each word in middle of textUITextView 文本在文本中间的每个单词之间给出了小的间隙
【发布时间】:2017-01-11 08:04:27
【问题描述】:

团队,

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

    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;

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

【问题讨论】:

  • 请提供屏幕截图。
  • 文本看起来更“合理”而不是“居中”
  • @d.felber 在“对齐”或“居中”这两种情况下,它将给另一个文本留出空白。如何避免 UITextView 文本中的空白。
  • @d.felber 在我的情况下,我需要使用 Justified 进行 NSAlignment。

标签: ios objective-c fonts uitextview


【解决方案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)

【讨论】:

  • 它的工作非常好,但是在句子结束时,它会尝试使用“-”符号指示符继续。如何避免“-”整个部分移到下一行
  • 使用连字符(音节分隔)时,一些对您的行来说太长的单词可能会被连字符(“-”)分隔如果您不希望这样,您可能需要使用@987654326 @。当您不允许使用连字符拆分单词时,您会得到问题中描述的结果。
【解决方案2】:

我通过将UITextFieldleftView 属性设置为具有所需填充大小的空视图解决了同样的问题:

UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
textView.leftView = paddingView;
textView.leftViewMode = UITextFieldViewModeAlways;

【讨论】:

    【解决方案3】:

    我还遇到了 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

    希望这对你有帮助。

    谢谢,杰。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 2011-04-01
      • 1970-01-01
      相关资源
      最近更新 更多