【问题标题】:How to set Line Spacing in UI Text View (Empty text view) to enter message with line space如何在 UI 文本视图(空文本视图)中设置行间距以输入带有行间距的消息
【发布时间】:2014-06-23 12:32:12
【问题描述】:

在我的应用程序中,我需要在 ui 文本视图上设置一些行距..

我知道我们可以使用段落样式间距为不可编辑的文本视图/标签做到这一点

但是在我的应用程序中,当我输入文本时它不起作用,

只有当我有预定义的文本时我才能这样做,如果我清除文本段落 sty 将不起作用

 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.minimumLineHeight = 35.f;
    paragraphStyle.maximumLineHeight = 35.f;

    UIFont *font = [UIFont fontWithName:@"AmericanTypewriter" size:18.f];
    NSString *string = @"This is a test";
    NSDictionary *attributtes = @{
                                  NSParagraphStyleAttributeName : paragraphStyle,
                                  };
    deedTextView.font = font;
    deedTextView.attributedText = [[NSAttributedString alloc] initWithString:string
                                                                   attributes:attributtes];

但是,我没有像 NSString *string = @"This is a test"; 这样的预定义文本

文本视图必须为空,而开始

【问题讨论】:

    标签: iphone objective-c uitextview nsattributedstring


    【解决方案1】:

    我遇到了同样的问题。根据 Sergius 的回答,我想出了以下工作解决方案。 Sergius 回答的问题是所有其他已设置的属性都将被覆盖(字体、颜色...)

    所以最好编辑现有的 typingAttributes:

    NSDictionary* d = deedTextView.typingAttributes;
    NSMutableDictionary* md = [NSMutableDictionary dictionaryWithDictionary:d];
    [md setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    deedTextView.typingAttributes= md;
    

    【讨论】:

      【解决方案2】:

      想到的一个简单选项如下。

      使用UITextViewDelegate 中的这两种方法之一,您可以实现您想要的:

      - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
      - (void)textViewDidChange:(UITextView *)textView;
      

      您的算法如下-取textView.text,将其转换为您需要的样式并将textView.text设置为textView.attributedText

      你也可以试试这样设置:

      deedTextView.typingAttributes = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
      

      【讨论】:

      • 我试过不行,打字时每个字符都跳到下一行
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      相关资源
      最近更新 更多