UITextView的行间距

  • 设置UITextView的行间距
// 创建对象
UITextview *textView = [[UITextView alloc] init];
  // 设置属性
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
 // 设置行间距
paragraphStyle.lineSpacing = 5;
NSDictionary *attributes = @{
  NSFontAttributeName:[UIFont systemFontOfSize:17],
  NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:[self bejsonWithString:json] attributes:attributes];
  • NSMutableParagraphStyle简单介绍
@property(NS_NONATOMIC_IOSONLY) CGFloat lineSpacing;  // 行间距 
@property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacing;   // 段落间距
@property(NS_NONATOMIC_IOSONLY) NSTextAlignment alignment;   
@property(NS_NONATOMIC_IOSONLY) CGFloat firstLineHeadIndent;  // 第一行头缩进
@property(NS_NONATOMIC_IOSONLY) CGFloat headIndent;  // 头缩进
@property(NS_NONATOMIC_IOSONLY) CGFloat tailIndent;   // 尾部缩进
@property(NS_NONATOMIC_IOSONLY) NSLineBreakMode lineBreakMode;  // 换行模式
@property(NS_NONATOMIC_IOSONLY) CGFloat minimumLineHeight;  // 最小行高
@property(NS_NONATOMIC_IOSONLY) CGFloat maximumLineHeight;  // 最大行高
@property(NS_NONATOMIC_IOSONLY) NSWritingDirection baseWritingDirection;  // The base writing direction for the receiver.
@property(NS_NONATOMIC_IOSONLY) CGFloat lineHeightMultiple;  // The line height multiple
@property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacingBefore;
@property(NS_NONATOMIC_IOSONLY) float hyphenationFactor;
// Adds tabStop to the receiver.
-(void)addTabStop:(NSTextTab *)anObject NS_AVAILABLE(10_0, 9_0);
// Removes the first text tab whose location and type are equal to those of tabStop.
-(void)removeTabStop:(NSTextTab *)anObject NS_AVAILABLE(10_0, 9_0); 
-(void)setParagraphStyle:(NSParagraphStyle *)obj NS_AVAILABLE(10_0, 9_0);
  • UITextViewDelegate
// 开始编辑
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    NSLog(@"%s",__FUNCTION__);
    return  YES;
}
// 结束编辑
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    NSLog(@"%s",__FUNCTION__);
    return  NO;
}
// 编辑发生变化,再次编辑
-(void)textViewDidChange:(UITextView *)textView
{
    NSLog(@"%s",__FUNCTION__);
}
// 是否替换range内的文字text
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    // 当输入换行符,退出编辑
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return  YES;
}

你可能感兴趣的:(UITextView的行间距)