UITextView中,能不能单独设置每行文字的字体,颜色,大小

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UITextView中,能不能单独设置每行文字的字体,颜色,大小相关的知识,希望对你有一定的参考价值。

在UITextField中自带placeholder属性,可以用于提示输入框信息。但是UITextView并不具备此功能
介绍两种方法来实现:
第一种:
初始化UITextView
//首先定义UITextView
UITextView *textView = [[UITextView alloc] init];
textView.font = [UIFont systemFontOfSize:14];
textView.frame =CGRectMake(10, 0, cell.contentView.bounds.size.width-20, side);
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
textView.backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:textView];
textView.hidden = NO;
textView.delegate = self;
//其次在UITextView上面覆盖个UILable,UILable设置为全局变量。
uilabel.frame =CGRectMake(17, 8, cell.contentView.bounds.size.width - side+10, 20);
uilabel.text = @"请填写审批意见...";
uilabel.enabled = NO;//lable必须设置为不可用
uilabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:uilabel];
实现UITextView的代理
-(void)textViewDidChange:(UITextView *)textView

self.examineText = textView.text;
if (textView.text.length == 0)
uilabel.text = @"请填写审批意见...";
else
uilabel.text = @"";



第二种:
UITextView 实现 placeholder 及隐藏键盘

#import <Foundation/Foundation.h>
@interface UIPlaceHolderTextView : UITextView
NSString *placeholder;
UIColor *placeholderColor;

@private
UILabel *placeHolderLabel;


@property(nonatomic, retain) UILabel *placeHolderLabel;
@property(nonatomic, retain) NSString *placeholder;
@property(nonatomic, retain) UIColor *placeholderColor;

-(void)textChanged:(NSNotification*)notification;

@end

#import "UIPlaceHolderTextView.h"
@implementation UIPlaceHolderTextView
@synthesize placeHolderLabel;
@synthesize placeholder;
@synthesize placeholderColor;

- (void)dealloc

[[NSNotificationCenter defaultCenter] removeObserver:self];
[placeHolderLabel release]; placeHolderLabel = nil;
[placeholderColor release]; placeholderColor = nil;
[placeholder release]; placeholder = nil;
[super dealloc];


- (void)awakeFromNib

[super awakeFromNib];
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];


- (id)initWithFrame:(CGRect)frame

if( (self = [super initWithFrame:frame]) )

[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];

return self;


- (void)textChanged:(NSNotification *)notification

if([[self placeholder] length] == 0)

return;


if([[self text] length] == 0)

[[self viewWithTag:999] setAlpha:1];

else

[[self viewWithTag:999] setAlpha:0];



- (void)setText:(NSString *)text
[super setText:text];
[self textChanged:nil];


- (void)drawRect:(CGRect)rect

if( [[self placeholder] length] > 0 )

if ( placeHolderLabel == nil )

placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
placeHolderLabel.lineBreakMode = UILineBreakModeWordWrap;
placeHolderLabel.numberOfLines = 0;
placeHolderLabel.font = self.font;
placeHolderLabel.backgroundColor = [UIColor clearColor];
placeHolderLabel.textColor = self.placeholderColor;
placeHolderLabel.alpha = 0;
placeHolderLabel.tag = 999;
[self addSubview:placeHolderLabel];


placeHolderLabel.text = self.placeholder;
[placeHolderLabel sizeToFit];
[self sendSubviewToBack:placeHolderLabel];


if( [[self text] length] == 0 && [[self placeholder] length] > 0 )

[[self viewWithTag:999] setAlpha:1];


[super drawRect:rect];

@end

//隐藏键盘,实现UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text

if ([text isEqualToString:@"\n"])
[m_textView resignFirstResponder];
return NO;

return YES;
参考技术A 在UITextField中自带placeholder属性,可以用于提示输入框信息。但是UITextView并不具备此功能
介绍两种方法来实现:
第一种:
初始化UITextView
//首先定义UITextView
UITextView *textView = [[UITextView alloc] init];
textView.font = [UIFont systemFontOfSize:14];
textView.frame =CGRectMake(10, 0, cell.contentView.bounds.size.width-20, side);
textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
textView.backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:textView];
textView.hidden = NO;
textView.delegate = self;
//其次在UITextView上面覆盖个UILable,UILable设置为全局变量。
uilabel.frame =CGRectMake(17, 8, cell.contentView.bounds.size.width - side+10, 20);
uilabel.text = @"请填写审批意见...";
uilabel.enabled = NO;//lable必须设置为不可用
uilabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:uilabel];
实现UITextView的代理
-(void)textViewDidChange:(UITextView *)textView

