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

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

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


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


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

    _textView.textColor = [UIColor redColor];

完全没有作用,所以也需要通过属性来设置了,具体的要实现以上内容的代码请看下面

1.png

首先要支持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

比较容易看懂,就不多解释了。

目录
相关文章
|
安全 前端开发 iOS开发
钉钉里微应用ios 底部安全区域的颜色怎么修改?
钉钉里微应用ios 底部安全区域的颜色怎么修改?
464 5
|
iOS开发
iOS 渐变颜色 CGGradientCreateWithColorComponents 属性介绍
iOS 渐变颜色 CGGradientCreateWithColorComponents 属性介绍
251 0
|
iOS开发
iOS UIPageViewController 翻页背景颜色修改
iOS UIPageViewController 翻页背景颜色修改
91 0
|
图形学 iOS开发 Android开发
从Unity开发到移动平台制胜攻略:全面解析iOS与Android应用发布流程,助你轻松掌握跨平台发布技巧,打造爆款手游不是梦——性能优化、广告集成与内购设置全包含
【8月更文挑战第31天】本书详细介绍了如何在Unity中设置项目以适应移动设备,涵盖性能优化、集成广告及内购功能等关键步骤。通过具体示例和代码片段,指导读者完成iOS和Android应用的打包与发布,确保应用顺利上线并获得成功。无论是性能调整还是平台特定的操作,本书均提供了全面的解决方案。
646 0
|
前端开发 iOS开发
input框设置placeholder属性在iOS中显示不完整
input框设置placeholder属性在iOS中显示不完整
225 1
|
iOS开发
iOS 15后设置导航控制器的导航条背景色无效的问题处理
iOS 15后设置导航控制器的导航条背景色无效的问题处理
688 0
|
安全 开发者 iOS开发
iOS16系统手机设置开启开发者模式才能安装ipa包
iOS16系统手机设置开启开发者模式才能安装ipa包
848 1
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
按钮的image图片是非圆角,直接对UIButton设置圆角,iOS13系统没有圆角效果的问题及解决方案
156 0
|
API 开发工具 iOS开发
在应用研发平台EMAS中,ios的推送有没有办法在app端设置在收到通知后是否展示的逻辑
在应用研发平台EMAS中,ios的推送有没有办法在app端设置在收到通知后是否展示的逻辑
183 1