介绍两种方法来实现:
第一种:
初始化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
}
鉴于原生开发工具在文本样式上控制的局限性,行业内成熟的APP应用都采用了Native+Web的混合视图,即在APP中嵌入浏览器框架,加载HTML网页。这样就可以完美的呈现富文本样式。比如微信的自媒体:这样的方式需要建立独立的H5站点,考虑到资讯页面一般都需要以链接的方式分享到站外,所以大都数阅读类的APP都是同时开发H5站和APP,利用一个后台提供内容给H5站和APP业务模型如下图所示:
在APP中嵌入浏览器加载内容唯一的缺点就是加载速度相对慢一些。但普通用户基本上看不出区别,所以是当前最流行的实现方式
使用DTCoreText等文字效果代码类库(iOS平台)
H5承载页 +
内嵌浏览器框架的方式虽然可以完美的呈现文本内容,但加载速度会慢一些,而且展示风格会有一点点突兀那么还有一种方法就是使用DTCoreText
DTCoreText
是一个功能十分强大的文字效果代码类库。在UITextView上实现十分丰富的文字效果,包括文字大小、颜色、字体、下划线,链接,给文字加上图片、视频,文字任意间距等等。实现类似于CSS网页的文字效果。
使用这种实现方法的APP比如,同样是以文字评论为主的APP,用户体验相比豆瓣电影就立刻显得高大上了:
这种方式可以直接实现富文本效果,但鉴于应用本身也需要以外链的方式分享到SNS站点,所以同样需要建立独立的H5站点
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)