iOS-UITextView设置行间距,内容颜色(变相设置类似UITextField的placeholder)

1.在用UITextView的时候一直很苦恼placeholder的设置,最开始是通过添加label,后来觉得麻烦,就通过UITextView本身的一些特性来进行模拟。

2.UITextView的行间距设置方法网上很容易搜的到,但是运用中却发现直接设置根本不行,原因是textView一开始必需要有内容(个中原因,不深究,估计是系统级的),但是有又很奇怪,所以想到个主意,一开始就给textView内容,输入时删除,这样就有了1中的placeholder效果,可谓一举两得。

3.做出了placeholder的效果,颜色也需要区别,发现这句:

    _textView.textColor = [UIColor redColor];

完全没有作用,所以也需要通过属性来设置了,具体的要实现以上内容的代码请看下面:
这里写图片描述
首先要支持UITextViewDelegate;
接着看代码:


#import "ViewController.h"

@interface ViewController ()<UITextViewDelegate>
{
    UITextView *_textView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.automaticallyAdjustsScrollViewInsets = NO;


    _textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, 300, 80)];
    _textView.delegate = self;
    _textView.text = @"请在这里输入内容";
    _textView.layer.cornerRadius = 10;
    _textView.layer.borderWidth = 1;
    _textView.layer.borderColor = [UIColor blackColor].CGColor;
    [self.view addSubview:_textView];

    //有内容时该设置才生效,需要切记
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = 8;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[UIColor grayColor]};
    _textView.attributedText = [[NSAttributedString alloc]initWithString:_textView.text attributes:attributes];


}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //收起键盘
    [_textView resignFirstResponder];

}

#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    //编辑时删除代替placeholder效果的内容
    if ([_textView.text isEqualToString:@"请在这里输入内容"]) {
        textView.text = @"";
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineSpacing = 8;
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[UIColor blackColor]};
        _textView.attributedText = [[NSAttributedString alloc]initWithString:_textView.text attributes:attributes];
    }

    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    //编辑时把placeholder效果颜色变为正常效果
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = 8;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[UIColor blackColor]};
    _textView.attributedText = [[NSAttributedString alloc]initWithString:_textView.text attributes:attributes];

}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    //若编辑结束,内容为空,变最开始placeholder效果
    if ([textView.text isEqualToString:@""]) {
        _textView.text = @"请在这里输入内容";
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineSpacing = 8;
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[UIColor grayColor]};
        _textView.attributedText = [[NSAttributedString alloc]initWithString:_textView.text attributes:attributes];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

比较容易看懂,就不多解释了。
下载地址

在 Swift 中,若需设置 `UITextView` 的行高和行间距,可以通过调整 `NSMutableParagraphStyle` 来实现。`UITextView` 的文本属性支持富文本格式,因此可以通过设置段落样式来控制行高和行间距。 具体实现方式如下:创建一个 `NSMutableParagraphStyle` 实例,并设置其 `lineSpacing` 属性以控制行间距,`minimumLineHeight` 和 `maximumLineHeight` 可用于设置行高范围。然后将该段落样式应用到 `NSAttributedString` 上,并将其赋值给 `UITextView` 的 `attributedText` 属性 [^2]。 以下是一个完整的示例代码: ```swift import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let textView = UITextView() textView.frame = CGRect(x: 20, y: 100, width: 300, height: 200) textView.font = UIFont.systemFont(ofSize: 16) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 10 // 设置行间距为10 paragraphStyle.minimumLineHeight = 20 paragraphStyle.maximumLineHeight = 20 let attributedString = NSAttributedString(string: "这是一个示例文本,用于演示如何设置UITextView的行高和行间距。", attributes: [ .font: UIFont.systemFont(ofSize: 16), .paragraphStyle: paragraphStyle ]) textView.attributedText = attributedString self.view.addSubview(textView) } } ``` 通过上述方式,可以灵活地控制 `UITextView` 中文本的行高和行间距,以满足不同的 UI 设计需求。 此外,也可以通过计算文本的绘制区域来动态调整行高,例如在布局中需要根据内容高度调整控件尺寸时,可以使用 `boundingRect` 方法结合字体和段落样式进行高度估算 [^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值