self.examineText = textView.text;
if (textView.text.length == 0)
uilabel.text = @"请填写审批意见...";
else
uilabel.text = @"";



第二种:
UITextView 实现 placeholder 及隐藏键盘

#import <Foundation/Foundation.h>
@interface UIPlaceHolderTextView : UITextView
NSString *placeholder;
UIColor *placeholderColor;

@private
UILabel *placeHolderLabel;


@property(nonatomic, retain) UILabel *placeHolderLabel;
@property(nonatomic, retain) NSString *placeholder;
@property(nonatomic, retain) UIColor *placeholderColor;

-(void)textChanged:(NSNotification*)notification;

@end

#import "UIPlaceHolderTextView.h"
@implementation UIPlaceHolderTextView
@synthesize placeHolderLabel;
@synthesize placeholder;
@synthesize placeholderColor;

- (void)dealloc

[[NSNotificationCenter defaultCenter] removeObserver:self];
[placeHolderLabel release]; placeHolderLabel = nil;
[placeholderColor release]; placeholderColor = nil;
[placeholder release]; placeholder = nil;
[super dealloc];


- (void)awakeFromNib

[super awakeFromNib];
[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];


- (id)initWithFrame:(CGRect)frame

if( (self = [super initWithFrame:frame]) )

[self setPlaceholder:@""];
[self setPlaceholderColor:[UIColor lightGrayColor]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];

return self;


- (void)textChanged:(NSNotification *)notification

if([[self placeholder] length] == 0)

return;


if([[self text] length] == 0)

[[self viewWithTag:999] setAlpha:1];

else

[[self viewWithTag:999] setAlpha:0];



- (void)setText:(NSString *)text
[super setText:text];
[self textChanged:nil];


- (void)drawRect:(CGRect)rect

if( [[self placeholder] length] > 0 )

if ( placeHolderLabel == nil )

placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
placeHolderLabel.lineBreakMode = UILineBreakModeWordWrap;
placeHolderLabel.numberOfLines = 0;
placeHolderLabel.font = self.font;
placeHolderLabel.backgroundColor = [UIColor clearColor];
placeHolderLabel.textColor = self.placeholderColor;
placeHolderLabel.alpha = 0;
placeHolderLabel.tag = 999;
[self addSubview:placeHolderLabel];


placeHolderLabel.text = self.placeholder;
[placeHolderLabel sizeToFit];
[self sendSubviewToBack:placeHolderLabel];


if( [[self text] length] == 0 && [[self placeholder] length] > 0 )

[[self viewWithTag:999] setAlpha:1];


[super drawRect:rect];

@end

//隐藏键盘,实现UITextViewDelegate
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text

if ([text isEqualToString:@"\n"])
[m_textView resignFirstResponder];
return NO;

return YES;

word 能不能将一行改成20个字?

可以!!!

点击菜单栏的“文件”——“页面设置”,打开“页面设置”对话框,切换到“文档网格”选项卡,在“网格”的选项下选择“指定行和字符网格”,然后在“字符”下的“每行”的选择框中选择“20”

参考资料:www.word329.cn

参考技术A 二楼正解。不过,还需要注意的是,你说的每行设置成20个字,还需要设置字体的字号,不然仍然会出错的,此时,可以继续单击此设置页面上的“字体设置”,继续设置字体的字号即可。实际上,是word自动调节字间距来实现的。本回答被提问者采纳 参考技术B 可以,将字体改成小二号,一行就可以放20个字

以上是关于UITextView中,能不能单独设置每行文字的字体,颜色,大小的主要内容,如果未能解决你的问题,请参考以下文章

怎么能让textview的每一行有横线

Swift 4:如何在UITextView中每行只设置一个单词,并为每行创建一个数组

UITextField和UITextView

WPS 版ppt中如何让一行中指定的字单击一下再出来出现

如何让UITextView文字垂直居中

怎么把图片上的字去掉啊?