代码之家  ›  专栏  ›  技术社区  ›  Max Seelemann

在UITextView中设置行高

  •  55
  • Max Seelemann  · 技术社区  · 15 年前

    我已经非常确定不能用任何公共API来完成,但我仍然想问:

    是否有任何方法可以更改UITextView中的行高度?

    静态地做就足够了,不需要在运行时更改它。问题是默认的行高度太小了。文本看起来非常压缩,当试图写更长的文本时,这是一场噩梦。

    谢谢, 马克斯

    编辑: UIWebView 不可编辑 . 我需要一个可编辑的文本与可接受的行高组成部分。来自Omni框架的东西也没有帮助,因为它太慢而且感觉不对劲。。。

    8 回复  |  直到 15 年前
        1
  •  110
  •   Graham Perks    9 年前

    ios7之后,styleString方法不再有效。

    有两种新的选择。

    首先,TextKit;一个强大的新布局引擎。要更改行距,请设置UITextView的布局管理器的代理:

    textView.layoutManager.delegate = self; // you'll need to declare you implement the NSLayoutManagerDelegate protocol
    

    - (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
    {
        return 20; // For really wide spacing; pick your own value
    }
    

    其次,iOS7现在支持NSParagraphStyle的行距。这提供了更多的控制,例如第一行缩进和边界矩形的计算。所以或者。。。

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.headIndent = 15; // <--- indention if you need it
    paragraphStyle.firstLineHeadIndent = 15;
    
    paragraphStyle.lineSpacing = 7; // <--- magic line spacing here!
    
    NSDictionary *attrsDictionary =
    @{ NSParagraphStyleAttributeName: paragraphStyle }; // <-- there are many more attrs, e.g NSFontAttributeName
    
    self.textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello World over many lines!" attributes:attrsDictionary];
    

    FWIW,旧的contentInset方法沿着UITextView的左边缘对齐文本,在iOS7下也没有用。相反,要删除边距:

    textView.textContainer.lineFragmentPadding = 0;
    
        2
  •  25
  •   Fattie    11 年前


    [UITextView styleString] :

    @interface UITextView ()
    - (id)styleString; // make compiler happy
    @end
    
    @interface MBTextView : UITextView
    @end
    @implementation MBTextView
    - (id)styleString {
        return [[super styleString] stringByAppendingString:@"; line-height: 1.2em"];
    }
    @end
    

        3
  •  21
  •   Vegard    9 年前

    在UITextView的属性检查器中,改为更改属性 Text Attributed (来自 Plain "more" 按钮,您可以在那里设置行高和间距。

    UITextView's Attribute Inspector

        4
  •  19
  •   Max Seelemann    14 年前

    . 听起来很傻,但似乎是唯一现实的方式。

        5
  •  10
  •   user1172004    13 年前

    只有在UITextView上定义了定义styleString的类别时,UITextView子类重写styleString才有效,否则会出现编译错误。例如,在UITextView子类中:

    #import "SomeDangTextView.h"
    
    @interface UITextView ()
    
    - (id)styleString;
    
    @end
    
    @implementation SomeDangTextView
    
    - (id)styleString {
        return [[super styleString] stringByAppendingString:@"; line-height: 1.5em"];
    }
    
    @end
    
        6
  •  1
  •   Raphael    5 年前

    只需实现以下方法 UITextViewDelegate

    let textViewAttributes: [NSAttributedString.Key:Any] = [
        .font: UIFont.systemFont(ofSize: 15, weight: .medium),
        .foregroundColor: UIColor.black,
        .paragraphStyle: {
            let paragraph = NSMutableParagraphStyle()
            paragraph.lineSpacing = 4
            return paragraph
        }()
    ]
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        textView.typingAttributes = textViewAttributes
    }
    

    添加这些属性很重要 textViewDidBeginEditing 因为每次文本选择更改时字典都会重置。更多信息可以在 official documentation

        7
  •  -1
  •   Jordan    15 年前

    此类不支持文本的多种样式。指定的字体、颜色和文本对齐属性始终应用于文本视图的整个内容。要在应用程序中显示更复杂的样式,需要使用UIWebView对象并使用HTML呈现内容。

        8
  •  -1
  •   Ritesh verma    12 年前

    此类不支持文本的多种样式。指定的字体、颜色和文本对齐属性始终应用于文本视图的整个内容。要在应用程序中显示更复杂的样式,需要使用UIWebView对象并使用HTML呈现内容。

        9
  •  -4
  •   AAV    13 年前